Remove the verb “Should” from your unit test descriptions please
I just feel like sharing my opinion about a somewhat trivial, minor thing in programming. I never really understood why so many projects have unit tests which are labeled as “Should do x” or “Should not be y”. The unit test performs a certain action, and has an assertion to test if the outcome matches your expectation.
Example class and method in Javascript:
class Foo {
bar(){
return "baz";
}
}module.exports = Foo;
Javascript unit test with Jasmine syntax
let foo;
describe("Foo", function(){
beforeEach(function(){
foo = new Foo();
}); describe(".bar", function(){
beforeEach(function(){
result = foo.bar();
}); // I USE THIS
it("returns 'baz'", function(){
expect(result).toEqual("baz");
}); // WITH SHOULD IT LOOKS LIKE THIS
it("should return 'baz'", function(){
expect(result).toEqual("baz");
});
});
});
I think all the “should do x” stuff messes up your reports as well.
Javascript unit test run console output
Foo
.bar
v returns 'baz'
v should return 'baz'
I think the explicit names, which describe actually what is performed, makes much more sense. When it fails, you directly see what the test performed. Even though it is a minor change, I think it really gives people a better feeling of what they are testing and it just seems semantically make more sense.
I also had a bit of discussion with someone who had a strong disagreement with my approach, can’t remember the reasons unfortunately. Please let me know in the comments what your thoughts are. Happy coding!