GIVEN my article WHEN a reader reads it THEN the reader writes better tests

Jamie Read on March 22, 2019

GIVEN a developer who hates writing tests I'm not one for testing, that's evidenced by my most used side project having around 5% test... [Read Full]
markdown guide
 

I like the idea of given/when/then but I don’t like to use it in Java JUnit e.g. because it’s not support as semantic or syntactic part of the test framework. I’m really opposed to use comments especially together with text explaining what the next lines do.

Some JavaScript test frameworks support it and especially languages that support meta programming.
I want the description to be part of the tests. Otherwise they will be obsolete very quickly...

 

This, like many things requires buy in from your team; otherwise, as you say, the comments will be obsolete quickly.

You need to be able to trust that your team will check for things like this in code review so that you can avoid the exact situation you described. If there isn't that trust then this will probably become more of a distraction and hinderance.

It's worth saying though, since the tests test behaviour, the comments shouldn't change that frequently otherwise your system behaviour is changing which will probably then be breaking backwards compatibility. Sure the exact lines of code may change with a refactor etc. but the behaviour described in the comments should ordinarily be static, and if not then obvious how they should be changing buy the commit message etc.

 
 

If you didn't use BDD before you should take a look at the SpecFlow Framework. Have fun.

 

That's exactly the framework the last test is written in!

Writing SpecFlow tests like that one was one of the first steps I took in adding tests for this side project. It took a little while to get the framework up and running but I knew that I actually could enjoy writing these forms of tests so it gave me that initial boost moving forward.

I still need to port the tests over to the .NET Core version. Unfortunately I can't get it working on CI until then!

 

This is great! I'll have to try this out with our team and see it in action.

 

Do let me know how well it works for you!

code of conduct - report abuse