C# 单元测试在 Azure DevOps 中运行,但不在 Visual Studio 中运行

标签 c# azure unit-testing

我需要在现有的 Visual Studio 解决方案中添加包含 100 个测试的单元测试。我编写了一个简单的单元测试只是为了检查它是否正常工作。

我的测试代码是:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Training.UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual(1, 1); 
        }
    }
}

Visual Studio 解决方案中使用的项目属于**SDK-Style**。现在,当我尝试运行所有测试时,我发现有些测试运行,有些测试未运行,如下图所示(包括我新添加的示例测试)。

enter image description here

我不明白为什么在 Visual Studio 的测试资源管理器中有些测试正在运行而有些测试根本不运行。 所有这些测试都在 Azure DevOps 管道中运行良好。我什至尝试通过按 Ctrl + R + T 在 Visual Studio 中单独运行示例测试,但测试仅构建而不运行。但如果

以下是运行的和未运行的 UnitTest 项目的示例项目文件。

正在运行的项目的.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <Import Project="..\..\..\..\_Solution\build.defaults.targets" />
    <PropertyGroup>
        <TargetFramework>net472</TargetFramework>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
        <IsCodedUITest>False</IsCodedUITest>
        <TestProjectType>UnitTest</TestProjectType>
        <OutputPath>$(SolutionDir)SystemTests\$(Configuration)</OutputPath>
        <AssemblyTitle>SystemTest</AssemblyTitle>
        <DebugType>full</DebugType>
        <LangVersion>8</LangVersion>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <Optimize>false</Optimize>
        <DebugSymbols>true</DebugSymbols>
        <AssemblyName>SystemTest.Bus</AssemblyName>
    </PropertyGroup>
    <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
    
    <ItemGroup>
        <Reference Include="PresentationCore" />
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="MSTest.TestAdapter" Version="$(MSTestTestAdapterNugetVersion)" />
      <PackageReference Include="MSTest.TestFramework" Version="$(MSTestTestAdapterNugetVersion)" />
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkNugetVersion)" />
        <PackageReference Include="FluentAssertions" Version="$(FluentAssertionsNugetVersion)" />
    </ItemGroup>
    
  <ItemGroup>
    <ProjectReference Include="..\..\..\UnitTestSetups.csproj" />
    <ProjectReference Include="..\..\Bus.Impl.csproj" />
  </ItemGroup>
  <Target Name="CopyCustomContent" AfterTargets="AfterBuild">
    <Copy SourceFiles="..\..\..\..\_Solution\automation.runsettings" DestinationFolder="$(OutDir)" />
  </Target>
</Project>

未运行项目的.csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">  
  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
  <Import Project="..\..\..\..\_Solution\build.defaults.targets" />

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
    <IsCodedUITest>False</IsCodedUITest>
    <TestProjectType>UnitTest</TestProjectType>
    <OutputPath>$(SolutionDir)\Tests\$(Configuration)</OutputPath>
    <AssemblyTitle>Training.UnitTests</AssemblyTitle>
    <LangVersion>8</LangVersion>
  </PropertyGroup>
  
    <ItemGroup>
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="$(FluentAssertionsNugetVersion)" />
    <PackageReference Include="Moq" Version="$(MoqNugetVersion)" />
    <PackageReference Include="MSTest.TestAdapter" Version="$(MSTestTestAdapterNugetVersion)" />
    <PackageReference Include="MSTest.TestFramework" Version="$(MSTestTestAdapterNugetVersion)" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkNugetVersion)" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Caliburn.Micro" Version="$(VcSimNugetVersion)" />
  </ItemGroup>
  
  <ItemGroup>
    <ProjectReference Include="..\..\Training.csproj" />
  </ItemGroup>
</Project>

问题:为什么只有部分单元测试在 Visual Studio 的测试资源管理器中运行?未在 VS 中运行的测试在 Azure DevOps 管道中运行良好。

最佳答案

比较两个 csprojs,我注意到包含未运行测试的 csprojs 包含以下行:

 <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

我检查了previous answer我相信上面的行要么在错误的位置,要么没有被使用,这就是为什么我首先建议删除它以验证我的假设。原来它没有被使用,删除后,它解决了你的问题。

关于C# 单元测试在 Azure DevOps 中运行,但不在 Visual Studio 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74890484/

相关文章:

unit-testing - 单元测试拦截器(AXOS)

python - 测试 Python 脚本

c# - 如何通过电子邮件获取 ASP.NET MembershipUser

c# - Angular 2 Http 删除 header 以便与 Web API REST 一起使用

c# - Android 应用程序 SQLite 的更新如何进行

azure - 此 Azure 存储帐户容器 REST API 中要传递哪些参数?

azure - 如何在 Azure DevOps 管道任务中传递 Json 变量作为输入

c# - EF 5 迁移无法连接到我们的数据库,即使它在运行时运行良好

azure - Windows HPC 服务器和 Azure

unit-testing - 带有 angulartics2 的单元测试组件 - 无法绑定(bind)到 'angularticsCategory',因为它不是 'div' 的已知属性