Run TestFixtures in Order with NUnit

2019-07-10 02:20发布

问题:

I need to have ordered test fixtures in my NUnit c# Application. I have an example on how to run ordered test methods from this page, and I've tried to implement the same logic for test fixtures with the same methods provided in the example application. In our application a test fixture is separated for each class and each test fixture has one test method. Our latest attempt was to use a Parent Test Fixture which inherits from a class called: OrderedTestFixture (the same as in the example), which has the following method:

public IEnumerable<NUnit.Framework.TestCaseData> TestSource
{
    get
    {
        var assembly = Assembly.GetExecutingAssembly();

        foreach (var order in methods.Keys.OrderBy(x => x))
        {
            foreach (var methodInfo in methods[order])
            {
                MethodInfo info = methodInfo;
                yield return new NUnit.Framework.TestCaseData(
                    new TestStructure
                    {
                        Test = () =>
                        {
                            object classInstance = Activator.CreateInstance(info.DeclaringType, null);
                            info.Invoke(classInstance, null);
                        }
                    }).SetName(methodInfo.Name);
            }
        }
    }
}

This method is supposed to return, in order, the test methods that will execute. However, even if it returns the test methods in order, it fails to execute them in order.

I'm using the same exact logic as in the App example. An orderedTestAttrribute class that inherits from Attribute that will be placed in every test method like this:

[Test]
[OrderedTest(1)]
[BeforeAfterTest]
public void TestMethod() { }

Does anyone out there have any idea how can I make this work without changing my current implementation of having one testFixture with one test class separately?

回答1:

Ok, so if I understand you correctly, you want to order test across multiple TestFixtures. So in this case, you don't want to use the OrderedTestFixture because that was specifically created to NOT run test across multiple fixtures. If you look at my GitHub repo from the earlier question, you'll want to follow the Example 2 code. Note that in this case you should only use the OrderedTest attribute - using Test will throw the whole thing off because then it will just be randomly scheduled by NUnit instead of being ordered through the use of TestCaseData. Refer back to my blog post for more details.

Hope that helps.