How to run an NUnit test in VisualStudio with OpenCover code coverage? - c#

I am new to NUnit testing. I was able to write test cases for my functions, now what I want is to know the code coverage for my test cases. For that I use OpenCover. But I don't know how to install and get OpenCover to work with Visual Studio 2015, can anyone guide me to install OpenCover for Visual Studio and provide help to see the code coverage in Visual Studio?

The best solution I found for code coverage in Visual Studio is as follows:
Open Visual Studio 2017
Go to Tools->Extensions and Updates
Go to "Online", in the search (top right of the window) type
"AxoCover"
Click install, follow the instructions (i.e. close Visual Studio to
start the install)
Reopen Visual Studio
Build your project
Go to Tools->AxoCover
Under the "Tests" tab right click your top directory and go "Cover
tests"
Click on the "Report" tab

You need some VS extension to include the coverage results in VS.
One example:
https://visualstudiogallery.msdn.microsoft.com/6950a046-8919-4935-8542-c6f37956f688/view/
you need OpenCover installed/present in solution
you need NUnitConsole installed/present in solution
it does not support VS2017
NUnit3 supported only when you do a custom build from git (not in latest release)
I have nothing to do with this extension, it is just working for me. Use at your own risk :)
If you mean it and you can spent some $ go for http://www.ncrunch.net/

Related

No Colours in Specflow Feature Files on Visual Studio

I have the latest version of SpecFlow installed, and still the feature files display uncoloured, like so...
(Not enough rep to insert images)
Specflow uncoloured feature files
Is there certain configurations that are set for colours to be displayed in Specflow files?
VS2015
Specflow 2.1
There is an option for coloring syntax in Tools->Options->SpecFlow that could cause the problem.
EDIT:
Ensure your SpecFlow extension is installed correctly:
Go to Tools->Extensions and updates -> Installed look for SpecFlow for Visual Studio 2015. You might try to uninstall and install it again.
If you can't find in the list then select Online, in search type specflow, then select result and install.
Gherkin code highlighting issue on Visual Studio 2019
For Visual studio 2019 need to install an Extension
Extensions -> Manage Extensions -> Search for Specflow
Can see Extension by name 'Specflow for Visual Studio 2019' click and restart the Visual Studio 2019 app.
FIX for VS 2022
If this is still an issue, On update, the extension is removed.
Navigate to Extensions, Click Online, Search Specflow and install the extension.
Restart and modify
Fix is complete
In my case I had the coloring in some projects but in other I haven't. Pre-installing the Visual Studio extension worked for me.

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.

NUnit not working on Visual Studio 2010?

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

how to properly install stylecop?

Well i downloaded the newest version, then installed, checked to instal entire files on local drive. I restarted VS2010 and rerun it. Unfortunatelly i can't find in menu > tools anything with should be connected with stylecop ( as in tutorial "how to use stylecop"). I am using x64 Windows7.
I really really don't know how to to use it.
Look: I click inside code, white field and i see only this:
1) Download StyleCop from here
2) Close all Visual Studio instances
3) Install StyleCop
4) Open Visual Studio, Right click inside code file and you could see Run StyleCop Option
Are you using one of the Visual Studio Express products? If so, you will not see UI integration of StyleCop since the VS Express edition does not support integration of add-ins and packages like StyleCop.
If you are using Express, you could still run StyleCop, but this will require using either MSBuild integration or a pre- or post- build event.
It looks like it is possible to use StyleCop with Express edition. Here is how.

Missing code coverage tab in VS localtestrun.testrunconfig?

I am relatively new to unit testing and was attempting to add some code coverage to my unit tests. Yet i can't find the code coverage tab in Visual Studio 2008 in the localtestrun.testrunconfig, is there any missing Add-in or tool?
Thanks,
DMS
As I understand "Code Coverage" is available only in "bigger" editions of Visual Studio 2008. In other words, this feature is NOT available in the "Visual Studio 2008 Professional Edition".
Which edition of Visual Studio 2008 are you using ?
MSDN Link
Go to the menu Test, select Edit test run configuration for your current test run and check enable code coverage. Then perform a run and then on the test run results you can choose to display code coverage.

Categories