简体   繁体   English

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

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

I need to add a Unit Test in an existing Visual Studio solution with 100s of tests.我需要在具有数百个测试的现有 Visual Studio 解决方案中添加单元测试。 I wrote a simple Unit Test only to check if it is working.我写了一个简单的单元测试只是为了检查它是否工作。

My test code is:我的测试代码是:

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

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

The projects used in the Visual Studio solution are of **SDK-Style** . Visual Studio 解决方案中使用的项目是**SDK-Style** Now when I try to run all the tests, I found that some of the tests run and some of test do not run as shown in the screenshot below (including my newly added sample test).现在,当我尝试运行所有测试时,我发现一些测试运行,一些测试没有运行,如下面的屏幕截图所示(包括我新添加的示例测试)。

在此处输入图像描述

I do not understand why some tests are running and some tests do not run at all in Visual Studio's Test Explorer.我不明白为什么在 Visual Studio 的测试资源管理器中有些测试正在运行而有些测试根本没有运行。 All of these tests run fine in the Azure DevOps pipeline .所有这些测试都在 Azure DevOps 管道中运行良好 I even tried to run my sample test individually in Visual Studio by pressing Ctrl + R + T but the test only builds not run.我什至尝试通过按Ctrl + R + T在 Visual Studio 中单独运行我的示例测试,但测试只构建不运行。 But if但是如果

Following are the sample project files of the UnitTest projects which run and those wo do not run.以下是运行和未运行的 UnitTest 项目的示例项目文件。

.csproj of a running Project正在运行的项目的 .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 file of a NOT running 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>

QUESTION: Why only some of the UnitTests are running in Visual Studio's Test Explorer ?问题:为什么只有一些单元测试在Visual Studio 的测试资源管理器中运行? The test which do not run in VS run fine in Azure DevOps pipeline.不在 VS 中运行的测试在 Azure DevOps 管道中运行良好

Comparing both csprojs, I noticed the one which contains the tests that are not running contains the following line:比较两个 csprojs,我注意到包含未运行的测试的包含以下行:

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

I've checked a previous answer and I believed the above line was either in the wrong place or not being used, reason why I've first recommended removing it to validate my assumption.我检查了以前的答案,我认为上面的行要么在错误的地方,要么没有被使用,这就是为什么我首先建议删除它以验证我的假设的原因。 It turns out it was not being used and after removing, it solved your problem.原来它没有被使用,删除后,它解决了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 visual studio 2010 c#单元测试 - visual studio 2010 c# unit tests Visual Studio C#单元测试 - Visual Studio C# Unit Tests C# Visual Studio 单元测试不再在一个存储库中运行,但在另一个存储库中运行:相同的服务应用程序代码 - C# Visual Studio Unit Tests no Longer run in one repo but does in the other: Same Service Application code C#UWP Visual Studio 2017单元测试 - C# UWP Visual Studio 2017 Unit Tests 使用C ++和C#的Visual Studio 2017单元测试以及未发现的C ++测试 - Visual Studio 2017 Unit Tests with C++ and C# with undiscovered C++ tests Visual Studio C#单元测试-报告每次运行的结果 - Visual Studio C# unit testing - report results for each run 如何通过 Visual Studio 2008 在 azure DevOps 中构建 C# 代码? - How to build C# Code in azure DevOps via visual studio 2008? 调试单元测试时,Visual Studio c#不应启动IIS网站 - Visual Studio c# should NOT start IIS website when debugging unit tests 无需在远程计算机上安装Visual Studio,就可以在dll中远程执行C#单元测试 - Execute C# unit Tests in dll remotely without installing Visual Studio on remote machine Visual Studio C#单元测试 - 使用各种/多个测试初始化​​运行单元测试,多次运行相同的单元测试? - Visual Studio C# unit testing - Run Unit test with varied/multiple test initializations, Run same unit test multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM