11

I created a .Net 6 class library and now I am trying to create a test project.
a .net 6 test project is not available. Online I found the information to use .Net Framework or .Net core in order to test.

I created a .Net Framework 4.8 test project an referenced my class library.

I receive the Compiler error:

Project '..\CircularList\CircularList.csproj' targets 'net6.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.8'. UnitTests

How do I do Unit tests then? Is there any way to target .Net 6.0 from .Net Framework 4.8?

julian bechtold
  • 1,875
  • 2
  • 19
  • 49
  • 1
    have you tried the "xunit test project" or "nunit test project" templates? both offer .NET 6 on my installation; and no: you can't cross-reference between .NET 6 and .NET Framework 4.8 (in either direction) – Marc Gravell Jan 30 '22 at 21:26
  • 3
    Well - **.NET 4.8** is the "old", legacy/full/Windows-only .NET framework - **.NET 6** is the new, cross-platform .NET Core based framework - those are very different and you cannot mix those. But there should **definitely** be unit test projects for .NET Core in VS - search for "MSTest Test Project" - which clearly states *A project that contains unit tests that can run on .NET Core ...* - and this **does** support .NET 6 – marc_s Jan 30 '22 at 21:26
  • If you're using Visual Studios, you might want to update to 2022 to make this easier. I expect that version comes with everything you need to easily create .net 6 projects and test projects. You'll need a .net 6 test project. – Nathan Cooper Jan 30 '22 at 21:30
  • 1
    Did you click on the old MSTest one? The one that said .NET Framework? Click on the XUnit instead and it will give you an more modern choice of framework. – Nathan Cooper Jan 30 '22 at 21:40

6 Answers6

8

None of the previous answers worked for me. I was trying to add a Test project for my Windows Forms (.NET 6.0) project. Tried all of the available project templates, and none worked.

Just needed to modify the test project .csproj (by double clicking the Test Project), and change TargetFramework from net6.0 to net6.0-windows.

3

I guess that must have been the answer. Don't use the project template "Unit Test Project (.NET Framework)" if you want to use test a .net6 library.

Use a more up-to-date template in the project creation wizard. There are newer MSTest ones, but one could also take advantage of better templates like the xUnit one.

Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75
2

This is A solution, not likely THE only solution:

I also have a .NET 6.0 project that I would like to test.

With the project template picker, I picked the C# NUnit Test for .NET Core. When advancing to next screen, there was a dropdown that allowed me to pick a Target framework. .NET 6.0 was the default option.

1

I created a NET 6.0 class library and received the same message.

I went into properties, just to double check that my project and my test project were set the same and noticed that the Target OS was not set in my library.

It was in my project, but not in my class library.

Once I changed my class library "Target OS" - to be windows, the same as my project, the error messages went away.

Kennetic
  • 83
  • 8
0

None of the previous answers worked for me. In the beginning, I think .Net framework missing, so I install the installer and install, but it doesn't work. I also try to change the project "Target OS" to "Windows", it doesn't work. I find the Test Project property target is still .Net 4.7.2, the UI is a old version, not like the new version.

Then I remove the Test Project created from the template, and right-click the method to test and click "Create Unit Tests", then create the Test Project by the pop-up window. It works. And the new Test Project works successfully, its property target is .Net 6, and the UI is the new version.

Jun Yu
  • 375
  • 1
  • 5
  • 21
0

In my Unit test project, I was getting this error even though .Net 6.0 was installed because Unit test project version tag was TargetFrameworkVersion with version V6.0

Updating tag to TargetFramework with version net6.0 solved the issue.