Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 looking for is something like the NUnit SimpleTestRunner.

Any ideas?

/Erik

like image 242
Erik Öjebo Avatar asked Jan 13 '10 07:01

Erik Öjebo


People also ask

How do I automate unit testing in Visual Studio?

To generate unit tests, your types must be public. Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs.

How do I add a unit test to API?

Add unit test project to an existing application To add a unit test project, right-click your solution and select Add and New Project. Select Test in the left pane, and select Unit Test Project for the project type. Name the project StoreApp. Tests.

Can we unit test API?

API Unit Tests Like any other set of tests, API testing follows the Arrange-Act-Assert pattern. However, unlike other tests, you can begin testing your Web Service API without writing any code—with Azure's API Management service, for example you can define your API without writing any code at all.

How do I run all unit tests in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


1 Answers

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!
like image 195
Orion Edwards Avatar answered Oct 13 '22 02:10

Orion Edwards