Xamarin portable .NETStandard 1.5 unit test - c#

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.

Related

Can't add reference to System.Windows.Forms to .NET 5.0 Xunit unit test project

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.

create unit tests for .net 6.0

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.

How to test .NET Standard 2 library with either NUnit, xUnit or MSTest from either Rider or VS 2017?

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.

Can I use new NUnit APIs from a Xamarin.Android unit test app?

I'm currently writing a Xamarin.Android unit test app with NUnit / VS2017. I am using the version of NUnit provided by Xamarin that was automatically referenced when I created my project, namely NUnitLite. Its assembly, Xamarin.Android.NUnitLite, appears to be a framework assembly rather than a NuGet package, and I can't uninstall it.
I'm wondering, am I stuck with whatever NUnit APIs Xamarin chose to put in NUnitLite? I want to write
Assert.That(expected, Does.StartWith(actual));
But it looks like that was added in NUnit v3, and the Does class doesn't even exist in NUnitLite. Does this mean I have to write
Assert.IsTrue(expected.StartsWith(actual));
instead?
Xamarin uses a forked version of the original NUnitLite 0.7 or 1.0. (They actually started using it at version 0.7, and I'm not sure if it got updated to 1.0.)
That NUnitLite (which is still available from the NUnit project on GitHub) was a framework and matches approximately the features of NUnit 2.4 - 2.5. The notion of fluent, constraint-based tests originated in NUnitLite and was eventually added to NUnit itself.
With NUnit 3, we eliminated the distinction between "Lite" and "Full" nunit. The extra code that allowed NUnitLite to be used with self-executing test assemblies was refactored into a separate package and we called that "NUnitLite."
In retrospect, the renaming was a mistake. It's obviously confusing to have two NUnitLites out there, one of them a framework and the other not! At this point, however, I'm not sure there is much to do about it.
The alternative to using the Xamarin runner and test is to use NUnit and NUnitLite 3.x. Another option is to use the NUnit Xamarin runner, which is however not yet fully stable.

Framework/visual studio project type to use for unit testing .net standard libray?

Given that the implementation of the .net standard library is provided by the underlined implementation (.Net framework, .Net Core (windows/Linux etc) ). And it is possible that the APIs have little different behaviour. TimeZoneInfo.Id provides different results based on the OS (last time I checked).
So, how to unit test? In visual studio we can either create a .Net Core xUnit project or .Net Framework xUnit project. How to run the same xUnit tests in two different environment?
The simplest way is to multi-target the test project. That is, create a .NET Core xUnit project from the template and change
<TargetFramework>netcoreapp1.1</TargetFramework>
to
<TargetFrameworks>netcoreapp1.1;net461</TargetFrameworks>
Note the change to plural / additional s here. The plural form allows you to specify multiple target frameworks to test on, so you could easily add multiple versions of .NET Core and .NET Standard.
While the VS-integrated test runner will only run the first framework at the moment (see this tracking issue on GitHub), dotnet test on the console will run your tests on all specified configurations.
Instead of targeting multiple runtimes, you could just as well use a test platform that is capable of running net standard tests by default. Nuclear.Test can help you with a more complete feature test of your implementation on multiple platforms.

Categories