public void InstantiateWithParameterList() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(FileNotFoundException), "No such file."); FileNotFoundException exception = new FileNotFoundException("No such file."); Assert.True(attr.Expects(exception)); }
public void ExpectsFailsIfParameterValuesDontMatch() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(FileNotFoundException), "No such file."); FileNotFoundException exception = new FileNotFoundException("A totally different message."); Assert.False(attr.Expects(exception)); }
override public IEnumerable <UnitTestInfo> GetUnitTests() { var methods = GetMethodsWithAttribute <TestMethodAttribute>(type).ToList(); var rl = methods.Select(x => new TraditionalUnitTestInfo() { UnitTestName = x.Name, method = x, suite = this }).ToList(); testInitMethods = GetMethodsWithAttribute <TestInitializeAttribute>(type); testCleanupMethods = GetMethodsWithAttribute <TestCleanupAttribute>(type); foreach (var uti in rl) { uti.ignored = uti.method.GetCustomAttribute <IgnoreAttribute>() != null; TestMethodAttribute tmattr = uti.method.GetCustomAttribute <TestMethodAttribute>(); uti.sourceCodePath = tmattr.File; uti.line = tmattr.Line + 1; ExpectedExceptionAttribute eeattr = uti.method.GetCustomAttribute <ExpectedExceptionAttribute>(); if (eeattr != null) { uti.ExceptionType = eeattr.ExceptionType; } } return(rl.Cast <UnitTestInfo>()); }
public ExpectedException(ExpectedExceptionAttribute expectedExceptionAttribute) { exp = expectedExceptionAttribute; if (exp == null) { throw new ArgumentNullException("expectedExceptionAttribute"); } }
private static void ProcessNoException(MethodInfo method) { ExpectedExceptionAttribute exceptionAttribute = (ExpectedExceptionAttribute)Reflect.GetAttribute(method, typeof(ExpectedExceptionAttribute)); if (exceptionAttribute != null) { Assert.Fail("Expected Exception of type <{0}>, but none was thrown", exceptionAttribute.ExceptionType); } }
public TestInfo(String Name, Func <Task> t, ExpectedExceptionAttribute expectsExceptionType, bool IsIgnored = false, string ignoredReason = "") { this.Name = Name; this.AsFunc = t; this.IsTask = true; this.IsIgnored = IsIgnored; this.IgnoredReason = ignoredReason; this.ShouldExecute = false; this.ExpectsException = expectsExceptionType; }
/// <summary> /// Default ctor /// </summary> public void SetTestMethod(MethodInfo method, MethodInfo setupMethod, MethodInfo teardownMethod, object instance) { Name = method.Name; this.method = method; this.setupMethod = setupMethod; this.teardownMethod = teardownMethod; this.instance = instance; expectedException = GetCustomAttribute <ExpectedExceptionAttribute>(method); ignore = GetCustomAttribute <NUnit.Framework.IgnoreAttribute>(method); isAsync = GetCustomAttribute <System.Runtime.CompilerServices.AsyncStateMachineAttribute>(method) != null; }
public static bool TryGetFirstExpectedExceptionAttribute(SyntaxList <AttributeListSyntax> attributes, out ExpectedExceptionAttribute expectedException) { var expectedExceptionNode = ExpectedExceptionHelper.GetExpectedExceptionAttributes(attributes).FirstOrDefault(); if (expectedExceptionNode != null) { expectedException = new ExpectedExceptionAttribute(expectedExceptionNode); return(true); } expectedException = null; return(false); }
public void ExpectedExceptionAttribute_should_raise_verification_error() { ITestMatcherFactory attr = new ExpectedExceptionAttribute(typeof(Exception)); var matcher = attr.CreateMatcher(TestContext); try { matcher.Matches(() => { throw new AssertException(); }); } catch (AssertVerificationException ex) { const string actualMessage = "Can't use `ExpectedExceptionAttribute` or `ThrowsAttribute` on a test that throws assertions exceptions if those exceptions might be caught by the attribute. Replace with `Assert.Throws`."; Expect(ex.Message).ToBe.EqualTo(actualMessage); Assert.Pass(); } }
public static TestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms) { TestMethod testMethod = new TestMethod(method, parentSuite); string fullName = method.ReflectedType.FullName; if (parentSuite != null) { fullName = parentSuite.FullName; } if (CheckTestMethodSignature(testMethod, parms)) { if (parms == null) { testMethod.ApplyCommonAttributes(method); } object[] customAttributes = method.GetCustomAttributes(typeof(ICommandDecorator), inherit: true); for (int i = 0; i < customAttributes.Length; i++) { ICommandDecorator item = (ICommandDecorator)customAttributes[i]; testMethod.CustomDecorators.Add(item); } ExpectedExceptionAttribute[] array = (ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), inherit: false); if (array.Length > 0) { ExpectedExceptionAttribute expectedExceptionAttribute = array[0]; string handler = expectedExceptionAttribute.Handler; if (handler != null && GetExceptionHandler(testMethod.FixtureType, handler) == null) { MarkAsNotRunnable(testMethod, $"The specified exception handler {handler} was not found"); } testMethod.CustomDecorators.Add(new ExpectedExceptionDecorator(expectedExceptionAttribute.ExceptionData)); } } if (parms != null) { method = testMethod.Method; if (parms.TestName != null) { testMethod.Name = parms.TestName; testMethod.FullName = fullName + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string text = (testMethod.Name = MethodHelper.GetDisplayName(method, parms.OriginalArguments)); testMethod.FullName = fullName + "." + text; } parms.ApplyToTest(testMethod); } return(testMethod); }
public void ArgumentNullExceltions() { new ExpectedExceptionAttribute((Type)null); // Should fail new ExpectedExceptionAttribute((String)null); // Should fail new ExpectedExceptionAttribute((Type)null, null); // Should fail new ExpectedExceptionAttribute((String)null, null); // Should fail new ExpectedExceptionAttribute("BogusTypeName"); // Should fail new ExpectedExceptionAttribute("BogusTypeName", null); // Should fail new ExpectedExceptionAttribute(_assertionException, null); // Should work new ExpectedExceptionAttribute(_assertionException.FullName, null); // Should work ExpectedExceptionAttribute expectedException = new ExpectedExceptionAttribute(_assertionException); expectedException.Message = null; // Should work expectedException.ExceptionType = null; // Should fail }
protected override void ResolveMembers() { expectedException = Method.GetCustomAttribute <ExpectedExceptionAttribute> (); if (expectedException != null) { expectedExceptionType = expectedException.ExceptionType.GetTypeInfo(); } if (!CheckReturnType()) { var declaringType = ReflectionHelper.GetTypeFullName(Method.DeclaringType.GetTypeInfo()); var returnType = ReflectionHelper.GetTypeFullName(Method.ReturnType.GetTypeInfo()); throw new InternalErrorException("Method '{0}.{1}' has invalid return type '{2}'.", declaringType, FullName, returnType); } base.ResolveMembers(); }
public void TestExpectedExceptionAttribute() { ExpectedExceptionAttribute expecterException; expecterException = new ExpectedExceptionAttribute(_assertionException); Assert.AreEqual(_assertionException, expecterException.ExceptionType); expecterException = new ExpectedExceptionAttribute(_assertionException.FullName); Assert.AreEqual(_assertionException, expecterException.ExceptionType); expecterException = new ExpectedExceptionAttribute(_assertionException, null); Assert.AreEqual(_assertionException, expecterException.ExceptionType); Assert.IsNull(expecterException.Message); expecterException = new ExpectedExceptionAttribute(_assertionException.FullName, null); Assert.AreEqual(_assertionException, expecterException.ExceptionType); Assert.IsNull(expecterException.Message); expecterException.ExceptionType = typeof(Exception); Assert.AreEqual(typeof(Exception), expecterException.ExceptionType); expecterException.Message = "message"; Assert.AreEqual("message", expecterException.Message); }
public void TestRunTestMethod() { ExpectedExceptionAttribute expectedException = new ExpectedExceptionAttribute(_assertionException); ITest test; test = MockTestingHelper.CreatePassTest(); expectedException.RunTest(test); Assert.AreEqual(TestStatus.Fail, test.Result.Status); test = MockTestingHelper.CreateFailTest(); expectedException.RunTest(test); Assert.AreEqual(TestStatus.Pass, test.Result.Status); expectedException.Message = "Invalid message"; expectedException.RunTest(test); Assert.AreEqual(TestStatus.Fail, test.Result.Status); expectedException.Message = MockFixture.FailMessage; expectedException.RunTest(test); Assert.AreEqual(TestStatus.Pass, test.Result.Status); }
private void ProcessException(MethodInfo method, Exception caughtException) { ExpectedExceptionAttribute exceptionAttribute = (ExpectedExceptionAttribute)Reflect.GetAttribute(method, typeof(ExpectedExceptionAttribute)); if (exceptionAttribute == null) { throw new NUnitLiteException("", caughtException); } Type expectedType = exceptionAttribute.ExceptionType; if (expectedType != null && expectedType != caughtException.GetType()) { Assert.Fail("Expected Exception of type <{0}>, but was <{1}>", exceptionAttribute.ExceptionType, caughtException.GetType()); } MethodInfo handler = GetExceptionHandler(method.ReflectedType, exceptionAttribute.Handler); if (handler != null) { InvokeMethod(handler, caughtException); } }
public void ReturnsCorrectExeptionFullName() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(FileNotFoundException)); Assert.Equals("System.IO.FileNotFoundException", attr.ExceptionTypeFullName); }
public ExpectedException(ExpectedExceptionAttribute exceptionAttribute) { ExpectedExceptionType = exceptionAttribute.ExpectedException; }
public void InstantiateWithEmptyParameterList() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(FileNotFoundException), new object[] {} ); Assert.Equals("System.IO.FileNotFoundException", attr.ExceptionTypeFullName); }
public void ParseAssemblies() { numTests=0; foreach (AssemblyItem ai in assemblyCollection.Values) { foreach (NamespaceItem ni in ai.NamespaceCollection.Values) { foreach (ClassItem ci in ni.ClassCollection.Values) { TestFixture tf=new TestFixture(); foreach (Attribute attr in ci.Attributes) { // verify that attribute class is "UnitTest" string attrStr=attr.ToString(); attrStr=StringHelpers.RightOfRightmostOf(attrStr, '.'); Trace.WriteLine("Class: "+ci.ToString()+", Attribute: "+attrStr); try { if (attr.GetType() == typeof(TestClassAttribute)) { TestUnitAttribute tua = new TestFixtureAttribute(); tua.Initialize(ci, null, attr); tua.SelfRegister(tf); } } catch(Exception e) { Trace.WriteLine("Exception adding attribute: "+e.Message); Trace.WriteLine("Attribute "+attrStr+" is unknown"); } } if (tf.HasTestFixture) { foreach(MethodItem mi in ci.MethodCollection.Values) { foreach (object attr in mi.Attributes) { // verify that attribute class is "UnitTest" string attrStr=attr.ToString(); attrStr=StringHelpers.RightOfRightmostOf(attrStr, '.'); Trace.WriteLine("Method: "+mi.ToString()+", Attribute: "+attrStr); try { if (attr.GetType() == typeof(TestMethodAttribute)) { TestUnitAttribute tua = new TestAttribute(); tua.Initialize(ci, mi, attr); tua.SelfRegister(tf); } else if (attr.GetType() == typeof(PexRaisedExceptionAttribute)) { PexRaisedExceptionAttribute excAttr = (PexRaisedExceptionAttribute)attr; TestUnitAttribute tua = new ExpectedExceptionAttribute(); tua.Initialize(ci, mi, attr); tua.SelfRegister(tf); } } catch(TypeLoadException) { Trace.WriteLine("Attribute "+attrStr+"is unknown"); } } } testFixtureList.Add(tf); numTests+=tf.NumTests; } } } } }
public override TestResult[] Execute(ITestMethod testMethod) { // NOTE // This implementation will need to be refactored as we add more // execution variations. int retryCount = 1; Type eet = null; Attribute[] attr = testMethod.GetAllAttributes(false); if (attr != null) { foreach (Attribute a in attr) { if (a is RetryAttribute) { RetryAttribute retryAttr = (RetryAttribute)a; retryCount = int.Parse(retryAttr.Value); } if (a is ExpectedExceptionAttribute) { ExpectedExceptionAttribute eea = (ExpectedExceptionAttribute)a; eet = eea.ExceptionType; } } } TestResult[] results = null; var res = new List <TestResult>(); var currentCount = 0; while (currentCount < retryCount) { currentCount++; try { results = base.Execute(testMethod); } catch (Exception e) { if (eet == null) { break; } if (e.GetType().Equals(eet) == false) { break; } } if (results == null) { continue; } foreach (var testResult in results) { testResult.DisplayName = $"{testMethod.TestMethodName} - Execution number {currentCount}"; } res.AddRange(results); if (results.Any((tr) => tr.Outcome == UnitTestOutcome.Failed)) { continue; } break; } return(res.ToArray()); }
public void ExpectedArgumentNullException() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(ArgumentNullException), "fileName"); Assert.True(attr.Expects(new ArgumentNullException("fileName"))); }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> private TestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms) { TestMethod testMethod = new TestMethod(method, parentSuite); testMethod.Seed = random.Next(); string prefix = method.ReflectedType.FullName; // Needed to give proper fullname to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) { prefix = parentSuite.FullName; //testMethod.FullName = prefix + "." + testMethod.Name; } if (CheckTestMethodSignature(testMethod, parms)) { if (parms == null) { testMethod.ApplyAttributesToTest(method); } foreach (ICommandDecorator decorator in method.GetCustomAttributes(typeof(ICommandDecorator), true)) { testMethod.CustomDecorators.Add(decorator); } ExpectedExceptionAttribute[] attributes = (ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); if (attributes.Length > 0) { ExpectedExceptionAttribute attr = attributes[0]; string handlerName = attr.Handler; if (handlerName != null && GetExceptionHandler(testMethod.FixtureType, handlerName) == null) { MarkAsNotRunnable( testMethod, string.Format("The specified exception handler {0} was not found", handlerName)); } testMethod.CustomDecorators.Add(new ExpectedExceptionDecorator(attr.ExceptionData)); } } if (parms != null) { // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we reassign it here. method = testMethod.Method; if (parms.TestName != null) { testMethod.Name = parms.TestName; testMethod.FullName = prefix + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments); testMethod.Name = name; testMethod.FullName = prefix + "." + name; } parms.ApplyToTest(testMethod); } return(testMethod); }
public void InstantiateWithEmptyParameterList() { ExpectedExceptionAttribute attr = new ExpectedExceptionAttribute(typeof(FileNotFoundException), new object[] {}); Assert.Equals("System.IO.FileNotFoundException", attr.ExceptionTypeFullName); }
private static TestResult RunTest(Type t, MethodInfo mi) { if (mi.GetCustomAttributes(typeof(IgnoreAttribute), true).Any()) { return(new TestResult() { TestName = mi.Name, Message = "skipped", Type = TestResultType.Skipped }); } ExpectedExceptionAttribute expectedEx = mi.GetCustomAttributes(typeof(ExpectedExceptionAttribute), true) .OfType <ExpectedExceptionAttribute>() .FirstOrDefault(); try { object o = Activator.CreateInstance(t); mi.Invoke(o, new object[0]); if (expectedEx != null) { return(new TestResult() { TestName = mi.Name, Message = string.Format("Exception {0} expected", expectedEx.ExpectedException), Type = TestResultType.Fail }); } else { return(new TestResult() { TestName = mi.Name, Message = "ok", Type = TestResultType.Ok }); } } catch (TargetInvocationException tiex) { Exception ex = tiex.InnerException; if (ex is SkipThisTestException) { return(new TestResult() { TestName = mi.Name, Message = "skipped", Type = TestResultType.Skipped }); } if (expectedEx != null && expectedEx.ExpectedException.IsInstanceOfType(ex)) { return(new TestResult() { TestName = mi.Name, Message = "ok", Type = TestResultType.Ok }); } else { return(new TestResult() { TestName = mi.Name, Message = ex.Message, Type = TestResultType.Fail, Exception = ex }); } } }
private TestCaseExpectingExceptionAttribute[] GetExceptionRelatedTestCases(SyntaxList <AttributeListSyntax> attributes, bool isExpectedException, ExpectedExceptionAttribute expectedException) { return(ExpectedExceptionHelper.GetTestCaseAttributeWithExpectedException(attributes, isExpectedException) .Select(x => new TestCaseExpectingExceptionAttribute(x, expectedException)) .ToArray()); }