Search code examples
c#unit-testingvisual-studio-codexunitproject-reference

C# Project Reference Warning Type confilict


I have a main asp.net core project which is called "API" and inside of it I have another project called "API.Tests" for xunit tests. So when I'm referencing main project (<ProjectReference Include="..\API.csproj" />), to get access to main's project classes, I'm also referencing everything that's inside of a "API.Tests" project since it's inside of a main project. That's why this warning appears when I'm trying to inherit one class from another in test project:warning

This is API.csproj file:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <GenerateProgramFile>false</GenerateProgramFile>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

This is API.Tests.csproj file:

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

  <PropertyGroup>
    <Nullable>enable</Nullable>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    <IsPackable>false</IsPackable>
    <TargetFramework>net6.0</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.1.2"></PackageReference>
    <ProjectReference Include="..\API.csproj" />
  </ItemGroup>

</Project>

Please help, I don't know how to fix this.


Solution

  • The only thing you can and should do is put the production code in a sub-project as well and reference that sub-project from API.Tests instead of the main project.

    i.e.

    API
       \API.Application
       \API.Tests
    

    with API.Tests referencing API.Application.