Can I debug while running a VS Unit Test? - c#

I want to unit test a user component which use custom events. when doing this without using VS Unit test Framework debug.assert succeed, when doing the same thing with VS Unit Test Framework, assert fails this is illogical so I want to debug while doing Unit test with VS framework. But It seems I can't because code never pause at debug point that I set.
So is there a way to force it to pause at debug point in that case ?
Update: I mean not in the Test Project itself but in the Targeted Project while running the Test Project.

Yes, with the Visual Studio Test project, you can set breakpoints and step through your code.
Be sure to run your tests with the Debug menu -- Tests -> Debug -> Tests in Current Context.
For any given test class or test method, use the keyboard shortcut Ctrl-R Ctrl-T.
When your cursor is in a method (either a test method, or the code covered by the test), it'll break on any breakpoints in that method, or any breakpoints anywhere in the code execution path. If your cursor is on the class, or in between test methods, it'll run all tests in the class. Breakpoints will be hit here as above.

The answer by P. Campbell is not exactly right. If you start debugging a project (such as a WCF services site), it is impossible to start any unit tests, debug mode or not. The options to do so are simply grayed out in Visual Studio 2012.
This means you simply cannot debug out-of-process code from a unit test. You can only debug code that was directly called in process by the unit test.
This is a major problem in VS2012 that they need to fix now.

In VS2015, select:
Test->Debug->All Tests
Or you can highlight a specific test in the editor and select
Test->Debug->Selected Tests

Under the Test Explorer -> Right click on the specific test case you want to debug -> From the Context Menu click Debug the selected test case option.

Related

Unit Testing without a main static method?

I am starting to go through a tutorial on Unit Testing. I am using Visual Studio 2019 and I am trying to run the projects/ tests but keep getting these errors,
CS5001 Program does not contain a static 'Main' method suitable for an entry point
CS006 Metadata file 'C:(filepath)... .exe' could not be found
I know neither class has a main static method but the tutorial I am following along with doesn't either so I am not sure what the issue is. I have searched Stack Overflow and Google but can only find answers going over my head, any idea what how to fix this?
It is pretty simple code as I am just at the beginning so I think the problem is something else but am not sure (obviously)
UPDATE:
Here is a screen shot of the structure of my solution:
structure of solution
The original project was a Console application, the I added a Test project to the solution.
Test project are not applications and are normally run using a test runner.
Run you tests from VS's Test Explorer
Option B. Run the test directly from its code.
It sounds like you've written your unit tests inside a Console Application template, removed the Program class's Main() method and hit F5 to run your unit tests.
You don't F5 unit tests, you run them from a test runner. Right-click any of your test methods and choose something like "Run Unit Tests" to show that test runner. Or click Test -> Test Explorer to see all discovered tests. See also Run unit tests in "Get started with unit testing".
If that works, create a new project that's actually a Unit Test Project so you at least get a fancy project icon, and move your test code into that project.

SpecFlow with NUnit: SetUp method runs twice

I installed SpecFlow and SpecRun recently on top of NUnit. I had a bit of trouble with references and Nuget packages but finally my tests run again. But this time whenever I run test (SpecFlow feature) my TestBase [SetUp] method once it reaches end runs again, resulting with opening browser window again. Test runs to the end with second attempt. Any one had similar problem?
I was checking solutions that point to PDB files as I see this popping up in Debug window but non seemed to work. Also, in Immediate Window I see this: Step into: Stepping over non-user code
I'm running test under recent version of SpecFlow v2.1.0 and NUnit3.21 against WebDriver v2.53.
For future reference. NUnit and SpecFlow hooks are mutually exclusive. Make sure you run your tests with attributes specific for providers you want to run test with.

Tests succeed when run from Test View but fail when run from test list editor or command line

I have a test suite, comprised of both unit tests and integration tests, in a project using C# on .NET 4.0 with Visual Studio 2010. The test suite uses MSTest. When I run all tests in solution (either by hitting the button in the testing toolbar or using the Ctrl-R A shortcut chord) all of the tests, integration and unit, pass successfully.
When I either attempt to run the same tests from the command line with mstest (explicitly using the only .testsettings file present) or attempt to run them from the Test List Editor or using the .vsmdi file the integration tests fail.
The integration tests test the UI and so have dependencies on deployment items and such, whereas the unit tests do not. However, I cannot seem to pin down what is actually different between these two methods of running the tests.
When I inspect the appropriate Out directories from the test run, not all of the files are present.
What would cause some of the files that deploy correctly in one situation from Visual Studio to not deploy correctly in another?
The static content started being copied shortly after I wrote the comments above. The other major issue I ran into was that the integration test project referenced libraries that were dependencies of the system-under-test (with copy-local set to true) in order to ensure that the DLLs would be present when they were needed. For some reason, these stubbornly refused to copy when the tests were run through Test List or mstest.
What I eventually did to work around it was include [DeploymentItem] attributes for the DLLs that I needed. This got things working no matter how the tests were run. What I am still unclear on, that may have answered the underlying solution, or provided a better solution, is how Test View/mstest differ from the regular test runner (assuming that the correct .settings file was passed to mstest.). I'm putting these notes/workarounds in an answer, but I'll leave the question open in case anyone can address the underlying cause for how the different test execution paths differ.

how to debug with xUnit?

I'm learning xUnit and so far, have found it to be a most useful tool. It's making me rethink some of my coding tactics to TDD instead.
However, I've come across an interesting problem. My test case is failing. No real concern there, but how do I debug it?
Specifically my test case is failing due to a "out of index" error, or something similar. It's NOT failing at the assert statement. What I need now, is some way to run the test case with the Visual Studio debugger active so that I can see the status of the different variables.
I'm not going to post code, as this situation is bound to come up again. Does anyone have any idea HOW to debug the test case itself?
Almost forgot! I'm using,
Visual Studio 2010 Ultimate (Dreamspark license)
xUnit 1.9
My workflow involves using the xUnit GUI runner to run the tests.
If what I'm asking is impossible, can someone suggest an alternative test suite I could use that has what I want?
In VS2015 and later, install the xunit.runner.visualstudio NuGet package. Then debugging is as easy as right-clicking on the test in the test explorer window. (Test-->Windows-->TestExplorer if you can't see it). You can also right-click anywhere in the code of the test and Run Test and Debug Test will be in the context menu. If your test is not showing up, be sure the class and method are both public.
I've not tested this but you should be able to attach visual studio to the xUnit GUI and debug from there.
From the Debug menu select 'attach to process', locate the name of the executable in the list and click attach. Set breakpoints in the unit test as required and run the test from the GUI. The breakpoint should be hit as expected.
I have failed in implementing all of the above, but the following worked for me:
Before the lines where you want to debug add the following line (then run the test):
System.Diagnostics.Debugger.Launch();
The drawback is that it will launch another instance of VS :).
Cheers!
In visual studio 2017, make sure that solution configuration is under 'Debug' mode. Under 'Release' mode it is not debugging.
The following will work in VS.NET and in SharpDevelop.
Open the test project's properties and go to Debug tab:
Under "Start Action" set "Start external program" to the xUnit runner
executable of choice.
Under "Start Options" set "Command line arguments" to the name of
your project's debug DLL.
Also set "Working directory" to the project's "bin\Debug\" directory.
Then select Debug > Run or press F5 to run your test in debug mode. Breakpoints will be hit.
The advantage of doing your debugging this way is you don't have to attach to the xUnit GUI each time, you just need to run your test project.
See the answer to this question: Stepping through and debugging code in Unit tests .
Essentially:
...go to 'Test' in the main menu of VS..., click submenu 'Debug' . . .'.
It also works for me using VS2017 ;)
Update for 2020:
If you're using Visual Studio Code with OmniSharp, you can click the "Debug Test" text above the method.
If you have resharper, with X-unit contrib extension installed (seriously recommended!), right click the class in visual studio and click "debug unit tests".
Simple!
set a break point inside of your method.
from visual studio Menu bar, click on Test.
from Debug click on Selected Test or All Test.
I tried all of the above and had no success.
The thing that worked for me is from the following post:
https://github.com/OmniSharp/omnisharp-vscode/issues/1630#issuecomment-317797607
In the .csproj file, under PropertyGroup add the following:
portable
Here's what I did, I just set the breakpoints, and from the menu from the top I choose "Test > Debug All Tests" or "Test > Debug last run"

How do I run NUnit in debug mode from Visual Studio?

I've recently been building a test framework for a bit of C# I've been working on. I have NUnit set up and a new project within my workspace to test the component. All works well if I load up my unit tests from Nunit (v2.4), but I've got to the point where it would be really useful to run in debug mode and set some break points.
I've tried the suggestions from several guides which all suggest changing the 'Debug' properties of the test project:
Start external program: C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe
Command line arguments: /assembly: <full-path-to-solution>\TestDSP\bin\Debug\TestDSP.dll
I'm using the console version there, but have tried the calling the GUI as well. Both give me the same error when I try and start debugging:
Cannot start test project 'TestDSP' because the project does not contain any tests.
Is this because I normally load \DSP.nunit into the Nunit GUI and that's where the tests are held?
I'm beginning to think the problem may be that VS wants to run it's own test framework and that's why it's failing to find the NUnit tests?
Edit: To those asking about test fixtures, one of my .cs files in the TestDSP project looks roughly like this:
namespace Some.TestNamespace
{
// Testing framework includes
using NUnit.Framework;
[TestFixture]
public class FirFilterTest
{
[Test]
public void Test01_ConstructorTest()
{
...some tests...
}
}
}
...I'm pretty new to C# and the NUnit test framework so it's entirely possible I've missed some crucial bit of information ;-)
Final Solution: The big problem was the project I'd used. If you pick Other Languages -> Visual C# -> Test -> Test Project ...when you're choosing the project type, Visual Studio will try and use it's own testing framework as far as I can tell. You should pick a normal C# class library project instead and then the instructions in my selected answer will work.
When I need to debug my NUnit tests, I simply attach to the NUnit GUI application nunit-agent.exe using "Debug|Attach to Process" and run the tests from the GUI. Any breakpoints in my tests (or the code they're testing) are hit. Am I misunderstanding your question, or will that work for you?
I use the same technique as you are trying Jon, without the /assembly flag, i.e.
Start External Program: C:\Program Files\NUnit 2.4.8\bin\nunit.exe
Command line arguments: "<path>\bin\Debug\Quotes.Domain.Tests.dll"
Does TestDSP.dll contain all your TestFixtures?
As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance
Simply remove the line that looks like
<ProjectTypeGuids>
{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>
from your project file.
This line basically tells VS.Net that it's a Test project, thus the "Cannot start test project". FYI here the 1st Guid says "it's a test", the 2nd says "it's C#".
For information on those Guids: http://www.mztools.com/Articles/2008/MZ2008017.aspx
In addition to the answer provided by #Justin here are some more details for NUnit 2.6.
Using NUnit 2.6 attach to nunit.exe or nunit-console.exe and NOT the agent. The configuration noted by #Justin is slightly different. Below is an example from nunit.exe.config (same for nunit-console.exe.config).
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- Comment out the next line to force use of .NET 4.0 -->
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0.30319" />
</startup>
For .NET 4 test project, to get break points to hit, you will have to comment out or remove the v2.0 line as the comment suggests. Once I did that I was able to debug the .NET 4.0 test project.
If you are using NUnit 2.4 or newer 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.
In Nunit 3.0.1 (I'm using VS2013), Open from main menu > Test > Windows > Test Explorer. Then in "Test explorer", right-click the test case, you might see:
Hope this helps.
Install TestDriven.NET, which is a plugin for Visual Studio
From there you can right click on your unit test assembly and click Run Tests to run the whole suite, right click on a TestFixture class to run just the tests in that class, or right click on a Test method to run just that method.
You also have the option to Test With Debugger, if you need to breakpoint into your tests in debug mode.
Try NUnitit - a open source Visual Studio Addin for Debugging NUnit Test cases
HomePage - http://nunitit.codeplex.com/
Remove ProjectTypeGuids from the project file.
Now with pictures:
Run NUnit gui (Download 2.6.2 from here) then go to File -> Open Project
Select your test .dll from bin folder (C:\......\[SolutionFolder][ProjectFolder]\bin\Debug\xxxxTests.dll)
Go to Visual Studio Debug -> Attach to process (Attach to process window will open)
From the list scroll down and select nunit-agent.exe then click Attach
At this point breakpoints in your tests should turn ripe red (from hollow).
Click Run on Nunit Gui and you should get your breakpoint hit...
Hope this saves you some time.
If you are able to get the console / or GUI working, but your breakpoints are not being hit, it may be because your app is running a different .NET runtime than NUnit is. Check to see if your nunit-console.exe.config / nunit.exe.config has the runtime specified.(The configurations live in the same directory as the nunit exe's.) Specify the runtime using the startup node:
<configuration>
<startup>
<supportedRuntime version="4.0" />
</startup>
If project path contains spaces e.g. "New Project" in path <path>\bin\Debug\New Project\Quotes.Domain.Tests.dll then enclose the Start Option --> Command Line Arguments project path in double quotes.
I spent a lot of time to figure this out.
Regarding what Mr. Patrick McDonald said
As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance
I tried to apply for my test class library but got some error regarding the path, so I tried to remove the 'Command Line Arguments', and luckily it worked well and as expected.
It sounds like you are trying to use the wrong library. NUnit can only start if the dll you are using contains TestFixtures.
+1 on TestDriven.Net. I've had the chance to use it a number of times.
You can download the personal version for evaluations purposes according the the license at http://testdriven.net/purchase_licenses.aspx.
I got the same error with MSTest. I found that in the Test Output window, some of the tests had duplicate IDs and could not be loaded. I removed all duplicate tests and now I was able to run the tests when i start the project.
There is also an extension now "Visual NUnit" that will allow you to run the tests from within Visual studio much like the build in test framework handles. Check it out its in the extension manager.
Open Visual Studio ---> your Project---> Select 'Properties'---> Select 'Debug' --> Select 'Start external program' and set the path of your NUnit there(Eg: Start external program = C:\Program Files\NUnit 2.6.2\bin\nunit.exe) ---->Save
After setting this just click Debug
For me solution was to adapt nunit configuration file. To use nunit with 4.5-.Net framework and x64 build option, I had to add one line to startup tag (supported runtime-version).
<startup useLegacyV2RuntimeActivationPolicy="true">
<!-- Comment out the next line to force use of .NET 4.0 -->
<supportedRuntime version="v4.0.30319" />
</startup>
Afterwards, I could start by right-click on the Testproject Debug -> Start new instance.
Before, I needed to again manually attach the project to the process.
My Debug properties were,
C:\Program Files (x86)\NUnit 2.6.4\bin\nunit.exe
with argument of the location of the .dll to be tested.
More information: nunit for testing with .NET 4.0
See if this helps..
How to add NUnit in Visual Studio
(RighteousRant)Although personally I don't like this approach.. If you need a debugger while you are test-driving your code, it's a "smell" in that you do not have enough confidence/know how your code works & need the debugger to tell you that. TDD should free you from needing a debugger if done right. Use 'Attach debugger to NUNit' only for rare cases or when you are wading in someone else's code.

Categories

Resources