Is there an API for running Visual Studio Unit Tests programmatically?

前端 未结 4 987
灰色年华
灰色年华 2020-12-16 14:41

Is there an API for running Visual Studio Unit Tests programmatically?

Running MSTests.exe with Process.Start() does not work in the current scenario. What I\'m look

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 14:49

    You're correct in that there's no public API for the mstest framework. I wrote a manual replacement for mstest one day to see how hard it was, and it's not as simple as it looks (particularly if you want to take advantage of more than one CPU core), so beware of going down this path.

    Personally I've always just run mstest.exe programatically and then parsed the resulting .trx XML file. Are there any particular reasons why you can't use Process.Start to run it?

    P.S. Some of the strange behaviour of mstest.exe are solved if you pass the /noisolation command line parameter - give that a go if you feel so inclined :-)


    Update: Erik mentions he wants to run the test API in the current thread so he can set the thread culture for globalization issues.

    If you run a unit test under the debugger, you'll notice that mstest creates a bunch of threads, and runs all your tests in different threads, so this isn't likely to work even if you could access the API.

    What I'd suggest doing is this:

    1. From your test "runner" application, set an environment variable
    2. Run mstest pointing it at the specific tests
    3. Add a [ClassInitialize] (or [TestInitialize]) method which reads this environment variable and sets the culture
    4. Profit!

提交回复
热议问题