防止项目被发现是测试项目

5

我有多个项目都共用一个util项目,其中包括一些基础测试类、帮助类等。

该项目的定义如下:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
    <RootNamespace>SomeRoot.Util</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="5.10.2" />
    <PackageReference Include="xunit" Version="2.4.1" />
  </ItemGroup>
</Project>

我假设既然它引用了xUnit,那么Visual Studio和dotnet test命令都将像测试项目一样处理它。

但由于它不是测试项目,我希望以某种方式防止Visual Studio和dotnet test将此项目识别为测试项目。

删除对xUnit的引用不是选择,因为基础类需要一些来自xUnit包的接口。


此项目中定义的任何“事实”或“理论”都将被Visual Studio发现并显示在测试资源管理器中。 - Ackdari
2
使用Fact意味着这是一个测试,为什么需要在基类中使用它? - Pavel Anikhouski
我只是想指出,工作室可以通过使用此项目定义来发现测试。另外,即使没有测试,dotnet test也会尝试执行该项目。 - Ackdari
1
请查看 GitHub 的问题4111129以获取一些提示。@Ackdari,添加<IsTestProject>true</IsTestProject>是否有帮助? - Pavel Anikhouski
1
@PavelAnikhouski 添加 <IsTestProject>true</IsTestProject> 可以解决使用 dotnet test 命令时的问题。 - Ackdari
显示剩余3条评论
1个回答

8
根据https://github.com/microsoft/vstest/issues/1129,似乎有两个检查项:IsTestProject属性和TestContainerProjectCapability项目。为了保险起见,我将两者都删除。
将此添加到项目文件中:
  <PropertyGroup>
    <IsTestProject>false</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <ProjectCapability Remove="TestContainer" />
  </ItemGroup>

这是我遇到的错误,通过添加上述内容已经解决了:
========== Starting test discovery ==========
Test project {ProjectName} does not reference any .NET NuGet adapter. Test discovery or execution might not work for this project.
It's recommended to reference NuGet test adapters in each test project in the solution.
Loaded 0 test records from the solution cache in 0.220 sec.
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process exited with error: Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'Microsoft.TestPlatform.CoreUtilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at Microsoft.VisualStudio.TestPlatform.TestHost.Program.Main(String[] args)
. Please check the diagnostic logs for more information.
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.ThrowOnTestHostExited(Boolean testHostExited)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings)
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.DiscoverTests(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接