Introduction to Unit Testing

Learn the basics of unit testing, how to structure your first test, and which parts of your code to test first.

Introduction to Unit Testing

If you write code, you’ve probably heard a thousand times that you need to write unit tests—and maybe felt that familiar mix of guilt and laziness. This guide is for anyone who has never written one: no jargon, no dogma, just the essentials to understand why testing is worthwhile and leave with your first test running.

Imagem de abertura com a metáfora da rede de segurança para testes.
Imagem de abertura com a metáfora da rede de segurança para testes.

What a unit test is (and isn’t)

A unit test is a small piece of code that automatically checks whether a small part of your program does what it’s supposed to do. "Small" is the key word: usually a function or method, isolated from the rest—with no database, network, or interface.

What it isn’t: it isn’t opening the application and clicking around to see whether it works (that’s manual testing), and it isn’t testing the entire system from end to end (that’s another type of test). A unit test targets the brick, not the building.

Why write tests if the code already works?

That’s the honest question every beginner asks. The answer: tests aren’t meant to prove that the code works today—they’re meant to let you know when it stops working tomorrow.

Think of them as a safety net. Without one, every code change is a gamble: did I break something somewhere else? With one, you make a change, run the tests, and know within seconds. That’s what gives you the confidence to refactor and the speed to grow without fear. The time you “lose” writing the test, you recover with interest the first time it saves you from a bug in production.

Test anatomy: arrange, act, assert

Ilustrar os três passos preparar-agir-verificar.
Ilustrar os três passos preparar-agir-verificar.

Almost every test follows three steps, an easy pattern to remember:

  1. Arrange: set up the scenario—the input data and the object you’re going to test.
  2. Act: call the function you want to check.
  3. Assert: verify that the result is what you expected.

If you can describe your test in these three sentences before writing it, half the work is already done.

Your first test, in practice

Imagine a simple function that adds two numbers. The test asks: “if I pass 2 and 3, does it return 5?” You arrange the values (2 and 3), act (call the function), and assert (is the result 5?). If so, the test passes; if not, it raises an alarm—and points to exactly where the problem is.

The liberating detail: your first test will almost always seem too simple, testing something obvious. That’s fine. The goal is to learn the rhythm—arrange, act, assert—with something simple, and then apply it to real code.

What to test first (and what to ignore for now)

Ilustrar priorizar o que testar (regras de negócio e bordas).
Ilustrar priorizar o que testar (regras de negócio e bordas).

You don’t need to—and shouldn’t—test everything right away. Start with what hurts most when it breaks:

  • Business rules: calculations, validations, and decisions. This is where bugs are costly.
  • Edge cases: empty lists, zero values, and unexpected input. This is where code often stumbles.

And leave these for later: trivial code (a getter that only returns a value doesn’t need a test), and things tightly coupled to a database or network—those call for other types of tests, not unit tests.

Common mistakes beginners make

Here are two classic pitfalls you can avoid from the start:

  • Testing too much at once. A test should verify ONE thing. If it fails, you want to know immediately what broke—not search through ten assertions.
  • Testing the “how” instead of the “what.” Check the result, not the internal steps. If you tie the test to implementation details, any harmless refactoring will break it—and then the test becomes a nuisance instead of a safety net.

In the end, unit testing is less about “ensuring quality” in the abstract and more about something concrete: sleeping peacefully after a deployment. Write your first test today, even if it seems silly. The second will feel more natural—and before long, changing code without a safety net will seem like what it has always been: jumping without a parachute.

Did you enjoy this article?

Share it with your friends and help spread knowledge!