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

家住魔仙堡 提交于 2019-11-29 03:27:05

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!

After taking a deep dive with reflector into MSTest.exe and further down into the Visual Studio Unit Test stack, I found that the API used by MSTest is sealed up and made private so that i cannot be used from the outside.

Why not using Reflector and seeing how NUnit SimpleTestRunner is running the tests... And then use this technique...

You can make use of the Microsoft REST API's for TFS to run ms tests. Please refer to the documentation here.

I've linked to "Call a Rest API" so that you can see how you'd go about calling one of the REST API's for TFS.

Note that if your tests are linked to the build, they should run automatically every time a build is queued.

Here is the link to Run Functional Tests.

I've also discovered an article on using the TFS SDK API to run tests. Here is that link as well: Link to API Article

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!