Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement unit tests for .net Standard Library

So as far as I have been understanding from my research. A .net Standard Library can not be used on its own, so it needs to be tested through a different framework, either with .net Framework or .net Core reference. That's just how I interpreted it. Now I am trying to create a Standard Library as I need it to be compatible on most devices as possible, the problem is I don't know how to create the unit tests correctly. Every time I create a MSTest Project I get the following error:

Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X64. Following DLL(s) do not match framework/platform settings. Otchi.Ebml.Tests.dll is built for Framework 2.1 and Platform AnyCPU.

I have experimented a lot with the different architectures and other settings, but nothing seems to work to remove that warning. Am I doing something incorrectly or is there a different, more suitable approach to creating unit tests for a Standard Library

like image 244
J.Paravicini Avatar asked Oct 07 '19 16:10

J.Paravicini


People also ask

How do I test a .net class library?

Test the Release version of the library In the Visual Studio toolbar, change the build configuration from Debug to Release. In Solution Explorer, right-click the StringLibrary project and select Build from the context menu to recompile the library. Run the unit tests by choosing Test Run > All Tests from the menu bar.

Which unit testing framework is used with the C# and .net framework?

xUnit test is the best Unit testing framework for . Net programming languages like C#, VB.Net, and F#.

What can be used for unit testing in .net solutions?

xUnit. xUnit is a free, open source, community-focused unit testing tool for . NET. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing .


1 Answers

A .NET Standard Library does not contain the necessary components for execution. It only defines the execution patterns. When you deploy to a specific machine / processor architecture, the code must know which instruction set to execute upon.

This is where the .NET framework and .NET core come into play. The .NET framework contains execution details for instruction sets supported by the Microsoft Windows operating system. .NET core, similarly, contains the instruction set for additional architectures not natively supported by Windows.

To write Unit Tests, you need to create a NEW project, either in .NET or in .NET core, to execute your code. If you want to cover all your bases, or if you have some binaries that are compiled differently for different architectures (third party libraries come to mind) then you might want to unit test for multiple distributions.

Your library should be a standalone library. Your unit tests should be executed before you update the library to validate that the code being entered into the library passes. That way anyone who uses the library after that can feel confident that it will work.

like image 66
Zakk Diaz Avatar answered Oct 26 '22 20:10

Zakk Diaz