0
votes

So I have a .NET Core 3.1 project. I am trying to create a unit testing project to go along with it. I have created a .NET Standard 2.1 Class Library for this purpose. I'm then trying to add a reference to the main project, in order to be able to reference code from there. This isn't working, as I get the following compilation errors:

'..\MyProj\MyProj.csproj' targets 'netcoreapp3.1'. It cannot be referenced by a project that targets '.NETStandard,Version=v2.1'. Project MyProj is not compatible with netstandard2.1 (.NETStandard,Version=v2.1). Project MyProj supports: netcoreapp3.1 (.NETCoreApp,Version=v3.1) Test (test\Test)

So this does make sense to me, as I understand why .NET Core wouldn't be able to reference a .NET Standard project.
But then how can I create a Unit Testing project? Is it possible to create a .NET Core 3.1 Class Library? From everything I have read, it seems like that doesn't exist.

How can I set up a unit test project that references the main project?

2
You should not do that. Please check this -> stackoverflow.com/questions/59444690/…panoskarajohn
I don't have any issue creating a .NET Core 3.1 Class library. What problem are you running into?Jonathon Chase
@JonathonChase - I do not have any option to create a .NET Core 3.1 class library, only a .NET standard one.Sean
@panoskarajohn - yes, I am aware of why it should not be possible. However, I do not then have a solution of how to set up a unit testing project.Sean
@panoskarajohn hmmm so I can find tutorials like this: tutorialspoint.com/dotnet_core/… that appear to show that there should be an option in project creation for a .NET Core Class Library. However I don't have that option in my project templates, which is super weird!Sean

2 Answers

4
votes

.NET Standard was created as an interface for different .NET implementations. For example, this allows you to share code between .NET Core and the full .NET framework. I would create your library as a .NET Standard 2.0 project and then you can reference it in your .NET Core 3.1 application and your unit testing project.

I only suggest .NET Standard as it is generally more portable than .NET Framework or .NET Core libraries. You'd be able to target more platforms such as Xamarin, desktop, or web with .NET Standard.

0
votes

I think you just need to use .Net Core 3.1 in your project, not NetStandard. I use it this way and there is no troubles. I guess you don't really need your unit testing to be portable?