# Unit Testing Fundamentals

> Write fast, focused tests for your logic with a test runner, assertions and sensible use of test doubles. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/backend/unit-testing
Last updated: 2026-11-28

Write fast, focused tests for your logic with a test runner, assertions and sensible use of test doubles.

Beginner · about 6 hours · difficulty 2/5

A **unit test** checks a small piece of logic (a function, a class) in isolation, runs in milliseconds and gives precise feedback when something breaks. Unit tests are the base of a healthy test suite.

### What to learn

- A test runner for your stack: **Vitest** or **Jest** (Node), **pytest** (Python), `go test`, **JUnit 5** (Java), **xUnit** (.NET), **RSpec** (Ruby).
- The **Arrange, Act, Assert** structure and descriptive test names ("returns 0 discount for orders under 50").
- **Test doubles**: stubs return canned data, mocks verify interactions, fakes are lightweight working implementations (like an in-memory repository). Prefer fakes and real objects over heavy mocking.
- **Coverage** as a flashlight for untested code, not a target to game.

### Design for testability

Keep business logic in pure functions or classes that receive their dependencies (dependency injection) instead of importing a database client directly. Then logic is trivially testable, and I/O is tested separately.

### Pitfalls

- Testing implementation details, so every refactor breaks tests.
- Mocking everything, which makes tests pass while the real system fails.
- Flaky tests that depend on time, randomness or ordering. Inject clocks and seeds.

You are ready to move on when your project's core business rules are covered by fast unit tests that run on every save and read like a specification.

## Key concepts

- **Test runners (Vitest, Jest, pytest, JUnit)**
- **Arrange, Act, Assert**
- **Stubs, mocks and fakes**
- **Dependency injection for testability**
- **Code coverage**
- **Avoiding flaky tests**
- **Property-based testing**

## Mini project

Extract the pricing or discount logic from one of your projects into a pure module and cover it with at least 15 unit tests, including edge cases and a fake clock.

## Check yourself

What is the difference between a stub and a mock, and why can too many mocks make a test suite less trustworthy?

## Free resources

- [The Practical Test Pyramid](https://martinfowler.com/articles/practical-test-pyramid.html) (Martin Fowler)
- [Vitest guide](https://vitest.dev/guide/) (Vitest)
- [pytest documentation](https://docs.pytest.org/) (pytest)

Previous: [Managing Secrets and Configuration](https://www.internhack.in/roadmaps/tracks/backend/secrets-management) · Next: [Integration and End-to-End Testing](https://www.internhack.in/roadmaps/tracks/backend/integration-e2e-testing)

## More in Testing and Shipping Continuously

- [Unit Testing Fundamentals](https://www.internhack.in/roadmaps/tracks/backend/unit-testing)
- [Integration and End-to-End Testing](https://www.internhack.in/roadmaps/tracks/backend/integration-e2e-testing)
- [Test-Driven Development](https://www.internhack.in/roadmaps/tracks/backend/test-driven-development)
- [Continuous Integration and Delivery](https://www.internhack.in/roadmaps/tracks/backend/ci-cd)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
