Unit testing, test coverage and quality appeared in several of the backend adverts we read. Tests are small programs that check your code does what you expect. They let you change code without fear, and they show an employer that you care whether things actually work, not just whether they ran once on your laptop.
Why tests matter for backend work
Imagine you change how amounts are rounded. Without tests, you would have to click through every payment screen to check nothing broke. With tests, you run one command and see in seconds whether every money calculation is still correct. On a team, tests also run automatically on every pull request, so nobody merges a change that breaks something.
Kinds of tests
- Unit tests check one small function, such as
normalisePhoneordailyTotal. - Integration tests check several parts together, such as calling an API endpoint and checking what it saved in the database.
- End to end tests drive the whole system like a user would. They are slower, so teams have fewer of them.
Most of your tests should be fast unit and integration tests.
A unit test
With a test runner such as Vitest or Jest in JavaScript (or pytest in Python):