NUnit not working on Visual Studio 2010? - c#

I'm new to NUnit and I just download it today and I can't get it working with visual studio. (I'm new to Visual Studio as well)
I'm following TekPub's Mastering C# 4.0 tutorial.
Screenshot: Something is missing within the red circle area. There should be some "green thing" on the side that let you double click on it and run the NUnit test.
http://dl.dropbox.com/u/20422001/NunitMissing.jpg
As you can see on the above screenshot, the NUnit seems not working....
Did I miss something? Please advice. Thanks
EDIT
This is how it SHOULD look like. I took a screenshot from the video tutorial:
http://dl.dropbox.com/u/20422001/nunit.jpg
and here is the project files:
http://dl.dropbox.com/u/20422001/MasteringCSharp.rar
As you can see, I don't have "Unit Test" Tab and "Unit Test Session"...

You aren't doing anything wrong. Out of the box, NUnit doesn't integrate directly with Visual Studio. The screenshot from the video appears to be using Resharper's test runner.
Typically testing with NUnit requires you to compile your test project and then load it into the NUnit GUI (nunit.exe) where the tests can be run from there. When using the NUnit GUI, the project will reload anytime the assembly is recompiled. (I sometimes prefer having this in a separate process as it doesn't tie up Visual Studio's UI, but that's a matter of preference.)
The added advantage of using a third party test runner like Resharper, TestDriven.net, Galileo, etc is that it allows you to run or debug the tests directly from within the IDE. You can accomplish the same thing using NUnit by configuring Visual Studio to attach to NUnit and debug your tests from the external process. This post shows you how.

I haven't seen that particular video, but it sounds like you need an NUnit test runner for Visual Studio.
Check out the following:
Visual NUnit 2010 - free!
TestDriven.net
ReSharper

That my friend is JetBrains' Resharper - it's a paid add-in to Visual Studio that allows you to run tests from within the IDE. Highly recommended if you're working for a prolonged period in VS.
Of course since you're just starting out, you could use the NUnit GUI to run your tests. You can also set it to run tests after each build.
Tools > Settings > Test Loader > Assembly Reload. Check "Re-Run last tests run". Apply changes

Related

Running unit tests from a command line

I'm working on a suite of application that interact with many websites and api's. I have created unit tests that test this code.
As it's beyond my control, I want to know as soon as sites / api's change and the code stops working so I can fix and would like to run the tests periodically against the assemblies running. How can I run the tests outside of VS?
I'd like the option of testing on Linux mono too.
You can use MsTest from the command line:
Example:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\mstest" /testcontainer:C:\MyTestProject\bin\debug\MyTestProject.dll /resultsfile:C:\TestResults\results.trx
(Full list of command line switches on the link above)
This is set to Visual Studio 2015, for 2017 just make sure you have the right version number, I believe 15.0
MSTest has been deprecated, you'll want to invoke VSTest from the command line

Visual Studio 2013, run one test separated via shortcut

I hope some one has an idea why Ctrl+R, T is not working in VS2013 anymore.
Previously (VS2012) when I hit the shortcut, and I had my cursor in a test, it only runs the test.
At the website of Microsoft it says exactly how I should expect it should work.
It says:
Ctrl + R, then press T
This runs the tests in the current scope. That is, it runs the
current test method, all the tests in the current test class, or
all the tests in the namespace, respectively.
Source: http://msdn.microsoft.com/en-us/library/vstudio/ms182470.aspx#RunTestsFromTestProject
However in VS2013 it runs all the tests in the current file.
When I use the Test Explorer it runs correctly the only selected test...
I have the following extensions installed:
Productivity Power Tools 2013
Xunit.net runner for Visual Studio 2012 and 2013
Chutzpah Test Adapter for the Test Explorer
Chutzpah Test Runner Context Menu Extension
Ok, its a bug in the xUnit testrunner.
I tested with NUnit and it works as expected.
On xUnit.net is a bugreport.
Link: http://xunit.codeplex.com/workitem/9901
Edit:
Issue is solved in version 0.99.2

Turn visual studio project testfixtures into nunit dll

I have a visual studio project. I later added a .cs file for testing and have added the appropriate 'using' statements and [Test] and [Testfixture] attributes. When I open the nunit gui application, however, it only accepts .dll, .exe, and .nunit files. I was wondering what exact steps I need to take to execute the tests I wrote. Is it possible to do so directly in visual studio?
You should add the NUnit dll as a reference on your project.
JetBrains' Resharper is able to run the tests inside Visual Studio. Read more about Resharpers unittesting here. JetBrains has also developed a line coverage tool: dotCover.
Right now, Resharper is the best solution.
Test Driven .Net is the best free solution I know of.
Visual Studio 11 will finally allow for 3rd part testing framework plugins.

How to debug with vs.net 2008 and nunit?

I am running vs.net 2008 with nunit runner.
If I set a breakpoint in the unit test code, it just runs the unit test in the runner.
I'm guessing I have to integrate nunit with vs.net somehow?
My unit tests are just a class library project, I cannot run the tests in vs.net 2008, I can only do it via the runner currently.
If you want to debug in your current setup (running nunit runner that has your assemblies loaded), do the following:
Run nunit.exe
Open (or create) your nunit project (File > New/Open Project
Under Tools > Settings, click IDE Support > Visual Studio and check 'Enable Visual Studio Support'
Once your nunit file is setup and you are ready to run, go to VS and click Tools > Attach To Process...
Verify that 'Managed Code' is selected in the 'Attach to' area
Find unit.exe, select it, and click Attach
Place desired breakpoints in the library
From the nunit GUI, click Run
If youre debugging the correct assemblies, it should hit your breakpoint.
Ideally, I would change it to run nunit as an external program and supply your .nunit project file as the input. See here for more details.
I am running vs.net 2008 with nunit runner.
If I set a breakpoint in the unit test code, it just runs the unit test in the runner.
(emphasis mine)
You just answered your own question. Why would you expect the Visual Studio breakpoint to magically push you into Visual Studio when you don't run the code via Visual Studio in the first place?
I would recommend refactoring your tests so that as many tests as possible can be run through Visual Studio (see http://www.nunit.org/index.php?p=vsSupport&r=2.2.10 for details on NUnit support for Visual Studio), and then only run the remaining tests on the console.
To gain debugging unit tests with VS 2008, do the following:
1) Create project in NUnit runner and add your dll with unit tests into it.
2) Set up "Start external program" in your unit test project - NUnit exe file.
3)
When you debug set up your unit tests project as "startup
project" or whatever you have in VS2008.
4) Run

C# testing framework that works like JUnit in Eclipse?

I come from a Java/Eclipse background and I fear that I am spoiled by how easy it is to get JUnit and JMock running in Eclipse, and have that GUI with the bar and pass/fail information pop up. It just works with no hassle.
I see a lot of great options for testing in C# with Visual Studio. NUnit looks really nice because it contains unit and mock testing all in one. The trouble is, I can't figure out how to get the IDE display my results. The NUnit documentation seems to show that it doesn't automatically show results through the VS IDE. I found http://testdriven.net/, which seems to trumpet that is makes VS display these stats and work with multiple frameworks, but it isn't open source.
Is there anyway to get unit and mock testing working with the VS IDE like it does in Java with Eclipse?
On installing NUnit you get an NUnit.exe - use this to open and run your tests. It has an UI and shows pass/fails and shows output.
You can add a build action in Visual Studio that on a specific testing configuration will build, then immediately invoke NUnit on that dll.
EDIT: (more details)
In test project:
Project Properties -> Debug (set a build configuration - I use "NUnitDebug")
Start Action -> "Start external program": C:\Program Files\NUnit 2.5.3\bin\net-2.0\nunit.exe (use your own path)
Start Options -> Command line arguments: MyTestProject.dll (replace with the name of your DLL)
EDIT2: As brendan said, Moq is a good mock framework that can be used.
Resharper will let you do this and has a nice UI. I believe the core of it is NUnit. For the mock stuff you'll want to use Moq.
Resharper is not free/open source but is so worth the price.
If you are looking for something like Eclipse/JUnit, you shouldn't have tried Microsoft product line.
But the good news is that SharpDevelop has such nice integration with NUnit and it is open source. However, it aims as an alternative to VS, not an addon for VS.
You could read ASP.NET MVC Test Framework Integration Walkthrough and run your tests from the VS test runner.
Have you tried using the Testing projects in Visual studio? They're practically identical to nUnit, and can be run simply by hitting F5.
For mocking, chose whichever suits you, We're looking at Moq for Silverlight support.

Categories