5

I have a solution with a NUnit testproject (Foo.Test).

+-- src
|   +-- Foo.Gui
|   +-- Foo.Test
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="TestResults\**" />
    <EmbeddedResource Remove="TestResults\**" />
    <None Remove="TestResults\**" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GitVersionTask" Version="5.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="nunit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Foo.Gui\Foo.Gui.csproj" />
  </ItemGroup>

</Project>

The following call is used for testing:

dotnet test --no-build -c Release -v n src\%TESTPROJECT%\%TESTPROJECT%.csproj --logger "trx;LogFileName=testreport.trx"

If I run it local, everything is working as expected:

enter image description here

The same call on build server is not finding any test:

enter image description here

gitlab-ci.yml

test:solution:
    stage: test
    script:
        - dotnet test --no-build -c Release -v n src\$env:TESTPROJECT\$env:TESTPROJECT.csproj --logger "trx;LogFileName=testreport.trx"
    after_script:
        - trx2junit src\$env:TESTPROJECT\Testresults\testreport.trx 
    artifacts:
        reports:
            junit: src\$env:TESTPROJECT\Testresults\testreport.xml

Someone got a hint what can be wrong? The Testproject (Foo.Test.dll) is available and present in the build artifacts.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • Is the NUnit3 adapter present in the output directory? – Charlie Nov 22 '19 at 17:46
  • 1
    Yes it is. Actually I think I foung the answer. I had to add an additional `dotnet restore` command on the build pipeline before testing, to restore the `project.assets.json`. Then everything works as expected – Dominic Jonas Nov 25 '19 at 07:24

1 Answers1

5

I had to add an additional dotnet restore command on the build pipeline before testing, to restore the project.assets.json. Now everything is working as expected

test:solution:
    stage: test
    script:
        - dotnet restore src
        - dotnet test --no-build -c Release -v n src\$env:TESTPROJECT\$env:TESTPROJECT.csproj --logger "trx;LogFileName=testreport.trx"
    after_script:
        - trx2junit src\$env:TESTPROJECT\Testresults\testreport.trx 
    artifacts:
        reports:
            junit: src\$env:TESTPROJECT\Testresults\testreport.xml

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • Any idea why? I feel like this is a bug with dotnet test. I had GitLab add both the bin and obj folders of both my project and test project (so it includes project.assets.json of both) to the artifacts and _still_ had to do a dotnet restore before the tests could be discovered. – Coda17 Feb 24 '20 at 19:52
  • 1
    I honestly did not investigate the problem further. Now that this simple solution always works, the problem was solved for me. – Dominic Jonas Feb 25 '20 at 07:37
  • I suspect to make sure the unit test adapter is available. You can also install test adapters globally to ensure they're always available. – jessehouwing Mar 04 '23 at 16:25
  • @jessehouwing Could you tell us more about how to install the adapter globally? – Dominic Jonas Mar 06 '23 at 07:19
  • 1
    You can store the test adapter anywhere and then pass the path to your test adapters to `dotnet test`. Add: `"-p:VSTestTestAdapterPath=/path/to/test adapters/"`, the path you pass in will be searched recursively. – jessehouwing Mar 06 '23 at 08:43