How much of our code is being covered by our unit tests? Are there areas we are not testing? By capturing code coverage data during a test run, we can analyze which areas of our applications are well-tested and which ones require additional tests to be written. And where better to capture code coverage information than on our build server?
In this webinar we will use dotCover to collect code coverage information while running tests in our CI process. We will see how we can configure code coverage and how we can use the TeamCity Visual Studio plugin to download the coverage snapshot generated on the build server and inspect it using dotCover on a developer machine.
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
.NET Code Coverage for Continuous Integrationusing TeamCity and dotCover
1. .NET Code Coverage for
Continuous Integration
using TeamCity and dotCover
Maarten Balliauw
Technical Evangelist
maarten.balliauw@jetbrains.com
@maartenballiauw
2. Who am I?
Maarten Balliauw
Belgium (Antwerp)
Technical Evangelist, JetBrains
Focus on web
ASP.NET MVC, Windows Azure, ...
PHP, ...
Big passion: Cloud (Windows Azure)
Home brewer
http://blog.maartenballiauw.be
@maartenballiauw
6. Code Coverage
“Code coverage is a measure used to
describe the degree to which the source
code of a program is tested by a particular
test suite.”
Wikipedia - http://en.wikipedia.org/wiki/Code_coverage
8. Holy grail?
Code Coverage is just a tool
Verify # of statements executed during a test run
100% coverage != 100% bug free / well-tested code
It’s relatively easy to cover all lines of code
It’s much harder to test them correctly
Should we aim for 100%?
We should care about code coverage!
Identify parts of code not covered by any type of test and reduce chance of
defects
9. dotCover
Capture which statements have been executed (using profiler)
During unit test
During manual test
Display coverage percentage
Statement level coverage visualization
Hotspots to quickly identify parts that need testing
Filtering, exporting, ...
Navigate between code and test
Command line runner
11. When to perform code coverage?
Let’s counter that with a question:
When to run unit tests?
On developer machine is always nice, but…
Continuous Integration is our view of the truth
Compile, Run tests, Code coverage, …
12. TeamCity
JetBrains Continuous Integration Server
Project
Logical name/unit
Build Configuration
Something we want to run
Build Steps
Various steps we want to run (like compilation or unit tests, .NET, Java, deployment, …)
Build
Execution log and artifacts of a build configuration after run
Integrated build step for running NUnit + code coverage using dotCover
14. TeamCity Service Messages
Test resuls from xUnit were reported using TeamCity Service Messages
http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-
Reporting.NETCodeCoverageResults
##teamcity[testStarted name='testName' captureStandardOutput='<true/false>']
##teamcity[testFinished name='testName' duration='<test_duration_in_milliseconds>']
We can report coverage using this technique as well!
##teamcity[importData type='dotNetCoverage' tool='dotcover' path='coverage.xml'
Now all we need is way to run tests with coverage...
15. dotCover Command Line Runner
dotcover.exe
Create configuration (which executable, which assemblies, …)
Run dotcover.exe with that configuration
Export to XML, HTML, …