Skip to content

Handle ExpectedExceptionAttribute #2

@busterwood

Description

@busterwood

Add to Test

        public Type ExpectedException => (Type) attrsByName["ExpectedExceptionAttribute"].FirstOrDefault()?.ConstructorArguments?.First().Value;
...


 public static IEnumerable<Test> Tests(MethodInfo method)
        {
            var attrsByName = method.CustomAttributes.ToLookup(a => a.AttributeType.Name);
            
            // check test cases first just in case someone adds the TestAttribute as well
            if (attrsByName["TestCaseAttribute"].Any())
            {
                foreach (var testCase in attrsByName["TestCaseAttribute"])
                {
                    yield return new TestCase(method, attrsByName, testCase);
                }

            }
            else if (attrsByName.IsTest())
            {
                yield return new Test(method, attrsByName);
            }
        }

Add to TestRunner RunTestMethod:

            catch (Exception ex) when (ex.GetType() == test.ExpectedException)
            {
                Pass();
            }
            catch (TargetInvocationException ex) when (ex.InnerException.GetType() == test.ExpectedException)
            {
                Pass();
            }

Add to TestRunner WaitForAsyncTestToComplete:

            catch (AggregateException ex) when (ex.InnerException.GetType() == test.ExpectedException)
            {
                Pass();
            }
            catch (AggregateException ex) when (ex.InnerException is TargetInvocationException eio && eio.InnerException.GetType() == test.ExpectedException)
            {
                Pass();
            }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions