Is there a way to run unit tests sequentially with MSTests?

前端 未结 6 1403
走了就别回头了
走了就别回头了 2020-12-09 07:51

I am working in an application that is mostly single-thread, single user. There are a few worker threads here and there, and they are only using thread safe objects and clas

相关标签:
6条回答
  • 2020-12-09 08:14

    I used ordered tests, also configured them easily on jenkins just use command

    MSTest /testcontainer:"orderedtestfilename.orderedtest" /resultsfile:"testresults.trx"

    0 讨论(0)
  • 2020-12-09 08:17

    There is the notion of an "Ordered Test" in which you can list tests in sequence. It is more geared towards ensuring a certain sequential order, but I can't see how that would be possible if B doesn't wait for A to complete.

    Apart from that, it is unfortunate that your tests interfere with each other. There are Setup / TearDown methods that can be used per test such that it may after all be possible to isolate the tests from each other.

    0 讨论(0)
  • 2020-12-09 08:17

    you can Use Playlist

    right click on the test method -> Add to playlist -> New playlist

    you can then specify the execution order enter image description here

    0 讨论(0)
  • 2020-12-09 08:23

    I finally used the ordered test method. It works well.

    However, I had a hell of a time making it work with the NAnt build. Running only the ordered test list in the build requires using the /testmetadata and /testlist switches in the MSTest invocation block. The documentation on these is sketchy, to use a kind description. I google all over for examples of "MSTest /testmetadata /testlist" to no effect.

    The trick is simple, however, and I feel compelled to give it back to the community, in case someone else bumps into the same issue.

    1. Edit the test metadata file (with a .vsmdi extension), and add a new list to the list of tests (the first node in the tree on the left pane. Give it the name you want, for example 'SequentialTests'.
    2. If you used a /testcontainer switch for the MSTest invocation, remove it.
    3. Add a switch for MSTest -> /testmetadata:
    4. Add a switch for MSTEst /testlist:SequentialTests (or whatever name you used)

    Then MSTest runs only the tests listed in the test list you created.

    If someone has a better method, I'd like to hear about it!

    0 讨论(0)
  • 2020-12-09 08:29

    Use an Ordered Test.

    Test > New Test > Ordered Test

    0 讨论(0)
  • 2020-12-09 08:31

    You can specifically require a mutex for each test execution, either in the specific tests you want to serialize, or for all the tests in a class (whatever shares the same mutex string).

    For an entire test class, you can use the TestInitialize and TestCleanup attributes like so:

    private readonly Mutex testMutex = new Mutex(true, "MySpecificTestScenarioUniqueMutexString");
    
    [TestInitialize]
    public void Initialize()
    {
        testMutex.WaitOne(TimeSpan.FromSeconds(1));
    }
    
    [TestCleanup]
    public void Cleanup() {
        testMutex.ReleaseMutex();
    }
    

    To be clear this isn't a feature of tests, ANY locking structure should work. I'm using the system provided Mutexes in this case: https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx

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