Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a list of tests with VsTest.Console

How can I run a list of tests using vstest.console.exe? The .vsmdi format offered a way to specify test lists, but that format is deprecated(?).

I can run an explicit list of tests on the command line, which essentially does exactly what I want, but if the number if tests is large (say a few hundred) then I will run out of command line space!

vstest.console MyTests.dll /Tests:Test1,Test2

Is there no way that I can trick vstest.console.exe into running a list of tests defined in any other way?

(edit: emphasis)

Note: I don't want to change the test code, e.g add test category attributes or change naming schemes so name matching will select the subset. I need it to run a list of tests.

The best I can think of is to run as many as I can fit within the max command line length, and repeat until the set is done, then merge. But if there is some way of loading a legacy vsmdi list or similar it would be a lot easier.

vstest.console MyTests.dll < testnames.txt

vstest.console MyTests.dll /Testlist:testnames.txt
like image 902
Anders Forsgren Avatar asked Sep 28 '15 13:09

Anders Forsgren


People also ask

How do I use the VSTest console?

To run automated tests on an ARM architecture-based machine, you must use VSTest. Console.exe. Open Developer Command Prompt to use the command-line tool, or you can find the tool in %Program Files(x86)%\Microsoft Visual Studio\<version>\<edition>\common7\ide\CommonExtensions\<Platform | Microsoft>.

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).

How do I run MSTest from command-line?

MSTest utility. To access the MSTest tool, add the Visual Studio install directory to the path or open the Visual Studio Group from the Start menu, and then open the Tools section to access the Visual Studio command prompt. Use the command MSTest from the command prompt.

Does Visual Studio use MSTest or VSTest?

The Visual Studio Tests build runner integrates functionality of the MSTest framework and VSTest console runner. Support for both frameworks enables TeamCity to execute tests and automatically import their test results.


1 Answers

You can list your tests in a text file of a particular format and then feed that into vstest.console.exe like so. Assume file is called mytests.orderedtest:

vstest.console mytests.orderedtest

The mytests.orderedtest has to be in a specific format. There's an easy way to create such a test from Visual Studio and then you can look at the contents.

First, in Visual Studio, right-click on the project in Solution Explorer, then select Add / Ordered Test. This will create an orderedtest file with a nice UI that you can add your tests. So, pick your tests from the list: Test1,Test2. That will create a file that looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
    <OrderedTest name="mytests" storage="c:\src\MyTests\MyTests.orderedtest" id="ed4d22c5-ab9a-4ebd-954f-65ac4c034338" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <TestLinks>
    <TestLink id="14c6766b-c22b-130a-ddb0-53d5ddd6eb1d" name="Test1" storage="..\bin\debug\MyTests.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <TestLink id="24c6766b-c22b-130a-ddb0-53d5ddd6eb1d" name="Test2" storage="..\bin\debug\MyTests.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </TestLinks>
</OrderedTest>

If you are going to create this manually, outside of Visual Studio, note that the GUID in the id attribute is significant. It is the only way tests with the same name are differentiated across different fully-qualified class names. That is, the id is composed from namespace+class+method. This article explains it: http://blogs.msdn.com/b/aseemb/archive/2013/10/06/how-to-create-an-ordered-test-programmatically.aspx

Here's an routine that will convert the fully qualified method name into one of these GUIDs:

// convert the test (<Name space name>.<class name>.<test method name>) to a GUID
static Guid GuidFromString(string data) 
{
    SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();
    byte[] hash = provider.ComputeHash(System.Text.Encoding.Unicode.GetBytes(data));
    byte[] toGuid = new byte[16]; 
    Array.Copy(hash, toGuid, 16);
    return new Guid(toGuid); 
}
like image 103
zumalifeguard Avatar answered Sep 19 '22 09:09

zumalifeguard