Tests That Help you Find Defects Faster

Rate this content
Bookmark

This talk is about common mistakes people make when writing tests.


Mixing multiple concerns inside tests is tempting because it can feel like painting the whole picture. However, it obfuscates the root cause when a test fails. Setup methods are great but when developers are too focussed on keeping their tests DRY they can easily lead to test interdependence. Therefore, some principles we have learned to build our software we need to unlearn when it comes to testing.


The talk highlights more aspects like bloated tests which make it hard to figure out what they are about and proper usage of assertions to get better error messages.

Especially if you don't work with TDD it can be easy to come up with a test that looks good but stands in your way when it fails.


The talk will have a look at the four scenarios I outlined above, explain why it makes sense to think about them and actionable suggestions how to improve tests.

This talk has been presented at TestJS Summit 2021, check out the latest edition of this JavaScript Conference.

FAQ

The main focus of Philip's talk is on effective testing practices in software development, particularly on how to write tests that help identify defects faster and more efficiently.

Philip discusses different styles of writing test names and descriptions, specifically comparing Rspec and BDD (Behavior Driven Development) styles. He emphasizes that the choice between them is a matter of personal preference.

Philip advocates for TDD because it helps organize development work in small, manageable steps and ensures testing is integrated throughout the development process, leading to higher quality software.

Philip highlights the common mistake of cramming multiple use cases into a single test. He suggests that each test should cover exactly one use case to avoid confusion and increase clarity on what is failing and why.

Philip suggests using assertions that provide more context, such as those that check for specific properties or expected error messages. This approach helps developers understand what went wrong without needing to dig through the code.

Philip does not adhere to strict rules regarding the number of assertions per test case. He believes that whether a test uses one or multiple assertions should depend on what works best for the specific scenario.

Philip advises against overusing the DRY (Don't Repeat Yourself) principle in tests if it compromises test isolation. He notes that each test should operate independently to avoid failures caused by shared state between tests.

Philip recommends spiking into unknown code to understand it better and then rewriting the tests from scratch to ensure clarity and maintainability.

Philipp Giese
Philipp Giese
21 min
19 Nov, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk covers tests that help find defects faster, focusing on test case assertions, improving test failure context, test code structure, and the dangers of extracting code in tests. It emphasizes the importance of small tests, test isolation, and using TDD. The benefits of TDD and testable automation are discussed, along with setting up an engineering workflow and the use of mocking. Overall, the talk provides valuable insights into writing effective tests and ensuring code quality.

1. Introduction to the Talk

Short description:

Welcome to my talk on tests that help you find defects faster. I'm Philip, CTO at Oution, and I've learned from my own mistakes in writing tests. I'll start by discussing what this talk won't cover, such as naming tests and the number of assertions in a test case. Both Rspec and BDD styles are equally good, it's a matter of personal preference.

Hey there and welcome to my talk, tests that help you find defects faster. My name is Philip, but everyone just calls me Phil. So you can do this as well. I live in a town called Potsdam in Germany, right next to Berlin. But except for hipsters, we do have castles. I'm also CTO at a company called Oution, and I've been in technical leadership roles the past years. And I've mentored a couple of less experienced developers along the way.

And while doing so, I, you know, learned about different mistakes different people make about, you know, some troubles people put themselves through when they write tests for software. And since I enjoy writing tests a lot, I'm a huge fan of TDD, because it just helps me, you know, organize myself and you know, work in small steps. I thought this is a nice opportunity to share some of that knowledge. Obviously I've made most of those mistakes myself in the past years, often enough to, you know, kind of dissect them and look into what's really important and whatnot. Which brings me also to my first point. So I'd like to start this talk actually not with some topic, but with the topics that this talk won't be about. And the first thing will be naming tests, right? So there are a couple of different styles, how you can write test names, test descriptions. And just to give you an example, I'm just gonna run those here so you see how they look in a test runner. That's for instance one style called Rspec, where essentially you read through all the describes down to the it, and then this forms a whole sentence. So for instance, here a user can be identified. This is one way of doing it, obviously, right? There is also a different style, BDD, Behavior Driven Development, this is where you see the keyword should a lot, so here I've used user merely as a name for a group. And then the test name should be possible to identify a user. Now, the important point here is, you know, both are equally good, right? There is none... The one isn't better than the other. It's just a matter of personal taste, what you like better, what works for you. This is also why I don't wanna make this an issue here. So in this talk I'm gonna use the BDD style a lot, should, but, you know, I don't think it's the better one if you prefer to write your test differently. That is absolutely fine, so this is really not what this talk is about.

2. The Importance of Test Case Assertions

Short description:

This talk is not about arbitrary rules for the number of assertions in a test case. There are pros and cons to using one or multiple assertions. It's important to find an approach that works for you. The examples used are intentionally simple to illustrate the main points.

The second thing this talk isn't about is about certain arbitrary rules when it comes to how many assertions should be in one test case, right? There are linked rules out there that say every test should only have one assertion in it and I don't think it's necessarily always, you know, correct. So for instance, you know, these two are exactly the same. The first one uses one assertion where, you know, we wanna assert that a user object has certain structure so we can use match object to match against all those properties. And if something should be missing then it fails, but we could write the exact same test also with two assertions where we check for the properties individually. There are probably, you know, there are pros and cons to either of this approach, and either of these approaches, and but I wouldn't say, you know, one is particularly better or one is worse. So also here, you know, this is not something I'd like to talk about. Figure out something that works for you and go with that, right? And obviously…so what this talk is also not about is like, hopefully, is nitpicking my example. So I've chosen deliberately simple examples to, you know, to get the general idea across. They are obviously not from the real world, right? You probably wouldn't find them exactly like that in a real world scenario, sometimes they might even contradict certain rules that I say, but then always just to, you know, to get the point that I'm currently talking about better across.

QnA