5

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.

Ackdari
  • 3,222
  • 1
  • 16
  • 33
  • I don't think that these projects can be discovered without [xunit.runner.visualstudio](https://www.nuget.org/packages/xunit.runner.visualstudio) package (at least in Visual Studio) The same about [Microsoft.NET.Test.Sdk](https://www.nuget.org/packages/Microsoft.NET.Test.Sdk/) package, which is required for the runner – Pavel Anikhouski May 18 '20 at 07:13
  • Any `Fact` or `Theory` defined in this project is discovered by the Visual Studio and shown in the Test Explorer – Ackdari May 18 '20 at 07:18
  • 2
    Using `Fact` means that it's a test, why do you need it for the base classes? – Pavel Anikhouski May 18 '20 at 07:19
  • edit: added a missing target framework @Pavel the only the net461 version is discoverd and both are discovered by `dotnet test` – Ackdari May 18 '20 at 07:20
  • 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 tests – Ackdari May 18 '20 at 07:22
  • @Ackdari 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. – Kevin Brydon May 18 '20 at 07:29
  • 1
    Have a look at github issues [411](https://github.com/Microsoft/vstest/issues/411) and [1129](https://github.com/microsoft/vstest/issues/1129) for some hints. @Ackdari does adding `true` help you? – Pavel Anikhouski May 18 '20 at 07:37
  • 1
    @PavelAnikhouski adding `true` solves the problem with the `dotnet test` command – Ackdari May 18 '20 at 08:10

1 Answers1

6

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)
Sean Hall
  • 7,629
  • 2
  • 29
  • 44