Skip to content

SetUp/TearDown not called recursively for PropertyAttribute #457

@simendsjo

Description

@simendsjo

When using PropertyAttribute, I would expect all SetUpAttribute and TearDownAttribute to be run as would be the case for TestAttribute.

We have an attribute with a similar use-case, and does the following:

                    // We need to manually clean up the test before trying again.
                    // I was unable to reuse existing NUnit functionality for this.
                    //
                    // See also NUnits SimpleWorkItem and RetryCommand
                    TestFixture fixture;
                    {
                        var p = context.CurrentTest.Parent;
                        while (!(p is TestFixture)) p = p.Parent;
                        fixture = (TestFixture) p;
                    }

                    var teardownMethods = fixture.TearDownMethods.OrderByDescending(GetTypeDepth);
                    foreach (var m in teardownMethods)
                    {
                        try
                        {
                            m.Invoke(context.TestObject, new object[] { });
                        }
                        catch
                        {
                            // Has teardown already completed?
                            // We just ignore all errors and assume a full setup will solve all problems
                        }
                    }

                    var setupMethods = fixture.SetUpMethods.OrderBy(GetTypeDepth);
                    foreach (var m in setupMethods)
                    {
                        m.Invoke(context.TestObject, new object[] { });
                    }

where

        private static int GetTypeDepth(MethodInfo x)
        {
            var d = 0;
            for (var ty = x.DeclaringType; ty != null; ty = ty.BaseType)
            {
                ++d;
            }

            return d;
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions