【问题标题】:Azure Devops UnitTest not finding ".deps.json" fileAzure Devops UnitTest 未找到“.deps.json”文件
【发布时间】:2020-07-08 13:11:28
【问题描述】:

我创建了一个 asp.net core webapi 项目并为它创建了一个对应的 xUnitTest。当我在本地机器上运行 UnitTest 时,UnitTest 运行没有任何问题。 我将 XUnit 与它的 visualstudio 运行器一起使用。 以下是我的 UnitTest 项目的参考资料:

<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" />

当我现在通过 Azure Devops 运行单元测试时,它们运行成功并给我以下输出:

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;

我遇到的问题是,后来我得到一个异常,“.deps.json”文件没有找到

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

然后我检查是否可以搜索二进制文件夹,但没有找到测试项目。 我还尝试使用“DotNetCoreCLI@2”任务和“VSTest@2”任务,都产生了同样的问题。 这是我使用的 yaml 文件:

# 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

两个测试任务都将运行测试然后“失败”,因为找不到“.deps.json”文件。

【问题讨论】:

    标签: asp.net-core .net-core azure-devops azure-pipelines xunit


    【解决方案1】:

    除了用!**\obj\** 忽略obj 文件夹外,从.NET 5 开始,我们还必须忽略bin 文件夹内的ref 文件夹; !**\bin\**\ref\**。不忽略这一点会导致找到 2 个与 UnitTests.dll 匹配的文件,并且 ref 文件夹中的 dll 将失败并出现错误:UnitTests.deps.json 文件未找到

    What is this ref-folder?

    现在我们的测试任务如下所示:

    - task: VSTest@2
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: |
          **\*UnitTests.dll
          !**\*TestAdapter.dll
          !**\obj\**
          !**\bin\**\ref\**
        searchFolder: '$(System.DefaultWorkingDirectory)'
        runTestsInIsolation: true
        codeCoverageEnabled: true
    

    【讨论】:

    • 当我看到你提到 .Net 5.0 时,我知道我来对了地方。是的,忽略 \ref\ 目录,一切都很好。泰!
    • 有没有办法为testSelector: testPlan 排除testhost.dll 而不是testSelector:testAssemblies
    • 我建议的最佳选择是不为 Test 项目生成 ref 程序集,因为它确实不需要。我们可以将以下设置添加到 Test.csproj false
    【解决方案2】:

    这个问题有两个问题:

    1. 找到的单元测试位于“/obj/release”文件夹中,但没有将 *.deps.json 文件复制到此文件夹中。在 build-task 中,您可以看到 *.deps.json 已成功构建和复制:

    从 “d:\a\1\s\APPNAME\bin\Release\netcoreapp3.1\APPNAME.deps.json”到 "d:\a\1\s..."

    1. 我遇到的第二个问题是,单元测试中的引用引用了 .NET Standard 2.1 解决方案,该解决方案似乎没有在输出中包含任何 *.deps.json。

    这是我的固定 yaml 文件:

    # 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
    

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 2020-05-26
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多