NUnit Test Run Order

前端 未结 16 1351
名媛妹妹
名媛妹妹 2020-12-02 11:44

By default nunit tests run alphabetically. Does anyone know of any way to set the execution order? Does an attribute exist for this?

16条回答
  •  死守一世寂寞
    2020-12-02 12:20

    I really like the previous answer.

    I changed it a little to be able to use an attribute to set the order range:

    namespace SmiMobile.Web.Selenium.Tests
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Reflection;
        using NUnit.Framework;
    
        public class OrderedTestAttribute : Attribute
        {
            public int Order { get; set; }
    
    
            public OrderedTestAttribute(int order)
            {
                Order = order;
            }
        }
    
        public class TestStructure
        {
            public Action Test;
        }
    
        class Int
        {
            public int I;
        }
    
        [TestFixture]
        public class ControllingTestOrder
        {
            private static readonly Int MyInt = new Int();
    
            [TestFixtureSetUp]
            public void SetUp()
            {
                MyInt.I = 0;
            }
    
            [OrderedTest(0)]
            public void Test0()
            {
                Console.WriteLine("This is test zero");
                Assert.That(MyInt.I, Is.EqualTo(0));
            }
    
            [OrderedTest(2)]
            public void ATest0()
            {
                Console.WriteLine("This is test two");
                MyInt.I++; Assert.That(MyInt.I, Is.EqualTo(2));
            }
    
    
            [OrderedTest(1)]
            public void BTest0()
            {
                Console.WriteLine("This is test one");
                MyInt.I++; Assert.That(MyInt.I, Is.EqualTo(1));
            }
    
            [OrderedTest(3)]
            public void AAA()
            {
                Console.WriteLine("This is test three");
                MyInt.I++; Assert.That(MyInt.I, Is.EqualTo(3));
            }
    
    
            [TestCaseSource(sourceName: "TestSource")]
            public void MyTest(TestStructure test)
            {
                test.Test();
            }
    
            public IEnumerable TestSource
            {
                get
                {
                    var assembly =Assembly.GetExecutingAssembly();
                    Dictionary> methods = assembly
                        .GetTypes()
                        .SelectMany(x => x.GetMethods())
                        .Where(y => y.GetCustomAttributes().OfType().Any())
                        .GroupBy(z => z.GetCustomAttribute().Order)
                        .ToDictionary(gdc => gdc.Key, gdc => gdc.ToList());
    
                    foreach (var order in methods.Keys.OrderBy(x => x))
                    {
                        foreach (var methodInfo in methods[order])
                        {
                            MethodInfo info = methodInfo;
                            yield return new TestCaseData(
                                new TestStructure
                                    {
                                        Test = () =>
                                            {
                                                object classInstance = Activator.CreateInstance(info.DeclaringType, null);
                                                info.Invoke(classInstance, null);
                                            }
                                    }).SetName(methodInfo.Name);
                        }
                    }
    
                }
            }
        }
    }
    

提交回复
热议问题