I've just installed Visual Studio 2017. I have a project using NUnit for the test cases. Ctrl + R - T no longer runs the tests, and the Test Explorer no longer finds any test cases marked with the TestCase attribute.
Is there a way to get NUnit running yet, or an update I could find?
I reinstalled NUnit from the NuGet Package Manager to the latest version with no improvement.
Add the NUnit test adapter NuGet package to your test projects
2.* (https://www.nuget.org/packages/NUnitTestAdapter/)
3.* (https://www.nuget.org/packages/NUnit3TestAdapter/)
Or install the Test Adapter Visual Studio extension. There is one for
2.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTestAdapter)
3.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnit3TestAdapter).
I prefer the NuGet package, because it will be in sync with the NUnit version used by your project and will thus automatically match the version used in any build server.
You need to install NUnitTestAdapter. The latest version of NUnit is 3.x.y (3.6.1) and you should install NUnit3TestAdapter along with NUnit 3.x.y
To install NUnit3TestAdapter in Visual Studio 2017, follow the steps below:
Right click on menu Project → click "Manage NuGet Packages..." from the context menu
Go to the Browse tab and search for NUnit
Select NUnit3TestAdapter → click Install at the right side → click OK from the Preview pop up
This one helped me:
Getting started with .NET unit testing using NUnit
Basically:
Add the NUnit 3 library in NuGet.
Create the class you want to test.
Create a separate testing class. This should have [TestFixture] above it.
Create a function in the testing class. This should have [Test] above it.
Then go into TEST/WINDOW/TEST EXPLORER (across the top).
Click run to the left-hand side. It will tell you what has passed and what has failed.
My example code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace NUnitTesting
{
class Program
{
static void Main(string[] args)
{
}
}
public class Maths
{
public int Add(int a, int b)
{
int x = a + b;
return x;
}
}
[TestFixture]
public class TestLogging
{
[Test]
public void Add()
{
Maths add = new Maths();
int expectedResult = add.Add(1, 2);
Assert.That(expectedResult, Is.EqualTo(3));
}
}
}
This will return true, and if you change the parameter in Is.EqualTo it will fail, etc.
You need to install three NuGet packages:
NUnit
NUnit3TestAdapter
Microsoft.NET.Test.Sdk
You have to choose the processor architecture of unit tests in Visual Studio:
menu Test → Test Settings → Default processor architecture
Test Adapter has to be open to see the tests: (Visual Studio e.g.:
menu Test → Windows → Test Explorer
Additional information what's going on, you can consider at the Visual Studio 'Output-Window' and choose the dropdown 'Show output from' and set 'Tests'.
For anyone having issues with Visual Studio 2019:
I had to first open menu Test → Windows → Test Explorer, and run the tests from there, before the option to Run / Debug tests would show up on the right click menu.
Using the CLI, to create a functioning NUnit project is really easy. The template does everything for you.
dotnet new -i NUnit3.DotNetNew.Template
dotnet new nunit
On .NET Core, this is definitely my preferred way to go.
Install the NUnit and NunitTestAdapter package to your test projects from Manage Nunit packages. to perform the same:
1 Right-click on menu Project → click "Manage NuGet Packages".
2 Go to the "Browse" tab -> Search for the Nunit (or any other package which you want to install)
3 Click on the Package -> A side screen will open "Select the project and click on the install.
Perform your tasks (Add code)
If your project is a Console application then a play/run button is displayed on the top click on that any your application will run and If your application is a class library Go to the Test Explorer and click on "Run All" option.
To run or debug tests in Visual Studio 2017, we need to install "NUnit3TestAdapter". We can install it in any version of Visual Studio, but it is working properly in the Visual Studio "community" version.
To install this, you can add it through the NuGet package.
Related
I have a unit test in NUnit that call a recursive C++ function from CLI code.
The unit
[Test]
public void MyTest()
{
bool result = new MyClass().RecursiveFunction();
Assert.True(result);
}
As mentioned, RecursiveFunction() is a CLI function, which just call a C++ recursive function.
The unit test pass when I run it from Resharper's test explorer, however, when I try to run it from Visual Studio Test Explorer it fails due to StackOverflowException. In both cases I use x64 build & run configuration, and in both cases I use no shadow copy.
Have you check that the Test Explorer in VS Studio is on x64?
We had the exact same issue, If you are using team city and your nunit version is 2.6.4
we upgraded out nunit version into 3.9, please note nunit changed the API and the upgrade might take a few hours, and also the structure of the libraries of the nunit-console and nunit framework was changed.
Once we upgraded it to the new Nunit version it solved the issue.
I found this issue solved in the team city forums and I did upgrade nunit and it worked.
Trying to debug an NUnit test in VS2010 and is not stopping on the breakpoint. It was working the last time I tried need to debug the test (maybe 1 yr ago) but now it doesn't work.
I've tried the following:
How to debug with vs.net 2008 and nunit?
http://erraticdev.blogspot.com/2012/01/running-or-debugging-nunit-tests-from.html
I've tried attaching, rechecking my installation, updated the config file so it's using the 4.0 runtime, still doesn't hit the breakpoint.
What am I missing?
You can debug nunit tests in VS studio community version only. For this you need to install Nunit3testAdpter. You can add this in Tools -> Extension and updates. or from Nuget packages.
This is a long overdue question but I haven't seen the solution to debug NUnit tests directly within Visual Studio. This is quite easily possible and without any third party tools. All you have to do is configure your test project (which is just a plain old Class library project) so it will start NUnit test runner whenever you hit F5 or Ctrl-F5 (just run, no debugging).
Particularly you will have to configure your project properties to start an external program:
Step by step configuration is very well described in this blog post. It advises you to use NuGet to obtain NUnit test runner and configure project to execute the runner when you try running your library project. As simple as that. Using no specific tools which are usually not free.
There are multiple reasons why this may happen. If your application's target framework is different from NUnit's, then you won't be able to debug, because the tests are actually being run by nunit-agent.exe.
For me, my application was using 4.5, but NUnit's was using 3.5. (You can find this from one of the NUnit GUI applications -> Help -> About)
To fix this, change the nunit.exe.config file to include the following, inside the configuration section:
<startup>
<requiredRuntime version="4.0.30319" />
</startup>
This can also happen when your PDB generation is set to embedded. Switching it to full will allow the nunit test adapter to start with the debugger attached. At that point it can be switched back to embedded and it will debug again.
The setting is on the project properties pages, in the Build tab, then click the Advanced button. I always choose "All Configurations" at the top, so I make sure I'm emitting a useful PDB (or embedding the info) when building Release configuration assemblies too.
The other day I failed to hit a breakpoint in my NUnit debugging. It took me a while to realize that someone had copy/pasted the test into a different file and I was running a different test than the one that the breakpoint was in.
I recently had a similar problem. Changing the project's platform to AnyCPU solved it for me. Also worth checking:
1) Check NUnit's latest version (I was using 2.5.7, updated to 2.6.2)
2) The Target Framework for your test project. I was using .NET 4.0 (you can find it under Properties -> Application, on VS2010)
At least up to vs2010 the debugger can only debug 32 bit apps but not 64 bit apps.
With vs2010 I am using a 32bit nunit.exe version and use "attach to debugger". This workes well for me.
Please upgrade NUnit Test Extension and close the Visual studio so Visx (probably spell) will run and update NUnit tool.
After this you can open your project and run NUnit test project.
It will run.
I tested accurately.
Try changing the test to x64. From the menu do
Test -> Test Settings -> Default Processor Architecture -> x64.
i think u cant debug with Visual studio. better u install TestDriven.NET.
then
Put the BreakPoint on the test method
Right click on the test
method.
select TestWith->Debugger
I have created a class library prject, and created a class as described in the Video Tutorial at Watin
and using the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using WatiN.Core;
namespace ClassLibrary2
{
[TestFixture]
[RequiresSTA]
public class Class1
{
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
}
}
now what is the next step, How to run the test? Do I add the class library to a project and run calls the test method from code? or do I create a Unit Test Project (how?) ?
Thanks
To execute your test you need to compile your code, then run the test through a test runner.
The first way I ever did this was using the NUnit GUI
Compile your code to a class library. The result being a .dll file; something like MyTests.dll
Open the NUnit GUI. Start -> Programs -> etc etc etc.
In the NUnit GUI, open the .dll file that contain your tests.
In the NUnit GUI, select the test(s) you wish to run and click the Run button.
There are plenty of other ways to run your tests, but I believe the NUnit GUI to be the most basic for NUnit tests. Other options include: In Visual Studio via ReSharper, Gallio, in VS via External Tools, and many others.
If I install Gallio 3.x will it also install a test runner plugin for Visual Studio?
Or must I use an additional plug-in like TestDriven.NET or Visual Nunit to run MbUnit test classes from within VS?
Install Gallio 3.1 on the dev machine. Then in VS2008, you'll have the option to create a "MbUnit v3 Test Project". This doesn't just include all of the Gallio dlls for you, it has a magic line in the project which identifies it to VS as a Test project.
You can now just use the in-built VS2008 Test runner.
If you have any existing projects with unit tests in, rather than making new projets, edit your existing project file and add the following line on line 9 (underneath the <ProjectGuid> on line 8):
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you have a VB project, it has a different second GUID: <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids> You can find the correct values by creating a new MbUnit test project from the templates installed with Gallio and then looking at the project file (.csproj or .vbproj) in a text editor.)
Now when you reload the project, VS2008 will recognise it as a test project.
A distinct advantage that I found over using Icarus was that debugging is now far more straight forward with break points being hit as expected.
Good Luck, Lee
TestDriven.Net works really well. Gallio also supports the ReSharper unit test runner and Visual Studio test tools. We will be shipping a new release of Gallio this week with support for R# 5.0 and VS 2010.
These are instruction for running MBUnit tests in Visual Studio 2012 and above using a neat NUnit trick.
Firstly, install the NUnit Test Adapter extension (yes, NUnit)
Tools > Extension and Updates > Online > search for NUnit > install
NUnit Test Adapter.
You may need to restart the Visual Studio IDE.
Then, you simply need to add a new NUnit test attribute to your test methods. See example code here (notice the using statements at the top) ...
//C# example
using MbUnit.Framework;
using NuTest = NUnit.Framework.TestAttribute;
namespace MyTests
{
[TestFixture]
public class UnitTest1
{
[Test, NuTest]
public void myTest()
{
//this will pass
}
}
}
You can run and debug the test in visual studio as NUnit and Gallio Icarus GUI Test Runner will run them as MBUnit (enabling parallel runs for example). You will need to stop Gallio from running the NUnit tests by deleting the NUnit folder in the gallio install location i.e. C:\Program Files\Gallio\bin\NUnit
Hope this helps, this is a simple working method so please vote up, many thanks.
Is there an official Visual Studio test project template for NUnit? If not, what might one look like?
There is no official one. Just start a class library project and import the nUnit assemblies to it.
There are plenty of template how to articles around though - here is one.
We can use this official Template for Unit Testing as well.
Somebody on Github made a template: https://github.com/orangutanboy/NUnitTestFixtureTemplate
When creating a MVC application you have the option for creating a unit test project. Does not give examples but provides a framework.
There is no official template for NUnit exists in visual studio.you have to explaicitly download it. But it has general unit test framework.
you need to download Nunit framework using either nuget package.
Select Tool menu -> Library package manager ->manage nuget package for solution
or
Select Tool menu -> Library package manager ->package manager for console.
Add the reference in existing unit test project and remove the existing unit test namespace from it.