AssemblyInitialize seems break unit test project in .net 6 (visual studio 2022) - c#

I have a .net 4.8 unit test project and just migrated it to .net 6.
Although Test explorer shows all tests, when I try to run it in Visual Studio, it just doesn't run anything.
What I found is that as soon as my unit test project contains this AssemblyInitialize attribute, Visual studio just won't run any of the tests.
[TestClass]
public class SetupAssemblyInitializer
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
}
}
This works fine as long as the project is in .net 4.8 and I didn't find any doc saying this attribute doesn't work in .net 6 any more. Is there any workaround or alternative to run some code only once in my Unit Test project?
If I decorate the method with [ClassInitialize], then the project will run without issue. However, this Attribute will not make the method run only once for all tests.

Related

The NUnit 3 driver encountered an error while executing reflected code (NUnit.Engine.NUnitEngineException)

I have a simple Nunit project which contains only a single test. I want to execute my code using command, so I have installed ‘Nunit-console.exe’ on my Windows 10 machine. After installation, executed the code on command prompt using below command
nunit3-console.exe "G:\LiveProjects\NUnitTestProject2\NUnitTestProject2\bin\Debug\netcoreapp3.1\NUnitTestProject2.dll"
like so
After executing this command, I have discovered below error message:
NUnit.Engine.NUnitEngineException : The NUnit 3 driver encountered an error while executing reflected code.
----> System.InvalidCastException : Unable to cast transparent proxy to type 'System.Web.UI.ICallbackEventHandler'.
--NUnitEngineException
The NUnit 3 driver encountered an error while executing reflected code.
like so
Please refer my below code:
[TestFixture]
public class Tests
{
[SetUp]
public void Setup()
{
Console.WriteLine("Welcome setup");
}
[Test]
public void Test1()
{
Console.WriteLine("Welcome first Test ");
Assert.Pass();
}
}
like so
Configuration details of my project:
Microsoft.NET Framework version 4.7.0
OS: window 10
IDE: Visual studio 2019
Use Nunit framework for unit testing
NUnit3TestAdapter (Version) 3.16.1
Microsoft.NET.Test.Sdk (Version) 16.5.0
NUnit.Console (Version) 3.11.1
Target .NET framework (Version) 3.1
Also, I have tried to disable the ‘code coverage’ option in visual studio 2019, however - I am not able to see it in. Can anyone suggest - where is ‘code coverage’ option available in visual studio 2019.
Charlie's comment is correct - however unfortunately still won't resolve your issue. Your test suite targets .NET Core - which the NUnit Console does not yet support. (Although there is a beta version out with support.)
For now, the best solution is to run with dotnet test, rather than the nunit console. For more details, see the guide here: https://docs.nunit.org/articles/nunit/getting-started/dotnet-core-and-dotnet-standard.html
Version 3.16.1 of the NUnit 3 test adapter is not compatible with version 3.11.1 of the engine, which is used by the console package you installed.
You must either downgrade the console runner to 3.10 or upgrade the adapter to 3.17.
For a detailed explanation see my blog post at http://charliepoole.org/technical/nunit-engine-version-conflicts-in-visual-studio.html
Remove the console runner and run the below command that works
dotnet test [path to your nunit test project solution file]
Ex:
dotnet test D:\MyProject\MobileProject\MobileProject.NUnitTest.csproj
Mac:
dotnet test /Users/MyName/MobileProject/MobileProject.NUnitTest.csproj
https://docs.nunit.org/articles/nunit/getting-started/dotnet-core-and-dotnet-standard.html

.NET unit test run without Visual Studio

I have build the unit test DDL on my machine and run on the another place without installing visual studio.
To do this, I install the NUnit command line and call the my unit test DLL, and it does not work.
In my C# code I only add the [TestMethod] attribute on my test method.
The below image shows how I call the NUnit on the command line:
Is there an issue in here, or is there any other way to do this?
Note: I use the Selenium library in my project.
The NUnit console runner is made to run NUnit tests, that is, tests that use the NUnit framework. Since you are using the [TestMethod] attribute, you are probably using Microsoft's test framework.
NUnit used to be delivered (version 2) as a single package, but with version 3 it is divided into several different packages. You will have to download and reference the NUnit framework and change your tests to use its attributes and assertions.

C# TestClass on Mac

I am trying to get a file working on OS X which requires the use of the [TestClass()] and [TestMethod()] attributes. Im new to c# (Ive used java for a few years now) so Im not sure, but I think these attributes are part of Microsoft.VisualStudio. Since I'm on OS X I an using mono to compile and run C# files. How, if possible, can I get the same functionality as the Microsoft.VisualStudio.TestTools.UnitTesting namespace?
Xamarin Studio / Monodevelop has a unit testing add-in which allows running NUnit tests within the IDE. The add-in should be installed by default. If not, you can install it through Tools > Add-ins.
When you build a class that has [TestFixture] attribute and it has a method with [Test] attribute - you can run it from View > Pads > Unit Tests or green icon at signature line. In this screenshot, lines 12 and 26:
Additionally, there should be a project template which already has NUnit package pre-installed and a test class ready:
[TestClass()] and [TestMethod()] are part of MSTest framework and are not available in Mono.
Have you looked at using a mono build of Nunit you can also use "using mapping" to convert you ms test attributes to nunit here is an example http://www.martinwilley.com/net/code/nunitmstest.html
If using mono isn't a requirement, you could also look at using .NET Core it's still in development but it's the full .net stack by Microsoft for OSX and Linux

No test cases are showing in the NUnit test explorer

I have installed the NUnit VS extension into VS Community 2015. I have a VS solution containing a test project and within that I have a class
[TestFixture]
public class Tests
{
[Test]
public void canAddOnePlusOne()
{
Assert.Fail("Can't add");
}
}
The solution (re)builds successfully. Nothing appears in the Test Explorer. There is no output from running all tests. How do I get my tests to appear in the Test Explorer?
Without version numbers Install-Package NUnit had installed test framework version 3. The extension (without version #s) supports up to 2.6.4 version. Had to install the NUnit3 extension.

Unit test UWP app with NUnit on VS2015

I spent far too much time trying to run a basic NUnit test on a Microsoft Store app. I installed NUnit v3.0.1 and NUnit3TestAdapter on VS2015 Pro but I get the following which confirms that the test is not discovered:
------ Discover test started ------
NUnit Adapter 3.0.8.0 discovering tests is started
Exception NUnit.Engine.NUnitEngineException, Exception thrown discovering tests in <project exe file>
The NUnit 3.0 driver does not support the portable version of NUnit. Use a platform specific runner.
NUnit Adapter 3.0.8.0 discovering test is finished
========== Discover test finished: 0 found (0:00:01.0639991) ==========
The NUnit.Engine.NUnitEngineException error referred to is not helpful because there is no way of exploring where the exception is in my program. The test is a single method with one Assert statement just to test if the setup works so there is nothing complicated. I also don't understand what a "platform specific runner" is. I installed both NUnit and NUnit3TestAdapter thru NuGet Package Manager and I presume installations for relevant platform and version are carried out.
I even tried out installing "NUnit Templates for Visual Studio". This allowed me to create an NUnit project, and interestingly the test within NUnit project is discovered and run. But the downside is that I cannot reference my UWP app project from the working NUnit project, I think because the NUnit project only supports .Net frameworks (up to 4.6.1) not Windows 10 platforms.
I have used NUnit before for non UWP applications without much trouble. Am I missing something or NUnit as yet doesn't support UWP apps?
At the moment you can only test DNX projects with XUnit.
Here is a nice starting point for that.
From the official Asp.Net 5 documentation (which uses the same infrastructure as UWP):
For example, to work with the xUnit test framework, you would
configure the dependencies as follows: [example project.json I dont
want to copy]
As other test frameworks release support for DNX, we
will link to them here.
Since there are no other frameworks linked in the documentation, I assume that there are none that support DNX projects at the moment.
Since this question was originally posted NUnit has developed it's support for testing UWP/Xamarin apps. Check out the nunit.xamarin runners.
Essentially, your tests should be created in either a portable library, or a shared project. This can then be referenced by a UWP project runner, which can be run on the emulator.
If you take the shared project approach, you can also reference this through an .NET 4.5 project, which will allow your tests to integrate with the NUnit VS adapter, and show up in VS. Portable libraries can not yet be run with this runner, however that's in the pipeline.

Categories

Resources