[英]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.