簡體   English   中英

C# 單元測試在 Azure DevOps 中運行,但不在 Visual Studio 中運行

[英]C# Unit Tests run in Azure DevOps but not in Visual Studio

我需要在具有數百個測試的現有 Visual Studio 解決方案中添加單元測試。 我寫了一個簡單的單元測試只是為了檢查它是否工作。

我的測試代碼是:

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** 現在,當我嘗試運行所有測試時,我發現一些測試運行,一些測試沒有運行,如下面的屏幕截圖所示(包括我新添加的示例測試)。

在此處輸入圖像描述

我不明白為什么在 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,我注意到包含未運行的測試的包含以下行:

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

我檢查了以前的答案,我認為上面的行要么在錯誤的地方,要么沒有被使用,這就是為什么我首先建議刪除它以驗證我的假設的原因。 原來它沒有被使用,刪除后,它解決了你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM