Barbarian Meets Coding
barbarianmeetscoding

WebDev, UX & a Pinch of Fantasy

3 minutes readdev-talk-monday

Dev Talk Monday: Stop Hating Your Tests With Justin Searls

Dev Talk Monday is the series that brings you awesome dev talks every Monday… or Thursday XD

As you might know I love unit testing and TDD. I have been a little bit worse than usual at it in the last months though. Hence I said to myself, let’s look at a dev talk and get my test inspiration back up. And so, here it is, this awesome talk about how to stop hating your tests by the very charismatic justin searls.

How to Stop hating your tests is a great and thorough compilation of tips and tricks in writing maintainable tests. The talk contains a great number of useful recommendations about how to make your test a true aid to your project and not a constant source of pain and frustration.

These are some of the tips I enjoyed the most:

Forego big objects in your code. Big objects equal big tests equal pain. Tests are a great barometer of how good your code is, if it is painful to test, then there’s a big chance that your code is badly designed.

Be consistent. With tests, being consistent trumps creativity and DRY-ness. You want to help the reader understand as soon possible what is what your are testing, what are the conditions in which you are testing and what are the expected results. Use familiar patterns like AAA or GivenWhenThen and make them very visible and easily skimmed.

Using stuff like GivenWhenThen consistently gives you an even better way to diagnose the quality of your code:

You've got a lot of GIVEN steps? Maybe you have too many dependencies in your subject or too complex arguments...

If it takes more than one WHEN step then it is probably the case that your API is too confusing or hard to invoke, there's something awkward in how you use that object.

And if you have too many THEN steps then your code is probably doing too much.

I also found out about the jasmine-given library that gives you a more Rspec-y API to write your tests in a GivenWhenThen approach. This is pretty appealing to me because I went from writing C# tests like this:

[Test]
public GivenContext_WhenSomethingHappens_ThenExpectedResult(){
    // Arrange
    // Act
    // Assert
}

To writing tests using jasmine like this:

describe("Given that.....", () => {
       // could have some arrange here
   describe("And ....."), () => {
       // could have some arrange here

     it("Should ..... When .... ", () => {
        // Arrange
        // Act
        // Assert
    });
  });
});

When I could use jasmine-given and write them in this much more readable fashion:

describe("assigning stuff to this", function() {
  Given(() => {...}); // Arrange

  When(() => {...}); // Act

  Then(() => {...}); // Assert
});

I am soooo gonna check this project out. :)

The thing that I loved the most about this talk though, is that it proposes a very thoughful way of testing when you really question every detail and every decision of your testing practice. Which is pretty much what we all should do.

And there’s a ton of more of good stuff: the squint test, a discussion about expressiveness in test frameworks, how inconsistencies in your tests convey misleading meaning, an improved test pyramid, removing redundant code coverage, thoughful mocking, small feedback loops, judge your test frameworks by their feedback and not only their APIs, etc… Watch the talk!!

If you liked it, follow justin on the twitterverse, watch this other talk on targeted test suites and this other one on discovery testing or outside-in TDD.

And Have a great day!


Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García