Fail builds based on test coverage history (The Boy Scout Rule)

I’d like to abort builds where the test coverage declined. This way I hope to encourage developers to Leave the code better than they found it (The Boy Scout Rule). Any thoughts on how to implement this?

I can easily extract the build coverage using some shell scrtip, but I’m not aware of any API to store history metrics and fetch this during a build.

It is certainly possible. There’s a bit of wiring you’d need to do, but here is how I would do it.

Firstly, stating the obvious, you need a test runner capable of producing a percentage coverage value. Sounds like you have got that already. Then you need a way of storing this. One easy way to do this is to create a new Git repo specifically for coverage, and in your build commit a new value each time. You can then use Git commands to fetch the previous value. This would use the same repo SSH keys as your project.

Alternatively, you could use the CircleCI cache. The cache key could be the branch name, so branches are allowed to reduce coverage for the first commit, or if you want to be strict, have a fixed cache key so that any branch must increase coverage.

The Git approach is perhaps a bit “untidy” at first glance, but it has the advantage that it is easier to debug - you can use your code host to explore your coverage history.

Either way, you might want to implement a tagging system that allows you to escape this check. If you have an emergency hotfix to write, the last thing you want to deal with is a build system that won’t let you deploy your fix. Maybe a tag of no-coverage-check or something like that.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.