Code Review Videos > Learn C# > C# Run Tests Continuously

C# Run Tests Continuously

One thing JavaScript’s Jest testing framework makes ridiculously easy is the ability to run your tests every time you save a file.

Coming from JavaScript / TypeScript over to C#, I really wanted a similar feature.

I’ve not yet found a way to do this from the command line, but when using JetBrains Rider IDE, I have come up with something that is good enough for my current needs.

In order to make this work you will need the dotCover plugin. The only way to get this, as far as I am aware, is to buy the dotUltimate package, or the All Products Pack. This isn’t cheap, admittedly. I appreciate I am fortunate to be able to get access to this (yes, I do pay the full price) as it is a necessity for my work.

Assuming you have access to dotCover, here’s what you need to do:

Note here that I’m using the forthcoming / currently beta new UI change for Rider.

If you’re not then this is under File > Settings.

From there, expand out the Unit Testing > Continuous Testing section:

Set up your options accordingly, I’m not saying mine are best.

If you do not see the Continuous Testing section then almost certainly you do not have the dotCover plugin enabled.

I think if you don’t have this enabled then you need to highlight it, click Enable, and then the IDE will need to be restarted.

Once you have the plugin enabled and your IDE restarted, the only other thing I think you have to do is open the tests pane and check / select the Autostart Tests on Save: Run All Tests feature:

What should happen now is every time you write some code or change your tests, the entire test suite re-runs. Pretty cool.

Whilst I think that’s the minimum required, there are some more things I’ve done to make for a nicer Developer Experience.

C# Automatic Code Formatting On Save

Aside from Jest, another library I use on every JavaScript / TypeScript project I create or work on is Prettier. For those unfortunates who have never experienced the majesty of Prettier, it is an opinionated code formatter that magically (well, via clever AST cleverness) turns your code into a lovely, consistently formatted joy.

Unfortunately, I don’t think it works for C#, and I don’t think there is a C# equivalent.

But I might be wrong, because I am but a newb.

So instead, I am relying on the IDE for the things I would otherwise rely on Prettier. Here’s what I have:

Step one is to ensure that the code clean up process runs whenever I save the project. This is pretty much just me hammering ctrl + S every so often, and hoping the output looks good.

I was wondering exactly what the Full Cleanup entailed, and by prodding around for long enough I discovered it meant this:

But what it misses, and what annoys me perhaps more than it ought to, is that it doesn’t clear up blank lines. And I often bash in some blank lines just to trigger Prettier the code formatter.

I found Rider can do this though:

Those are the formatting extras I am using.

Of course the problem with this approach is that it only applies to me.

If I am working on a project with another developer, they may not have the same formatting opinions as I do. Prettier solves this by ensuring all developers working on the project run Prettier, and that it uses the same config for everyone so all code hitting the Git repo is formatted to the same standard.

At least, that’s the theory. It still requires a bit of extra sauces to make that work in practice, but let’s not go down that rabbit hole.

If you know a better way to achieve this goal, please do leave a comment and let me know how you do it. I’m not convinced on this approach, but it’s the best I am currently aware of.

Covered Code

Code formatting on save is super nice. But whilst digging into that I found something I definitely do not get in JavaScript / TypeScript land.

And that is being able to show code coverage directly inside WebStorm – the JetBrains IDE I use for JS / TS projects.

I discovered that in Rider you can enable the line highlighting feature provided by dotCover to show which lines are covered by tests directly inside the IDE.

This doesn’t work automatically as far as I can tell.

What this requires if that you run the test suite with coverage. This can be done from inside Rider:

And once that is done you get a really nice read out in the gutter as to which lines are covered:

You can meddle with these options – see the Highlighting screenshot above. If you select Line background then literally the lines are coloured green. I didn’t like that, it was too “in your face” for me. But still, better than having to dig into the HTML report like I’m used too.

Pretty cool.

Also, to save you a few seconds, I did take a look to see whether anything similar exists in WebStorm but it doesn’t appear so.

Is There A Better Way?

As I’ve said above, I am but a newb.

Is there a better way to do some or all of these tasks?

Personally I’d love an approach to formatting code that isn’t tied to Rider / my IDE.

And I’d love to be able to watch my code for changes directly from the command line, and run the tests via the command line, too.

But for now, this is pretty good.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.