nunit / nunit

NUnit Framework
https://nunit.org/
MIT License
2.51k stars 730 forks source link

The type or namespace name 'NUnit' could not be found - when compiling with netstandard2.0 #4800

Closed BananaSupreme closed 1 month ago

BananaSupreme commented 1 month ago

Hi!

It is my understanding that .Net standard cannot be used for running tests, but I am trying to just compile a file using NUnit on .Net Standard 2.0, the runner is in actuality running on .Net 8 Also of note, this only affects NUnit version 4, version 3, works fine.

That is the type of error I am getting, this also applies to the SetUpAttribute and Assert (for obvious reasons) CS0246 The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?) ClassLibrary1 ...\Class1.cs

I tried, clearing all the caches, bin, rebuild, restart, minimal repro, using another computer, VS or Rider. The only thing that did work is referencing the DLL directly rather than using Nuget management. I tried looking around but most answers seem to be focused on using the runner on standard rather than simple compilation.

I was hoping that someone here might have some solution or guidance for me, thank you so much for your time and effort maintaining this great project!

I am attaching a minimal repro, that fails. NUnit-minimal repro.zip

manfred-brands commented 1 month ago

As you said we can only run test on a real target. Are you writing an extension library with NUnit functions? If so you can multi-target your library by specifying more than one Target framework.

Netstandard is false promise. There are methods defined in there which compile for .net core, but then throw a PlatformNotSupported exception.

OsirisTerje commented 1 month ago

@BananaSupreme Your repro doesn't make sense. Only one csproj there. It targets net 8, but no adapter and no sdk, so this will not run tests. But it compiles as is.

BananaSupreme commented 1 month ago

Hi,

Thank you for your response!

We were sticking a base test file with some common utils for one of our projects, but because it is compiled to standard, and also needs to be, I was hoping there was someway to make this work.

@OsirisTerje Oh, I am very sorry, I kept changing the versions for demonstrations and must have left it on net8.0 by accident, do you mind seeing if it still compiles once it targets netstandard2.0?

OsirisTerje commented 1 month ago

Your code references NUnit, so that will not work. If this is meant to be a test project, then it needs reference to MS Test SDK and normally the NUnit3TestAdapter to make it runnable in dotnet test and in VS. And of course target .net 6 or higher.

Your classlibrary for the Software Under Test however, that can be in netstandard.

BananaSupreme commented 1 month ago

I was hoping I could keep it a class library and put some helper tests for NUnit, but I understand this will not work, thank you for your quick reply, we'll come up with a solution!

manfred-brands commented 1 month ago

I was hoping I could keep it a class library and put some helper tests for NUnit, but I understand this will not work, thank you for your quick reply, we'll come up with a solution!

You should ideally separate test code from production code and create two libraries.