Unit Tests Should Expose Bugs

This learning hour is about designing unit tests, including good assertions.

Learning Goals

Session Outline

Connect: Three facts

Why do developers write automated unit tests? Give me three facts about it.

Concept: Unit Test Design

Briefly explain the most important things about test design. For example:

A unit test should fail if the thing you are testing has a bug in it. We are trying to achieve Self Testing Code. When a test fails you want to understand as quickly as possible what is wrong. Show a failing test in a test runner. Point out how to find the test name, the failure message, and how to navigate to the line of code responsible.

Make sure they have access to the documentation for their test framework. Point out the documentation for how to assert for equality and assert that an exception is thrown.

Concrete - without AI tooling

Write some test cases for some existing code that has well-marked bugs in it. For example, CalcStats. Make sure to explain what the code is supposed to do as well as that it also contains bugs that are not marked.

Instructions:

If they complete CalcStats1, they could continue with CalcStats2, which should solve exactly the same problem, but it has different bugs.

Concrete - with agentic AI

Conclusions

What is important to remember when in unit test failure messages? Explain the main idea