Preventing a project from being discovered as a test project
Asked Answered
I

1

12

I have for several project a common util project which consits of some base test classe, helper class and so on. The project definition looks like this:

<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>

I assume that since it references xUnit, that both the visual studio and the dotnet test command treat it like a test project. But since it isn't I would like to somehow prevent both the visual studio and dotnet test to not recognize this project as a test project.

Removing the reference to xUnit is not an option since the base classes needs some interfaces from the xUnit package.

Ingridingrim answered 18/5, 2020 at 7:6 Comment(8)
I don't think that these projects can be discovered without xunit.runner.visualstudio package (at least in Visual Studio) The same about Microsoft.NET.Test.Sdk package, which is required for the runnerVitric
Any Fact or Theory defined in this project is discovered by the Visual Studio and shown in the Test ExplorerIngridingrim
Using Fact means that it's a test, why do you need it for the base classes?Vitric
edit: added a missing target framework @Pavel the only the net461 version is discoverd and both are discovered by dotnet testIngridingrim
I don't I just wanted to point out that the studio can discover the test by just using this project definition. Also the dotnet test tries to execute the project even if there are no testsIngridingrim
@Ingridingrim is correct. By default, dotnet test will pick up any project that has the xunit package installed, regardless whether there is anything in the project.Larson
Have a look at github issues 411 and 1129 for some hints. @Ingridingrim does adding <IsTestProject>true</IsTestProject> help you?Vitric
@PavelAnikhouski adding <IsTestProject>true</IsTestProject> solves the problem with the dotnet test commandIngridingrim
U
15

Based on https://github.com/microsoft/vstest/issues/1129, there seems to be two things it checks for: IsTestProject property and ProjectCapability item of TestContainer. I remove both to be safe.

Add this to the project file:

  <PropertyGroup>
    <IsTestProject>false</IsTestProject>
  </PropertyGroup>

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

This is the error that I was running into that is fixed by adding the above:

========== 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)
Ujiji answered 27/6, 2022 at 17:2 Comment(1)
I was having the same problem, this is apparently undocumented with your answer being the only place on the internet where it's explained. This seems to be a recurring theme with .NET tools...Squires

© 2022 - 2024 — McMap. All rights reserved.