Generating code coverage from integration tests with dotCover
Tags: csharp, dotnet, testing, tutorial
Published: 2020-01-31 20:32:12

Sometimes your integration tests aren't written in the same language as your application (especially e2e tests) and it gets hard to collect code coverage for the main application.  I currently have this exact issue where I have an application written in C# using dotnet core 3.1, and I have my integration tests written in JavaScript.  It's really easy to get the code coverage for unit tests in C#, but it becomes a little harder when my tests aren't written in C#.


That's where dotCover comes in. 

**NOTE: dotCover is not free to use. They do have a trial, but otherwise you need a [ReSharper Ultimate subscription](https://www.jetbrains.com/resharper/buy/#personal?billing=yearly) for personal or commercial use.** I'm personally a huge fan of JetBrains and already subscribe to their full suite of products.


First we'll [download dotCover](https://www.jetbrains.com/dotcover/download/#section=commandline), and place it in our path.  It will work in both Windows and Unix environments, and all the commands I will write here are for Windows.  After extracting the file, I'm going to add to my path using the `Edit System Environment Variables`. If you set it up successfully, you should be able to run `dotcover` in PowerShell and see this output. If you're running on Unix, you might have to add `+x` to the `dotCover.sh` and you might need to call it from the terminal as `dotCover.sh` instead of just `dotCover`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9rhkdrt86mnh66esgn2c.png)

Now that we have access to run `dotcover`, we can start our application with it and test against or (or just run against it to potentially see where we have dead code).

All the console commands [can be found here](https://www.jetbrains.com/help/dotcover/dotCover__Console_Runner_Commands.html). 

First off we're going to start our set our application up to get ready to start with this command.

`dotcover cover --targetExecutable="bin\Debug\netcoreapp3.1\win-x64\YourApplicationName.exe" --output="report.html" --reportType="HTML" --startInstance="1"`

Let's go over the args one at a time. 

`--targetExecutable` is the executable you want to test against.  For me this is my Web Api. 

`--output` is just that, it's the name of the file to open the coverage report. For now, `report.html` will suffice.  

`--reportType` is also pretty simple.  For me I like the pretty HTML it outputs so that's what I use.  Here's the rest you can use `HTML|JSON|XML|DetailedXML|NDependXML`. 

`--startInstance="1"` is going to set our application to be run by `dotcover` with the identifier 1.  This is so that if we need to something before we start our application, we can.  For me, I need to set up my JavaScript tests, so that's what I do here, but you can do whatever you need.

Now that our instance is prime and ready, we're ready to start our application via `dotcover`.  This is done by the `send` command.   

So we'll run `dotcover send --Instance="1" --Command="Cover"` which will start our app.

`--Instance="1"` is denote which instance we're sending it to

`--Command="Cover"` is to tell it to start

Next we can start our tests that run against our app.  For me this would be a simple `yarn start`

After that's over we need to tell the runner that it's done and to output the files.  All we have to do is send another command to our instance with `dotcover send --Instance="1" --Command='GetSnapshotAndKillChildren"`

It should say that it send the command successfully, and you should have your outputs!


![Alt Text](https://media.giphy.com/media/4xpB3eE00FfBm/giphy.gif)