NicoTek
NicoTek

Reputation: 1167

Code Coverage of a Selenium Test

I have installed OpenCover through nuget.

This is the line I'm running from my Command Prompt

C:\myapp\Main\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"C:\Program Files (x86)\NUnit 2.6.4\bin\nunit-console.exe" -register:user -targetargs:"C:\myapp\Main\myapp.SeleniumTests\bin\Debug\myapp.seleniumTests.dll /result=c:\TestRunner\SeleniumResults.xml /include:Single"

I can see that the test runs because FireFox opens up and it goes through all the necesary steps. but once its done I get "Visisted Classes 0 of 309"

I have tried adding

-searchdirs:"C:\myApp\Main\MainWebProject\bin"

but still it comes back with no coverage.

What am I missing?

Upvotes: 0

Views: 327

Answers (2)

NicoTek
NicoTek

Reputation: 1167

Ok I was able to make it run. It's important that Visual Studio is closed, if not it will have trouble registering the site from iis express

first in the console I go to

cd c:\MyApp\Main\MainWebProject\Bin

the script came out to:

C:\MyApp\Main\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -target:"C:\Users\Me\Desktop\TestRunnerWithServer.bat" -register:user -searchdirs:"C:\MyApp\Main\MainWebProject\bin"

the file "TestRunnerWithServer.bat" has the following steps

del "C:\TestRunner\TestResults.xml"
del "c:\TestRunner\SeleniumResults.xml"

call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86

rem go to the folder of the project with the unit tests
CD C:\MyApp\Main\MyTestsProject\bin\Debug

rem Run Unit Tests
MStest /testcontainer:MyTestsProject.dll /resultsfile:C:\TestRunner\TestResults.xml

rem now start IIS Express in a different window
cd C:\Program Files (x86)\IIS Express\
start iisexpress.exe /site:MainWebProject /config:C:\MyApp\Main\.vs\config\applicationhost.config

CD C:\Program Files (x86)\NUnit 2.6.4\bin

rem Run Selenium Tests
nunit-console C:\MyApp\Main\MyApp.SeleniumTests\bin\Debug\MyApp.seleniumTests.dll /result=c:\TestRunner\SeleniumResults.xml /noshadow

rem Close IISExpress
taskkill /IM iisexpress.exe

Upvotes: 0

Shaun Wilde
Shaun Wilde

Reputation: 8368

OpenCover only provides coverage of the .net assemblies that are loaded and executed by the target process and it's child processes.

I suspect your web application is not running in that context and as such even though your tests are running OpenCover is not actually profiling that code. So you need to run your web application in the same context - I recommend using iisexpress for this.

Now you may be thinking about the test assemblies and why aren't you getting coverage of those assemblies well should try using the /noshadow option with nunit-console.

Upvotes: 1

Related Questions