What is Unit Testing? - Explanation & Meaning
Learn what unit testing is, how to test individual components with Jest, Vitest, or pytest, and why test coverage is essential for reliable software.
Definition
Unit testing is the testing of the smallest units of code, such as individual functions, methods, or classes, in isolation from the rest of the system. The goal is to verify that each unit works correctly according to its specification.
Technical explanation
Unit tests are the foundation of the testing pyramid and are executed most frequently. They should be fast, isolated, repeatable, and self-validating (FIRST principles: Fast, Isolated, Repeatable, Self-validating, Timely). Popular test frameworks include Jest and Vitest for JavaScript/TypeScript, pytest for Python, and JUnit for Java. Mocking replaces external dependencies (databases, APIs, file systems) with controlled simulations so tests only assess the unit under test. Spies monitor whether functions are called correctly. Test fixtures provide predefined test data. Test coverage measures what percentage of code is reached by tests: line coverage, branch coverage, and function coverage. Coverage of 80% is often considered good, but qualitative tests are more important than quantitative coverage. Parameterized tests run the same test with different input values. Snapshot testing compares output with previously captured references. Property-based testing generates random inputs to discover edge cases.
How MG Software applies this
MG Software writes unit tests for all business-critical code. We use Vitest for our TypeScript/React projects and pytest for Python services. Our CI/CD pipeline mandates minimum test coverage and blocks merges when tests fail. We combine unit tests with integration tests and end-to-end tests for complete system coverage.
Practical examples
- A unit test verifying that a price calculation function returns correct results for regular prices, discounts, VAT, and edge cases like zero values or negative amounts.
- An isolated test of an authentication service using a mock database, so the test runs fast and does not depend on a real database connection.
- A CI/CD pipeline executing 500 unit tests in under 30 seconds on every commit, catching bugs immediately before code is merged.
Related terms
Frequently asked questions
Related articles
What is an API? - Definition & Meaning
Learn what an API (Application Programming Interface) is, how it works, and why APIs are essential for modern software development and system integrations.
What is SaaS? - Definition & Meaning
Discover what SaaS (Software as a Service) means, how it works, and why more businesses are choosing cloud-based software solutions for their operations.
What is Cloud Computing? - Definition & Meaning
Learn what cloud computing is, the different models (IaaS, PaaS, SaaS), and how businesses benefit from moving their IT infrastructure to the cloud.
Software Development in Amsterdam
Looking for a software developer in Amsterdam? MG Software builds custom web applications, SaaS platforms, and API integrations for Amsterdam-based businesses.