Skip to content
BoKSA

Testing Fundamentals

Testing Fundamentals

Automated tests exist to prevent regressions (a change breaking something that used to work), to document behaviour (a test shows what the code is supposed to do), and to enable safe refactoring (you can change how code works internally and immediately know if you broke anything). All readings on this page are from Aniche, M. (2022) Effective Software Testing, Manning.

The test pyramid

  • Unit tests — small, fast, isolated tests of one function or class in isolation.
  • Integration tests — test that multiple components work correctly together (e.g. a controller and a service, or a UI component and a mocked API).
  • End-to-end (E2E) tests — test a full user flow through the real (or a realistic) system; slower and more brittle, so used sparingly.

Read about the test pyramid and where to focus your testing effort:

Writing a unit test

The standard structure is arrange-act-assert: set up the situation, perform the action being tested, then assert the expected outcome. Good test names describe behaviour ("returns an error for an empty username"), not implementation ("test1").

Good test code is also maintainable: tests should be fast, independent of each other, isolated from external state, easy to write, and easy to read. When tests break these principles, they become slow, flaky, or hard to change.

Read about the principles of maintainable test code:

Mocking

A unit test should test one thing in isolation, but most code depends on other parts: a database, an API, another class. Mocking replaces such a dependency with a test double — a stand-in you control — so the test stays fast and only fails when the code under test is wrong.

Read about test doubles, mocks, and when to use them:

Test-first thinking and coverage

A test-first (TDD) mindset means writing a test before, or alongside, the code it verifies, using the test to clarify what "correct" means before you build it. Code coverage measures which lines or branches of code were executed by your tests — a useful signal, but not a guarantee: 100% coverage only means every line ran at least once, not that the assertions checking it are any good.

Read about code coverage and how to use it the right way:


Starting Points