5

I have a test suite of over 10,000 tests and sometimes only want to rerun the tests, that failed on the previous run, using the dotnet vstest CLI.

lauxjpn
  • 4,749
  • 1
  • 20
  • 40

1 Answers1

5

I ended up using the following PowerShell command, to run only the previously failed tests again, based on the newest trx file in .\TestResults\:

dotnet vstest '.\bin\Debug\netcoreapp3.0\MyTests.dll' /Logger:trx /Tests:"$((Select-Xml -Path (gci '.\TestResults\' | sort LastWriteTime | select -last 1).FullName -XPath "//ns:UnitTestResult[@outcome='Failed']/@testName" -Namespace @{"ns"="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"}).Node.Value | % {$_ -replace '^My\.Long\.And\.Tedious\.Namespace\.', ''} | % {$_ -replace '^(.*?)\(.*$','$1'} | Join-String -Separator ','))"

Beware, that there is a character limit on the maximum command line length, that can easily be hit when many tests have previously failed. Use the % {$_ -replace '^My\.Long\.And\.Tedious\.Namespace\.', ''} part, to get rid of namespace prefixes, if you can.

lauxjpn
  • 4,749
  • 1
  • 20
  • 40
  • !!! You just saved me a lot of time !!! The `NET5` command for `xUnit` will look slightly different, for example: ... – Konstantin Konstantinov Jan 21 '21 at 23:04
  • 3
    ```dotnet test .\MyUnitTests\bin\Debug\net5.0\MyUnitTests.dll --logger "trx;LogFileName=MyUnitTests_01.xml" --results-directory .\TestResults --logger "console;verbosity=minimal" --filter ("DisplayName~" + ((Select-Xml -Path .\TestResults\MyUnitTests.xml -XPath "//ns:UnitTestResult[@outcome='Failed']/@testName" -Namespace @{"ns"="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"}).Node.Value | % {$_ -replace '^MyUnitTests\.', ''} | % {$_ -replace '^(.*?)\(.*$','$1'} | Join-String -Separator ' | DisplayName~'))``` – Konstantin Konstantinov Jan 21 '21 at 23:04