NUnit Test Run Order

前端 未结 16 1280
名媛妹妹
名媛妹妹 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:28

    I'm suprised the NUnit community hasn't come up with anything, so I went to create something like this myself.

    I'm currently developing an open-source library that allows you to order your tests with NUnit. You can order test fixtures and ordering "ordered test specifications" alike.

    The library offers the following features:

    • Build complex test ordering hierarchies
    • Skip subsequent tests if an test in order fails
    • Order your test methods by dependency instead of integer order
    • Supports usage side-by-side with unordered tests. Unordered tests are executed first.

    The library is actually inspired in how MSTest does test ordering with .orderedtest files. Please look at an example below.

    [OrderedTestFixture]
    public sealed class MyOrderedTestFixture : TestOrderingSpecification {
        protected override void DefineTestOrdering() {
            TestFixture<Fixture1>();
    
            OrderedTestSpecification<MyOtherOrderedTestFixture>();
    
            TestFixture<Fixture2>();
            TestFixture<Fixture3>();
        }
    
        protected override bool ContinueOnError => false; // Or true, if you want to continue even if a child test fails
    }
    
    0 讨论(0)
  • 2020-12-02 12:29

    Usually Unit Test should be independent, but if you must, then you can name your methods in alphabetical order ex:

    [Test]
    public void Add_Users(){}
    
    [Test]
    public void Add_UsersB(){}
    
    [Test]
    public void Process_Users(){}
    

    or you can do..

            private void Add_Users(){}
    
            private void Add_UsersB(){}
    
            [Test]
            public void Process_Users()
            {
               Add_Users();
               Add_UsersB();
               // more code
            }
    
    0 讨论(0)
  • 2020-12-02 12:33

    NUnit 3.2.0 added an OrderAttribute, see:

    https://github.com/nunit/docs/wiki/Order-Attribute

    Example:

    public class MyFixture
    {
        [Test, Order(1)]
        public void TestA() { ... }
    
    
        [Test, Order(2)]
        public void TestB() { ... }
    
        [Test]
        public void TestC() { ... }
    }
    
    0 讨论(0)
  • 2020-12-02 12:34

    Your unit tests should each be able to run independently and stand alone. If they satisfy this criterion then the order does not matter.

    There are occasions however where you will want to run certain tests first. A typical example is in a Continuous Integration situation where some tests are longer running than others. We use the category attribute so that we can run the tests which use mocking ahead of the tests which use the database.

    i.e. put this at the start of your quick tests

    [Category("QuickTests")]
    

    Where you have tests which are dependant on certain environmental conditions, consider the TestFixtureSetUp and TestFixtureTearDown attributes, which allow you to mark methods to be executed before and after your tests.

    0 讨论(0)
提交回复
热议问题