How to add a unit test in Azure DevOps pipeline

Kevin YANG picture Kevin YANG · Dec 26, 2019 · Viewed 7.1k times · Source

The build runs well but can not see the unit test result When added a build pipeline which used a classic edit or for my solution.

And the code coverage also blank. I have two projects and tests project in the solution. The unit test task returns the result as below:

    A total of 14 test files matched the specified pattern.
           No test is available in d:\a\1\s\src.net... 
Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
            Results File: d:\a\_temp\TestResults\VssAdministrator_fv-az686_2019-12-26_06_10_28.trx
            Attachments:
              d:\a\_temp\TestResults\d301a26b-d99f-4b4e-bbe8-c95e588ee1c5\VssAdministrator_fv-az686 2019-12-26 06_10_20.coverage
            Vstest.console.exe exited with code 0.
            **************** Completed test execution *********************
            Test results files: d:\a\_temp\TestResults\VssAdministrator_fv-az686_2019-12-26_06_10_28.trx
            No Result Found to Publish 'd:\a\_temp\TestResults\VssAdministrator_fv-az686_2019-12-26_06_10_28.trx'.
            Created test run: 1006840
            Publishing test results: 0
            Publishing test results to test run '1006840'.
            TestResults To Publish 0, Test run id:1006840
            Published test results: 0
            Publishing Attachments: 2
            Completed TestExecution Model...

And also I can not see anymore from the Tests windows.

The test YAML script as below:

steps:
- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*test*.dll
     !**\obj\**
    codeCoverageEnabled: true
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

Answer

Merlin Liang picture Merlin Liang · Dec 26, 2019

There no any problem on your YAML, the same definition is all work for me.

A total of 14 test files matched the specified pattern.

No test is available in d:\a\1\s\src.net...

Based on these error message, the test files has been identified out from the folder which means the dlls has exists now. But it still say there's no test available, this is more relative with vstest.console.exe did not identify the test.

For example, if your test project type is NUnit, you could use one extension names NUnit test adapter to support this test type running in visual studio.

But, when the project is published into VSTS, this extension could not be published also, and this extension does not exists in VSTS hosted agent. Without this extension support, the vstest.console.exe could not identify the Nunit test, then it will prompt the message like No test is available in xxxxxxx. See this as refer to.

But, you can installed nuget packages to replace the role of that VS extension.

If it's type is Nunit test, you must ensure add the reference on packages NUnit and NUnit3TestAdapter in your csproj file:

<PackageReference Include="NUnit">
      <Version>3.12.0</Version>
    </PackageReference>
<PackageReference Include="NUnit3TestAdapter">
      <Version>3.15.1</Version>
    </PackageReference>

Similar method tp xUnit test.