How to setup CodeCov, C#
As a newbie or experienced programmer, you might have faced some issues when setting up CodeCov, because I know I did. Being aware of how hard it can be, but still being able to do so (because I love DevOps personally) despite of that is great, but I’m also going to share how I did so!
Before we start, keep note that my project uses:
Property | Value |
---|---|
Language | C# |
Version control | GitHub* |
CI | AppVeyor* |
Testing framework | xUnit** |
* | This is what I’ll use, however, it doesn’t specifically matter |
** | You can use any |
Now let’s get started!
Setting up CodeCov
Considering you have a CodeCov account, visit codecov.io/<version control>/<username>/ – in my case; https://codecov.io/github/ANF-Studios/ – and once there, create add/import a new repository:
Alternaitvely, you can directly visit codecov.io/<version control>/<username>/<repository>.
Once there, you should see something like this:
Keep note you might need to activate the repository in its settings if necessary. You should also notice your token, you don’t really need it if your repository is public and your CI is any of Travis, CircleCI, AppVeyor, Azure Pipelines or GitHub Actions, otherwise, you do.
Sending reports to CodeCov
Perfect! We now have added our project to CodeCov, now, we need to set up AppVeyor. I trust that you already have a CI, so we’ll skip the adding a codecov project part. Oh and, it usually doesn’t matter what CI you use.
But before we jump into the CI directly, let’s generate those reports on our local computers so that we can get to see the results for ourselves. So first and foremost, we run our tests, but, we also need to tell .NET to generate an XML report, to do so, we’ll be using XPlat Code Coverage which will help us generate code coverage reports. For that, we’ll use this as our coverage collector:
|
|
Coverage settings
On running this, you’ll notice an xml file has been generated in Project_Root/TestsFolder/TestResults/Some GUID/coverage.cobertura.xml
. And codecov does require an xml file, however, this is not the format of the xml CodeCov expects from us. CodeCov uses OpenCover. Hence you require coverlet.console to be installed (citation needed). You can run dotnet tool install coverlet.console --global
.
We’ll need to tell our test command to generate a report for CodeCov, we can do so by creating a coverage settings file, usually called coverage.runsettings
, but I personally like to name it codecov.settings
or coverage.settings
. But how does it look like?
It’s in an xml format and this is the bare bones you’ll be needing:
|
|
See Coverlet’s documentation for more information.
Generating reports
I like to store this runsettings
file in the tests directory, but you don’t need to do the same. Once the file is there, you can run
|
|
And voila! You have generated your report. Granted that you have unit tests covered, you shouldn’t have any errors when sending your report to codecov.
By far, the easiest way to send your report to CodeCov is using their console. You can run
|
|
But there’s a catch here, the cli will detect your project root and set its working directory there and that there’s a guid folder randomly generated so it’s hard to get that directory, there are multiple workarounds for it.
If you’re on Linux, bash; you can do something like foo/**.xml
and get that file, but I’m not much of a Linux person. However, if you’re using PowerShell (Core), you can enumerate into it:
|
|
For command prompt:
|
|
Final result
And now if we send our report:
Fun fact, my desktop wallpaper at the time of taking that image is this:
And now, if we check, our code coverage is reported:
It’s there and every statistic is there! And that leads us to our conclusion! But since this article is about sending reports using AppVeyor, we shall write a small script to do so, but first, don’t forget to push your settings!
|
|
This is the yml file if you’re interested. You can modify it for your needs. That said, this brings us to our conclusion, I hope you found this helpful!