Basic Assert in xUnit - c#

Looking for a basic assert in xUnit. This is simple in MSTest and NUnit.
Assert.Fail("some message"); // MSTest - works
Assert.Fail("some message"); // NUnit - works (same syntax)
Assert.Fail("some message"); // xUnit - does not compile
The project is a C# xUnit Test Project (.NET Core) in Visual Studio 2019.
UPDATE
Responding to comments about what version of xUnit is installed. The NuGet package was installed automatically by Visual Studio when the project was created. The .csproj file reveals that Visual Studio installed version "2.4.0".

In XUnit assert message is a parameter. It is part of all Assert Extension Methods.
For example :
Assert.False(true, "true will never be false")

xunit 2.4.2-pre.12 contains the Assert.Fail(string) method.
However, as was pointed out in the comments, this was recently added so prior versions do not contain the Assert.Fail() method.

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

Use MSTest V1 in Visual Studio 2017

My organization has a large number of legacy test code that is all set up to run on MSTest V1. And until they all have been migrated to MSTest V2, nobody is allowed to MSTest V2.
Now I have run into an unexpected problem. I need to create a new test project, but I can not find a way how to create a new MSTest V1 project in Visual Studio 2017.
In both .NET Core and .NET Framework, it creates a project with MSTest V2. And when I manually try to uninstall the MSTest V2 NuGet Packages and add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll it seems that that DLL is actually missing.
Is there a way to get MSTest V1 functionality working like this? Because the existing projects work perfectly fine.
Thank You!
So I am not sure myself why I couldn't find the DLL on the first attempt. When creating another project from scratch, the Quality Tools DLL was snuggly available. Once I picked that one and removed MSTest v2 it worked just fine!

No test is available in <path>\PKGS\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll

I am using .NET framework 4.5 and C# for my project and i used mstest for Test project. All unit test cases ran fine on Visual Studio 2017 enterprise edition, but when I created Jenkins for the Unit test, some of tests are executed and in middle the build fails saying:
No test is available in
\PKGS\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll.
Make sure that test discoverer & executors are registered and platform
& framework version settings are appropriate and try again
Additionally, path to test adapters can be specified using
/TestAdapterPath command. Example
/TestAdapterPath:. vstest.console.exe
F:\jnks\workspace\mPrint_Roam_Horizon_Inc\PKGS\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll
/InIsolation /EnableCodeCoverage
The error says that the file Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll does not contain any tests (which is correct, because it is a frameworkfile). You probably have some errors in the path where you are looking for dll's with tests in them.

.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.

How to Add Test Project for MVC Project in Visual Studio 2013

I have an MVC 4.5.1 project in Visual Studio 2013. It is already well built out and working.
I need to add unit tests to it. How can I add a testing project?
It doesn't seem like the options available to do this for previous versions of Visual Studio are the same in 2013.
Additional info: I'll be using NUnit and Moq for testing.
Just add a library project for tests and then install nunit and moq packages.

Categories

Resources