14

I created an asp.net core webapi project and created an coresponding xUnitTest for it. When I am running the UnitTest on my local maschine the UnitTest runs without any problems. I use the XUnit with its visualstudio runner. Here are the references of my UnitTest project:

<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />

When I now run the unittest via Azure Devops, they run successfully and give me the following output:

Created test run: 156
Publishing test results: 35
Publishing test results to test run '156'.
TestResults To Publish 35, Test run id:156
Test results publishing 35, remaining: 0. Test run id: 156
Published test results: 35
Publishing Attachments: 2
Failed tests: 0; Total tests: 35;

The problem I have is, that later I get an exception, that the ".deps.json" file was not found:

Unable to find d:\a\1\s\<project-path>\obj\Release\netcoreapp3.1\<project>.deps.json. Make sure test project has a nuget reference of package "Microsoft.NET.Test.Sdk".

I then checked if I can search for the binary folder, but then no test project is found. I also tried using the "DotNetCoreCLI@2" task and the "VSTest@2" task, both produce the same problem. Here is the yaml file I use:

# UnitTests
- task: VSBuild@1
    inputs:
    solution: '**/<projectnamespace>.UnitTests.csproj'
    vsVersion: 16.0  #also tried without this line
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: DotNetCoreCLI@2
    inputs:
    command: test
    projects: '**UnitTests.dll'
    arguments: '--configuration $(buildConfiguration)'

- task: VSTest@2
    inputs:
    testSelector: testAssemblies
    testAssemblyVer2: '**bin\**UnitTests*.dll'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    diagnosticsEnabled: true
    codeCoverageEnabled: true

Both test-task will run the test and then are "failed" because the ".deps.json" file was not found.

Celdus
  • 1,010
  • 1
  • 14
  • 25

2 Answers2

15

In addition to ignore the obj folder with !**\obj\**, from .NET 5 we also had to ignore a ref folder inside the bin folder; !**\bin\**\ref\**. Not ignoring this caused 2 files matching UnitTests.dll to be found, and the dll from the ref folder would fail with the error: UnitTests.deps.json file was not found

What is this ref-folder?

Now our test-task looks like this:

- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*UnitTests.dll
      !**\*TestAdapter.dll
      !**\obj\**
      !**\bin\**\ref\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    runTestsInIsolation: true
    codeCoverageEnabled: true
DanMusk
  • 399
  • 2
  • 10
  • 1
    When I saw you mention .Net 5.0 I knew I was in the correct spot. Yes, ignore the \ref\ dir and all was well. TY! – Larry Smith Mar 24 '21 at 20:42
  • Is there a way to exclude `testhost.dll` for `testSelector: testPlan` instead of `testSelector:testAssemblies`? – Denis S. May 25 '21 at 09:11
  • 4
    The best option I would suggest is to not produce ref assemblies for Test project as it is really not require. We can add below setting to Test.csproj false – Mahesh Bansode Jun 09 '21 at 09:34
1

There were two issues with this Problem:

  1. The found unit test was located in the "/obj/release" folder, but into this folder no *.deps.json files are copied. In the build-task you can see that the *.deps.json were successfully build and copied:

Copying file from "d:\a\1\s\APPNAME\bin\Release\netcoreapp3.1\APPNAME.deps.json" to "d:\a\1\s..."

  1. The second issue I had, was that a reference in the unit test was referencing a .NET Standard 2.1 Solution, which seems not to include any *.deps.json in the output.

Here is my fixed yaml file:

# UnitTests
- task: VSBuild@1
  displayName: 'Build Test'
  inputs:
    solution: '**/APPNAME.UnitTests.csproj'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    testSelector: testAssemblies
    testAssemblyVer2: 'APPNAME\bin\Release\netcoreapp3.1\APPNAME.UnitTests.dll' #Full path to test dll
    testFiltercriteria: FullyQualifiedName~APPNAME.UnitTests # Further namespace filter
    runInParallel: false
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    diagnosticsEnabled: true
Celdus
  • 1,010
  • 1
  • 14
  • 25