After much frustration, I now have monocov running on my Travis-CI build, and I’m going to share with you how I did it.

TL;DR The coverage results don’t seem accurate just yet, but jump to the end to see the full script to get it going on your build.

As I mentioned in a previous post, I am building a coveralls.io publisher for .Net code coverage. This means (to me any way) that I need to support mono coverage as well. The best suggested coverage tool mentioned online for mono is monocov, despite being officially unmaintained since May 2011. But I stuck with it.

So I started building a sample library with CI on github and very easily got examples working for AppVeyor on windows. But I wanted to get monocov working as a sample with Travis-CI before I built support for it. I found pretty quickly that monocov is not installed on Travis-ci, and in fact there is no package available to install it either that I could find.

Thanks to github forks, I found someone else had forked and updated the source for monocov to work with a newer Xamarin version of the mono installation. So naturally I forked this fork so I could make my own changes if necessary.

Using a few tricks I was able to get my Travis-CI build to clone down the git fork, build it and install it on the build server. I did have to change a compile setting and set a variable based on this blog post, and I had to pull down the mono.cecil nuget package so I had the Mono.Cecil.dll file required by the C# side of monocov. I also made to install gtk-sharp2 for the GUI library to build with make, although I don’t use it. After all these steps I finally got it to a point where it was installed. The Travis-CI code ended up using these instructions in the install section:

- sudo apt-get install gtk-sharp2
- curl -sS https://api.nuget.org/packages/mono.cecil.0.9.5.4.nupkg > /tmp/mono.cecil.0.9.5.4.nupkg.zip
- unzip /tmp/mono.cecil.0.9.5.4.nupkg.zip -d /tmp/cecil
- cp /tmp/cecil/lib/net40/Mono.Cecil.dll .
- cp /tmp/cecil/lib/net40/Mono.Cecil.dll /tmp/cecil/
- git clone --depth=50 git://github.com/csMACnz/monocov.git ../../csMACnz/monocov
- cd ../../csMACnz/monocov
- cp /tmp/cecil/Mono.Cecil.dll .
- ./configure
- make
- sudo make install
- cd ../../csMACnz/Coveralls.net-Samples 

Note I changed directories a few times, and pulled monocov into a directory beside my actual repo build folder.

To get it to run over my tests, I had to use a couple of options:

  • set the load library path as seen in the blog post I mentioned earlier.
  • –profile=monocov:outfile=monocovCoverage.cov to get monocov to run and output to a specified file
  • ,+[GameOfLife],+[GameOfLife.xUnit.Tests] as part of the coverage to specify which assemblies to record coverage for
  • /noshadow so it didn’t use a temp directory for the results, since it has to locate the dll in step 2
  • run a second step afterwards to convert the *.cov file into a folder of detailed xml files

The resulting code for this looked like this:

- export LD_LIBRARY_PATH=/usr/local/lib
- mono --debug --profile=monocov:outfile=monocovCoverage.cov,+[GameOfLife],+[GameOfLife.xUnit.Tests] ./src/packages/xunit.runners.1.9.2/tools/xunit.console.clr4.exe ./src/GameOfLife.xUnit.Tests/bin/Release/GameOfLife.xUnit.Tests.dll  /noshadow
- monocov --export-xml=monocovCoverage monocovCoverage.cov

So whats the next steps? Well now I have actual results I will get coveralls.net support written and get it published to nuget and usable on my Travis-CI build, so it is passing. After that I will look at the accuracy of the existing code, since my initial results haven’t really looked that accurate.

In summary, and If you just skipped to the end, my full travis.yml file running monocov over my .net project on Travis-CI looks like this:

language: csharp
solution: ./src/GameOfLife.sln
install:
  - nuget restore ./src/GameOfLife.sln
  - chmod +x ./src/packages/xunit.runners.1.9.2/tools/xunit.console.clr4.exe
  - sudo apt-get install nunit-console
  - sudo apt-get install gtk-sharp2
  - curl -sS https://api.nuget.org/packages/mono.cecil.0.9.5.4.nupkg > /tmp/mono.cecil.0.9.5.4.nupkg.zip
  - unzip /tmp/mono.cecil.0.9.5.4.nupkg.zip -d /tmp/cecil
  - cp /tmp/cecil/lib/net40/Mono.Cecil.dll .
  - cp /tmp/cecil/lib/net40/Mono.Cecil.dll /tmp/cecil/
  - git clone --depth=50 git://github.com/csMACnz/monocov.git ../../csMACnz/monocov
  - cd ../../csMACnz/monocov
  - cp /tmp/cecil/Mono.Cecil.dll .
  - ./configure
  - make
  - sudo make install
  - cd ../../csMACnz/Coveralls.net-Samples
script:
  - xbuild /p:Configuration=Release ./src/GameOfLife.sln
  - export LD_LIBRARY_PATH=/usr/local/lib
  - mono --debug --profile=monocov:outfile=monocovCoverage.cov,+[GameOfLife],+[GameOfLife.xUnit.Tests] ./src/packages/xunit.runners.1.9.2/tools/xunit.console.clr4.exe ./src/GameOfLife.xUnit.Tests/bin/Release/GameOfLife.xUnit.Tests.dll  /noshadow
  - monocov --export-xml=monocovCoverage monocovCoverage.cov

Hope someone finds this useful, and stay tuned for the coveralls.io update.