'MSTest unit test project added to C# solution causes existing project build to fail VS2022

Im using VS2022 to create a .NET Core 3.1 C# class library (DeletemeClassLib.csproj) with no added content. Solution and Project are in the same folder. This builds as expected.

When I add a MSTest project to the existing solution using .NET Core 3.1, the MSTest project will build without errors, but the DeletemeClassLib.csproj build fails with errors like:

Error CS0246 The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) DeletemeClassLib C:\Users...\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs 5 Active

The build errors appear to point to my UnitTest1.cs file (in the MSUnitTest.csproj project), but they only occur when building the separate DeletemClassLib.cs project. I also tried the same thing using nunit instead of MSTest, but I found similar results.

My DeletemeClassLib.csproj has no packages listed under the project. My MSUnitTest1 project lists coverlet.collector 1.2.0, Microsoft.NET.Test.Sdk 16.5.0, MSTest.TestAdapter 2.1.0, and MSTest.TestFramework 2.1.0.

DeletemeClassLib.csproj

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
  </PropertyGroup>

</Project>

MSUnitTests1.csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

</Project>

Class1.cs (main project file in DeletemeClassLib.cs project): using System;

namespace DeletemeClassLib
{
    public class Class1
    {
    }
}

MSUnitTest1.cs (Main MSTest unit test file):

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MSUnitTests1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

Build Output with username hidden:

1>------ Build started: Project: DeletemeClassLib, Configuration: Debug Any CPU ------ 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(14,12,14,54): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(15,12,15,60): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(16,12,16,58): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(17,12,17,67): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(18,12,18,54): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(19,12,19,52): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(20,12,20,54): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(1,17,1,29): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) 1>Done building project "DeletemeClassLib.csproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

Any ideas on what might cause the DeletemeClassLib.csproj build to fail when the MSUnitTest1.csproj is present?

If I move MSUnitTest1.csproj to a completely separate folder, everything builds correctly. However, I am trying to retrofit an older Github solution (this example is a simple representation of the other project) to add unit tests, and I can't add a unit test folder above the git parent to separate the unit tests and main project into separate folders as I would like to.

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source