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?
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.
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.
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.
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.
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.
Related
I have a solution in Visual Studio 2019 containing a WinForms .Net Framework 4.8 project and a .Net 5.0 test project. The WinForms project contains all my windows forms and some helper classes. The test project contains unit test code (using Xunit).
In my unit tests I want to test certain elements of my forms. For this I need a reference to System.Windows.Forms. The problem is that I am unable to add this reference to my test project. When clicking "Add project reference", there is no option to add assemblies as there is in the WinForms project.
How do I get this reference added? And why is it not possible to add assemblies to the test project?
I read this question, which seems to suggest that I need to delete my test project and replace it with a .net Framework 4.8 project. But this confuses me, since there is only one option available when creating a Xunit test project, namely .Net Core. So if Xunit is only for .Net Core projects, then how do I test a .Net Framework project?
Reading this question, I tried adding the reference manually to my .csproj file, but then I get compiler warning "Package 'System.Windows.Forms 4.0.0'... This package may not be fully compatible with your project.". Should I be concerned about this warning? This is probably a clue that I'm not supposed to do it this way.
Project A is a Windows Console Application that uses .NET Standard 2.0 (for compatibility) and has the following line of code
public List<string> GetListOfSerialPorts()
{
return System.IO.Ports.SerialPort.GetPortNames().Distinct().ToList();
}
Local unit tests run this code successfully. Meaning that when called from a local unit test, it works.
I now have a Project B (in the same Solution), which is a Windows GUI based on .NET 4.7.2. . This project does not need to as large compatibility. This project "uses" (references) Project A. The same function is called but from Project B and when that exact same line of code is called, I see an exception:
"System.IO.Ports is currently only supported on Windows.").
Help?? Any clues?
As a side note, if I try to implement 'SerialPort.GetPortNames()' in Project B, Visual Studio complains that it cannot reference the System.IO.Ports assembly but when I add it as a reference to the project, it complains that there are now two (ambiguous) references to System.IO.Ports (it is apparently already in 'System'). Note that this is not my main issue. My main issue is that my original line of code in Project A now fails when called from Project B.
Note that we have Microsoft.Windows.Compatibility 3.1.1 installed.
I resolved my issue by installing Microsoft Windows Compatibility dependency on Project B and it worked. I was under the impression it was only needed on the .NET Core project.
Startup project 'EFGetStartedUWP' is a Universal Windows Platform app. This version of the Entity Framework Core Package Manager Console Tools doesn't support this type of project. For more information on using the EF Core Tools with UWP projects, see https://go.microsoft.com/fwlink/?linkid=858496
I'm trying to connect SQLite Database to a basic UWP app for the sake of learning but when I try migration. It just keep giving me the above error. I've searched quite a lot on the internet but didn't get the appropriate answer. The Microsoft documentation is of no use in this scenario. Also I've installed Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Tools.
If anyone can suggest any other way to connect SQLite database to UWP that'll be very useful too as I'm quite new to C# and I've a university project to do on a UWP app.
I've just stepped on a similar issue, and I've been lucky enough to solve it. To start with, I found these links very useful:
Entity Framework Core tools reference - .NET Core CLI - Other target frameworks and Common options.
In my case, my solution ended up with a configuration like this:
the application project (Universal Windows) - startup project for the solution,
a class library project (.NET Standard 2.0) - with the models and context classes,
a dummy console app project (.NET 6.0) - necessary to act like a startup project for the tools.
Have into account that (at the time of writing) UWP doesn't support .NET Standard 2.1. It supports .NET Standard 2.0 from version 10.0.16299. This means the (Universal Windows) application project can't reference a .NET Standard 2.1 project (e.g. .NET 5.0, .NET Core 3.0, Mono 6.4, etc.), so the class library project CAN'T be .NET 2.1, .NET 5.0, etc. The opposite is not true: a .NET 5.0 or .NET 6.0 project can reference a .NET Standard 2.0 project, so the dummy console app project can be .NET 6.0.
Steps to add a migration:
In the class library project, install the Nuget package for the corresponding EFCore platform used (e.g. for SQLite, install Microsoft.EntityFrameworkCore.Sqlite). Do not install the most recent version but the last that depends on .NET Standard 2.0, which is 3.1.21 at the time of writing.
In the dummy console app project, install this Nuget package: Microsoft.EntityFrameworkCore.Design. For compatibility purposes, install the same version as the version chosen in the previous step (e.g. 3.1.21).
In the application project, add a reference to the class library project. (Right-click on the project, select Add > Project Reference..., and tick the class library project.)
In the dummy console app project, add a reference to the class library project. Note there's no need to change anything else in the dummy console app project, although the project doesn't have to be built (less deployed), so feel free to open the Configuration Manager, and untick Build (and Deploy). In addition, in the Configuration Manager, don't bother to change the platform (x64, Any CPU...) to be the same as the referenced class library project. After all, the dummy console app project won't be built, so ignore any warnings about the architecture not being the same as the class library project.
Rebuild your solution to make sure there are no other issues going on. If there are any, fix them before continuing.
Open the command prompt (tip: navigate to the root folder of your solution with Windows File Explorer and type cmd in the address bar to open the command prompt in that folder).
Type the following (-p signals the target project while -s indicates the startup project):
?> dotnet ef migrations add #MigrationName -s #DummyConsoleAppProject -p #ClassLibraryProject
Optionally, unload the dummy console app project, and load it again next time you need it to add a migration.
This looks to be a known issue from here: https://github.com/aspnet/EntityFrameworkCore/issues/9666
Using EFCore with UWP is a bit finicky.
So, add a new project to your solution -> select .net core console app -> create DBContext and your models there -> run the "Add-Migration" command and reference it back to your main project.
That should get rid of that error and let you use migrations with UWP.
I have a project where I use Azure Durable Functions, and they are available only on .NET Standard 2. So, it defines which class library can be used in testing projects. But, I cannot put together a library where either xUnit, NUnit or MSTest is used in unit/integration testing.
Adding NUnit to a project where .NET Standard 2 is class library fails with the following error:
INFO: Restoring packages for
C:\VSTS\github.com\netstandardXunitMsTestNunit\src\Netstandard2xUnitMsTestnUnit\nunit\nunit.csproj...
DEBUG: Restoring packages for .NETStandard,Version=v2.0... DEBUG:
Resolving conflicts for .NETStandard,Version=v2.0... ERROR: Cycle
detected. nunit -> NUnit (>= 3.9.0). DEBUG: Checking compatibility
of packages on .NETStandard,Version=v2.0. DEBUG: Checking
compatibility for nunit 1.0.0 with .NETStandard,Version=v2.0.
The error is the same for xUnit (just the error message talks about xUnit cycle).
Both error can be reproduced in Rider and Visual Studio 2017 Enterprise too. I tried it again after I cleaned nuget cache. The result is the same.
In case of MsTest, possible to add ms test libraries, but test discovery does not work neither Rider and nor Visual Studio.
Is it even possible unit test a .NET Standard 2 library?
Is there anything I can do beside waiting for these projects to pick up .NET Standard 2 stuff?
I created a small sample project, can be found here: https://github.com/SayusiAndo/netstandard2xunitresharper
There is no runtime for .NET Standard, so it will not execute your tests.
Your test assembly must target an executable platform, such as a version of .NET Framework or .NET Core.
<TargetFramework>net470</TargetFramework>
Or
<TargetFramework>netcoreapp2.0</TargetFramework>
See Running .NET Standard binaries on different frameworks for more details.
.NET Standard is a specification that each .NET Standard version(such as .NET Framework, .NET Core and Xamarin) defines the set of APIs that all .NET implementations must provide to conform to that version. You library has a value for TargetFramework of netstandard2.0 means you can reference the logic library not only from a .NET Core app, but also from an app built for .NET Framework or Xamarin.
However, You can’t build apps for it, only libraries. Here's the MSDN doc about .NET Standard.
So if you want to test the library, you need to specify the targets of which your library would support. And if you want to support multiple .NET version then you should test them all to make sure your library can run on these targets correctly. Here's the configuration of target framework in .csproj:
Single target:
<TargetFramework>net461</TargetFramework>
Multiple targets:
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
Create a new unit test project in the same solution that targets say .Net Framework 4.6.1 if your class library is to be used by an application that targets .Net Framework 4.6.1 so you test with the same combination of frameworks.
Add a reference to the class library project under references in the unit test project.
Add the xUnit and xUnit.runner.visualstudio nuget packages to the unit test project.
Rename the unit test class to something relevant, and replace the using MSTest directive with using XUnit.
Start writing and running tests.(build/re-build solution so it updates the tests list in the test explorer for each new test).
I have recently released my private unit test platform that does .NETStandard 2.0 just fine. Unless you have put a lot of time and effort into your tests you might want to have a look at Nuclear.Test.
Basically if you target your test project at .NETStandard it will execute these tests in a .NETFramework and .NETCore process to see if they both work correctly.
Unfortunately it requires .NETStandard 2.0 as a minimal version so far. Luckily that's what you are using.
This solution is neither NUnit nor xUnit nor MSTest but it will do exactly what you described.
I created a portable class library and then targeted it at .NETStandard 1.5 from the project properties. I then created a second project with the exact same project settings, but added code to use for invoking and testing the code in the first project. But I have been unable to add MSTest to use for triggering execution. I tried using the guidance at stack overflow # 41350323
but ran into problems. Wondering if anyone out there has been able to use MSTest for unit testing portable .NETStandard class libraries. Note that I also tried using the guidance for .NET core but the MSTest framework nuget does not seem to support .NETStandard.
If not a unit test, then is there a way to invoke from a console? I just need to test my code and this should not be so difficult.
You can define logic in a .NET Standard project (don't use PCL though, use the .NET Standard Class Library template in VS2017 RC), but then the unit test project should have its target platform (UWP/Desktop/Xamarin or others) instead of .NET Standard. For example, in VS2017 RC, a typical unit testing project can be a .NET Core console app.