How to debug Nunit test in Visual Studio 2010 - c#

I have a problem debugging an NUnit test from VisualStudio. I created an empty project (Console Application), then I added references to the NUnit library and wrote a simple test.
namespace ReimplementingLinq.Tests
{
[TestFixture]
public class WhereTest
{
[Test]
public void SimpleFiltering()
{
int[] source = { 1, 2, 3, 4, 2, 8, 1 };
var result = source.Where(val => val < 4);
int[] expected = {1,2,3,4};
CollectionAssert.AreEqual(expected, result);
}
}
}
Next I followed the advice given in this link
How do I run NUnit in debug mode from Visual Studio? but none of the solutions in that topic work for me. None of my breakpoints are hit while performing the test. I tried testing the solution by attaching to the process and also by running the project with an external program with arguments.
What can I do to debug my unit test?

Running or debugging NUnit tests directly with Visual Studio
Look ma' no extensions!
Simply configure your test project so that when you hit F5 (Start debugging) or Ctrl-F5 (Start without debugging) it will automatically start NUnit GUI and execute all tests within it. If any breakpoints get hit, you will also be able to simply debug your test code.
A step-by-step guide with images shows you exactly how to do it.
Laziness is the mother of all invention :-)

Assuming you're using a version of Visual Studio other than Express Edition then TestDriven.NET might be of use.
After installing it
Set a breakpoint within your test method
Right click and choose Debugger from the Test With menu
The debugger should launch and hit your breakpoint
Unfortunately you can't use this method with Express editions of visual studio because TestDriven.NET is a plugin for visual studio and the Express editions do not support the use of plugins
Running a test within a console app
You can also run a test in the debugger via a console application:
Create a new console application
Reference your unit tests project
Inside the Main method of the console application create a new instance of your test fixuture and then call one of the test methods. For example if I have a fixture named MyTests and a test named Test1 I'd write:
var myTests = new MyTests();
myTests.Test1();
Set a breakpoint at the line where you create an instance of the MyTests class and press F5
The debugger will hit your breakpoint and then you can use F11 to step into your TestFixture's constructor, or step over that into the test itself

If you are using NUnit 2.4 you can put the following code in your SetUpFixture class. (You can do this with older versions but you will need to do whatever equivalent that has to the SetUpFixture, or copy it in to the test itself.)
[SetUpFixture]
public class SetupFixtureClass
{
[SetUp]
public void StartTesting()
{
System.Diagnostics.Debugger.Launch();
}
}
What Debugger.Launch() does is cause the following dialog to show up when you click Run inside NUnit.
You then choose your running instance of visual studio with your project open (the 2nd one in my screenshot) then the debugger will be attached and any breakpoints or exceptions will show up in Visual Studio.

Have a look at NHarness on CodePlex.
It's a very simple, reflection based, test runner library, that recognises NUnit attributes, and can be run from a project within Visual Studio Express, allowing debug testing.
It currently has a test class level granularity, but method level calls are, supposedly, going to be added soon.

Had the same problem with NUnit 2.5.3, and eventually found a different solution. See if this works for you:
Open NUnit GUI and go into Tools menu Settings... dialog. Select Assembly Isolation subsection of Test Loader section in Settings tree. Set the Default Process Model to Run tests dierctly in the NUnit process.
I had it set to Run tests in a single seperate process, which is why the debugger could not link my dll under test to the symbols for debugging it. I am still using Use a sperate AppDomain per Assembly for my Default Domain Usage.

Related

vs2015 debug selected tests has never worked, no suppend on breakpoint

c#
namespace MyTestProject
{
[TestClass]
public class MyTesterClass
{
[TestMethod]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("$(SolutionDir)\\MyWebProject", "/MyWebProject")]
[UrlToTest("http://localhost:29633/")] // FIXUP for your port
public void MyTestMethod()
{
Console.Out.WriteLine("BEFORE");
Assert.IsTrue(System.Diagnostics.Debugger.IsAttached); // always fails
System.Diagnostics.Debugger.Break();
Console.Out.WriteLine("AFTER");
}
}
`
IDE: VS2015 update 1 (on Win10), no extra 3rd party addons installed
Target: .net 4.5
Using IISExpress to test as a non-Administrator user (which is 32bit even on 64bit system for me)
Have 2 binds setup in IISExpress the "localhost:29633" and one to IP "192.168.0.2:29633" to allow across network testing and usage. This is all working just fine. This is setup in .vs\config\applicationhost.config with 2 entires bindingInformation=":29633:*" and bindingInformation=":29633:192.168.0.2"
I can get my unit tests to pass successfully, but I can not get execution to stop at any breakpoint that is setup, either in code Break() or the IDE breakpoint window.
So I provide a target test class to get working in your IDE to prove it should work for someone else as-is.
You'll need to create a dummy/empty ASP.NET project called "MyWebProject" and have this test case in its own project.
Things I have tried:
Resetting all settings
Ensuring both projects (MyTestProject and MyWebProject are rebuild and setup for Debug build types)
Ensuring the ASP.NET debugging is enabled on that project.
Modifying Test menu -> Test Settings -> Default Process Architecture from 32bit to 64bit.
Modifying Test menu -> Test Settings -> Keep Test Execution Running
Settings for debug symbols, disabling Just My Code,
Settings for Common Properties -> Startup Project -> Multiple startup projects
Tried this (but all my code in project is rebuilt, I do not need to debug any DLLs): Use Managed Compatibility Mode
Lots of other things tried that I have simply forgotten now. Have "Reset all settings" since.
If I try to attach to process (existing IISExpress.exe that runs in background between tests), then I can not start my tests at all, option "Run/Debug All Tests" greyed out.
If I try to start any other app, like running ASP.NET webapp on its own, again I can not start the tests the options are greyed out.
So it has to work from "Test [menu] -> Debug -> All Tests" and letting VS2015 start up all processes as needed, with debugger attached (ideally to both IISExpress.exe and TE.processhost.managed.exe).
What I find painful is that if something went wrong during the setup why didn't the unit test fail on the basis that VS2015 could not attach to process and then confirm correct setup of process so it was executing the expected copy of the project, etc.. Surly it should ALWAYS suspend execution at the start to allow VS to setup and communicate to inferior process being debugger, then execution resumed. It should just fail unit test run and not even start the testing if the environment if not all setup as expected.
I notice what looks like 2 te.processhost.managed.exe (one is persistent across tests and one spawns during the current test run). The breakpoint dialog indicates this process is the location of the test case breakpoints (but not ASP.NET ones)
But of course the ASP.NET runs in iisexpress.exe which is another process.
So is debugger attaching to both ok? How can I see this while it should be the case? How can I stop debugger cleaning up on termination, as this resets UI to many options in Debug -> Window -> ... are not available (if you are not quick enough).
If I set an ASP.NET breakpoint in code for example in Application_BeginRequest() from Global.asax/aspx execution does not stop in here either.
I am after a working project ZIP file and settings of the simplest test case project that proves the debugger is attached from VS so I can run-step though the code.

How to run test methods (in Visual Studio) in Microsoft Test Manager?

I'm using Visual Studio 2012 Ultimate and Microsoft Test Manager. In my project, I have written a test class with some test methods (i.e. Assert this and Assert that).
How do I actually run them in Microsoft Test Manager? I have created a Test Plan and added Test Cases, but how do I actually link my test methods to the test case? I've already looked online and on the Microsoft website, but it wasn't helpful and they were skipping so many steps.
What do I need to configure in Visual Studio? in Microsoft Test Manager?
The test case must be associated with an automated test. MTM test cases have fields for "automation status" and "associated automation", but the later cannot be set from within MTM, it has to be set from Visual Studio.
Open Team Explorer in Visual Studio and use it to find and open the test case with a query. On the right hand side of the "associated automation" tab there is an ellipsis, click it and select the test to be run. See here for more details. Note that when the test case is opened within MTM the ellipsis is not shown. I like to think of "pushing test automation from Visual Studio into MTM" being the method and that test cannot be "pulled into MTM".
Visit this URL
Here it is very well explained how to associate a test method to a test case.

Why does visual studio 2012 not find my tests?

I have some tests that use the built in Microsoft.VisualStudio.TestTools.UnitTesting, but can not get them to run.
I am using visual studio 2012 ultimate.
I have a solution of two projects; One has tests, using Microsoft.VisualStudio.TestTools.UnitTesting, [TestClass] before the class, [TestMethod] before the test methods and reference Microsoft.VisualStudio.QualityTools.UnitTestFramework (version 10.0.0.0, runtime version v2.0.50727). I have tried dot-net framework 3.5, 4 and 4.5 others give a re-targeting error.
I have tried to build the solution and project. Test explorer has the message `Build your solution to discover all available tests. Click "run all" to build, discover, and run all tests in your solution.
So the question is: How to I get visual studio to find the tests?
Have also tried to follow this: http://msdn.microsoft.com/en-US/library/ms379625%28v=VS.80%29.aspx but with no success: I get stuck in section getting started, when asked to right click and select create tests. There is no create tests.
I have this test(it compiles, but does not show up in test explorer):
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace tests {
[TestClass]
public class SimpleTest {
[TestMethod]
public void Test() {
Assert.AreEqual("a","a", "same");
}
}
}
I have now discovered (see deleted answer below) that it is because it is on a shared drive, but I don't as yet know how to get around it. (something about the security setting maybe).
I had same symptoms, but under different circumstances.
I had to add one additional step to Peter Lamberg's solution — Clean your solution/project.
My unittest project targets x64. When I created the project it was originally targeting x86.
After switching to x64 all my unit tests disappeared.
I had to go to the Test Menu -> Test Setting -Default Processor Architecture -> x64.
They still didn't show up.
Did a build.
Still didn't show up.
Finally did a Clean
Then they showed up.
I find Clean Solution and Clean to be quite useful at getting the solutions to play ball when setting have changed. Sometimes I have to go to the extreme and delete the obj and bin directories and do a rebuild.
Please add the keyword public to your class definition. Your test class is currently not visible outside its own assembly.
namespace tests {
[TestClass]
public class SimpleTest {
[TestMethod]
public void Test() {
Assert.AreEqual("a","a", "same");
}
}
}
This sometimes works.
Check that the processor architecture under Test menu matches
the one you use to build the solution.
Test -> Test Settings -> Default Processor Architecture -> x86 / x64
As mentioned in other posts, make sure you have the Test Explorer window open.
Test -> Windows -> Test Explorer
Then rebuilding the project with the tests should make the tests appear in Test Explorer.
Edit: As Ourjamie pointed out below, doing a clean build may also help.
In addition to that, here is one more thing I encountered:
The "Build" checkbox was unticked in Configuration Manager
for a new test project I had created under the solution.
Go to Build -> Configuration Manager.
Make sure your test project has build checkbox checked
for all solution configurations and solution platforms.
I have Visual Studio 2012 and i couldn't see the Tests in Test Explorer,
So I installed the following:
NUnit Test Adapter
That fixed the issue for me !
In my recent experience all of the above did not work. My test method
public async void ListCaseReplace() { ... }
was not showing up but compiling fine. When I removed the async keyword the test the showed up in the Test Explorer. This is bacause async void is a 'fire-and-forget' method. Make the method async Task and you will get your test back!
In addition, not having the Test project's configuration set to "Build" will also prevent tests from showing up. Configuration Manager > Check your Test to build.
Since the project is on a shared drive as the original poster have indicated. VS.NET needs to trust network location before it will load and run your test assemblies. Have a read of this blog post.
To allow VS.NET to load things of a network share one needs to add them (shares) to trusted locations. To add a location to a full trust list run (obviously amend as required for you environment):
caspol -m -ag 1.2 -url file:///H:/* FullTrust
To verify or list existing trusted locations run:
caspol -lg
A problem I've found is that tests don't get found in the Test Explorer (nothing shows up) if the solution is running off a network drive / network location / shared drive
You can fix this by adding an environment variable.
COMPLUS_LoadFromRemoteSources and set its value to 1
I had the same problem.. In my case it was caused by a private property TestContext.
Changing it to the following helped:
public TestContext TestContext
{
get;
set;
}
After cleaning and building the solution (as described in #Ourjamie 's answer), the test methods in the affected test class were available in the Test Explorer.
I ran into the same issue while trying to open the solution on a network share. No unit test would be detected by Test Explorer in this case. The solution turns out to be:
Control Panel -> Internet Options -> "Security" Tab -> Click "Intranet" and add the server IP address or host name holding the network share to the "Sites" list.
After doing this, I recompiled the solution and now tests appeared.
This should be quite similar to the answer made by #BigT.
Quick check list for solving some common test problems. Make sure that:
Test class and test methods are public
Test class has [TestClass] attribute
Test methods have [TestMethod] attribute
If this does not help, try cleaning, rebuilding solution and restarting Visual Studio.
I was getting the error: "Failed to initialize client proxy: could not connect to vstest.discoveryengine.exe."
Try to run Visual Studio as Administrator. That worked for me.
There is another Stack Overflow post discussing this error, and the same solution works for them. The question remains why this works.
I sometimes get the same symptoms.
What I did is:
1. Closed the Test Explorer window
2. Cleaned the solution
3. Rebuild the solution
4. Relaunched Test Explorer window from Test -> Windows -> Test Explorer.
And I got my test in Test Explorer window.
From menu bar on top...
Test -> Run -> All Tests
You may also view all tests from Test Explorer (Test -> Windows -> Test Explorer)
Further with VS 2012, if you miss out anything try searching it using Quick Launch bar on top right (Ctrl + Q) "Test"
Hope this helps.
I found the best way to troubleshoot this issue is to create a .proj msbuild file and add your unit test projects which you hare having an issue into this file and execute the tests using the command line version of mstest. I found a small configuration issue in my app.config which only appeared when running the tests from mstest - otherwise the test project built just fine. Also you will find any indirect reference issues with this method as well. Once you can run the Unit test from the command line using mstest you can then do a clean solution, rebuild solution and your test should be discovered properly.
In My case it was something else. I had installed a package and then uninstall it and reinstall an earlier version. That left a residual configuration/runtime/asssemblyBinding/dependencyIdentity redirecting in my app.config. I had to correct it.
I figured it out by looking at the Output window and selecting "Tests" in the drop down. The error message was there.
This was a pain... I hope it helps someone else.
This is more to help people who end up here rather than answer the OP's question:
Try closing and re-opening visual studio, did the trick for me.
Hope this helps someone.
I know this is an older question but with Visual Studio 2015 I was having issues where my newly created test class was not being recognized. Tried everything. What ended up being the issue was that the class was not "included in the project". I only found this on restarting Visual Studio and noticing that my test class was not there. Upon showing hidden files, I saw it, as well as other classes I had written, were not included. Hope that helps
I was experiencing this issue many times when I try to build the solution in a different PC.
I am using NUnit and Specflow as well. By default My test project targets X86 But I have to change this to X64.
Steps are
1. Test Menu -> Test Setting -Default Processor Architecture -> x64.
2. Clean Build
3. Build
4. If still tests didn't show up.
5. Go to Tools  Extensions and Updates Then Install NUnit and Specflow libraries
6. Clean Build
7. Build
Then usually test will showed up in Test Editor.
I've updated VS 2012 to the Latest Update . ie visual studio update 3.
That fixed the issue for me.
For Me the solution was just a little bit less complicated.
I had just brought an existing solution on to my machine (cloned from gitHub) and we do not track the auto-generated .cs files that Visual Studio created. (For each feature file there is a .cs file with the same name)
Opening the solution without having the associated .cs files actually allow me to navigate to the bound methods, so it appeared as if specflow was wired up properly, but I was not able to view the test names in the Test Explorer.
For this problem simply excluding the feature files from the project and then re-including them, forced VS to regenerate these auto generated codebehind files.
After that, I was able to view the tests in the test explorer.
I had this problem when upgrading my solution from Microsoft Visual Studio 2012 Express for Web to Microsoft Visual Studio 2013.
I had created a Unit Tests project in 2012, and after opening in 2013 the Unit Test project wouldn't show any tests in the tests explorer. Everytime I tried to run or debug tests it failed, saying the following in the output window:
Failed to initialize client proxy:
could not connect to vstest.discoveryengine.x86.exe
I also noticed that on debugging the tests, it was launching an instance of Visual Studio 2012. This clued me into the fact that the Unit Tests project was still referencing 2012. Looking at the test project reference I realised it was targeting the wrong Microsoft Visual Studio Unit Test Framework DLL for this version of Visual Studio:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
I changed the version number from 11.0 to 12.0:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
I rebuilt all and this fixed the issue - all tests were found in the Test Explorer and now all tests are found and running perfectly.
Check that your test project is not set to Delay sign only in your project properties -> Signing. If it is, deselect it and do a clean rebuild.
I hit the same problem while trying to open the solution on a network share in VS2013 Ultimate.
I corrected the problem by turning on
Control Panel -> Internet Options -> "Security" Tab -> Click "Local intranet", click on sites and make sure "Automatically detect intranet network" is ticked.
These are all great answers, but there is one more reason that I know of; I just ran into it. In one of my tests I had a ReSharper message indicating that I had an unused private class. It was a class I'm going to use in an upcoming test. This actually caused all of my tests to disappear.
Check referenced assemblies for any assemblies that may have "Copy Local" set to "False".
If your test project builds to it's own folder (bin/Debug for example) and the project depends on another assembly and one of those assemblies in the References list is marked Copy Local = "False", the assembly cannot load due to missing dependencies and your tests will not load after a build.
It looks like NUnit Framework 2.6.4 does not work well with NUnit Test Adapter. In the website it mentions the test adapter will only work with NUnit Framework 2.6.3.
This was my problem:
1. I had downloaded NUnit and NUnit Test Adapter separately through Nuget in the VS2012. Somehow NUnit got updated to 2.6.4 Suddenly i did not see my test cases listed.
Fix:
Uninstall Nuget and Nuget Test adapter
a. Go to Tools> Nuget > Nuget Pkg manager > Manage Nuget Pkg for Solution
b. List installed packages
c. Click manage
d. Un-check your projects
Install NUnit Test Adapter including NUnit 2.6.3 Framework
Clean/Rebuild solution
Open Test > Test Explorer > Run All
I see all the test cases
Hope this helps
None of the solutions here helped me. The tests wouldn't be discovered for one solution whereas another solution referencing the same projects worked fine. I finally solved this by deleting the solutionname.v12.suo file.
I had same issue, but a bit different.
I was using visual studio 2012. For some reason, only the tests of the initial generated file was running. But tests in another file were not running. Tried out different solutions posted here, did not work.
Finally I figured out that I had a private method in the test class which was the first method inside the class. I just moved the private method after a test method; so now, a method with [TestMethod] attribute is the first method inside the class. Strange, but now it works.
Hope this helps someone someday.
Tests do not like async methods. Eg:
[TestMethod]
public async void TestMethod1()
{
TestLib oLib = new TestLib();
var bTest = await oLib.Authenticate();
}
After doing this:
[TestMethod]
public void TestAuth()
{
TestMethod1();
}
public async void TestMethod1()
{
TestLib oLib = new TestLib();
var bTest = await oLib.Authenticate();
}
It saw the test.
Adding my answer as this is the top result on Google for this.
I'm using Visual Studio 2015 and (unknowingly - I just ran Install-Package NUnit) installed the NUnit3 package NuGet to my test project. I already had the NUnit Test Adapter extension installed, and my tests were still not showing up.
Installing the NUnit3 Test Adapter through Tools > Extensions and Updates fixed this for me.

NUnit debugging a single test

I use NUnit plugin from ReSharper. I can't find any way of debugging a single test. The BUG button always launches all the tests, even when I launch the debug specifically from one test method.
I'm trying to reach a breakpoint with one specific test and I don't want to reach it with the other tests.
Do you know any way of doing this? Google didn't help me on this one...
Example of my test code
[Test]
public void IsValidDoer_DoerValid()
{
var mockRepositoryDoer = new Mock<IDoerRepository>();
mockRepositoryDoer.Setup(c => c.ActiveDoers).Returns(activeDoers.AsQueryable);
var doerValidation = new DoerValidation(mockRepositoryDoer.Object);
Assert.IsTrue(dModel.IncludedDoers.Any());
}
[Test]
public void IsValidDoer_DoerInvalidNoQuota()
{
var mockRepositoryDoer = new Mock<IDoerRepository>();
var activeDoers = listDoers.ToList();
activeDoers.First().QuotaActivity.Clear();
mockRepositoryDoer.Setup(c => c.ActiveDoers).Returns(activeDoers.AsQueryable);
var doerValidation = new DoerValidation(mockRepositoryDoer.Object);
Assert.IsFalse(dModel.IncludedDoers.Any());
}
Yes Alongside the code is a green and yellow mark just click this and click run it will run that single test. You just left click it once you will get options and depending on what you also have installed from Jetbrains you could launch code coverage from here too.
You can also choose to append it to an already existing session of other tests or create it in a session all on its own.
Clarification:
Someone downvoted this so I went back and took a look and tested it both for MSTEST and NUnit. it is true it is not desirable to execute the 15 tests if you only wish to debug one. The test was conducted in visual studio 2015 with Resharper 10 and visual studio 2013 with Resharper 8. If you click on the mark in the individual test file it will indeed only run the code once.
If you run multiple tests and get a test session in Resharper's runner with three tests it will, on right click, show "debug tests", however if you only select one it only runs one test and so only hitting the code only once.
We also where having the same issue when using ReSharper 10 as the test runner. It would run all the tests, even if I configured only a single test in the session. Also it would run all tests when I used the right click on the test ball for this test.
After installing the NUnit3 Test Adapter under Tools->Extensions and Updates->Online I could debug the tests from the regular Test Explorer of Visual Studio 2015 by right clicking and selecting Debug selected tests. This does only run this one test :)
I had same issue. When trying to debug single test by clicking on the circle and selecting 'Debug (bug icon)' it would Debug for all unit tests.
I resolved it by upgrading Resharper. I upgraded from Resharper Ultimate 10.0.1 to 10.0.2.
Try to right-click the code of the specific test. You should see "Debug Unit Test" or something like this in the context menu.

Running multiple NUnit Projects in Visual Studio

I have a solution that has multiple projects including NUnit Test projects. So the solution looks like this (using generic names, these aren't the actual names):
+ Solution
+ Project1
+ Project1.Test
+ Project2
+ Project2.Test
+ Project3
+ Project3.Test
...
I would like to run all the NUnit Tests through the NUnit GUI or console application when I click 'Start Debugging' from within Visual Studio.
Right now, what I have done is added a new Class library called TestRunner and set it to be the StartUp project (I've read I don't really need to do this, I can just right click on the project and click 'Debug > Start new instance'). Then inside the project properties on the Debug page, I set the 'Start Action' to 'Start external program' and select the nunit-console.exe (Looks like nunit.exe GUI doesnt support multiple assemblies as input parameters). Then in the 'Command line arguments' I enter the path to each of the projects. Like this:
This seems to work OK, but I'm wondering if there is a better way to do this (maybe I don't need an extra project, or there might be an easier way to run multiple NUnit Test projects from within Visual Studio).
Any suggestions on improving this would be appreciated.
Running NUnit 2.5.9 and Visual Studio 2008.
There are various test runner extensions for Visual Studio itself - personally I use ReSharper (commercial) and also NCrunch (used to be free, now commercial), although the latter is more of a continuous test tool than a "run explicitly" tool. If you're using Visual Studio non-Express, you should really look at running tests integrated into the IDE - it's much, much nicer than switching between apps.
However, if you want to run the NUnit GUI, just set up an NUnit project configuration which includes all your test projects - you'll only need to do that once, then you can use it however you run the tests.

Categories