unit test
An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
In more detail
Each test calls the code with known inputs and asserts the expected result, so a later change that breaks it is caught immediately. Good unit tests cover ordinary cases, edge cases such as empty input, and error cases.
Examples
Component unit tests render a component and assert on its DOM, roles and behaviour, but cannot see layout or pixel problems.Go: func TestHealthy(t *testing.T) in a _test.go file, run by go testdef test_word_count():
assert word_count("a b c") == 3Related terms
- integration test — A test that checks several real parts working together, such as the service talking to a real database.
- functional test — A test that exercises a public journey the way a client would, by sending real requests and checking the observable results.
- mock — A test double that stands in for a real dependency and checks that the code under test calls it in the expected way.
- test suite — A collection of tests run together to check a piece of software.
- end-to-end test — A test that drives the whole system from the outside, often through a browser or public API, with every part running.
- test pyramid — A guide for balancing a test suite: many fast unit tests, fewer integration tests and a small number of end-to-end tests.
- linter — A tool that reads code or configuration without running it and flags likely mistakes and style problems.
- smoke test — A quick test that checks the most basic behaviour works after a deploy, before deeper testing or real traffic.
- Testing Library — A family of testing libraries that render components and query them the way users find them, by role, label and text.
- visual regression testing — Testing that takes screenshots of components and compares them pixel by pixel with approved baseline images to catch unintended visual changes.
- regression — A bug where something that used to work stops working, or works worse, after a change.
- test double — Any object that replaces a real dependency in a test: a stub returns canned data, a fake is a small working implementation, and a mock checks interactions.
- arrange, act, assert — A three-step shape for a test: set up the starting state, perform the one behaviour under test, then check the observable result.
- xUnit.net — A popular open-source unit testing framework for .NET, where [Fact] marks a test method and Assert checks results.
- test function — A Go function whose name starts with Test and which takes a *testing.T, so go test finds and runs it automatically.
- deterministic — Always producing the same result from the same inputs, with nothing left to chance or timing.
- table-driven test — A Go test style that lists many input cases and expected results as rows in a slice, then checks them all in one loop.
- JUnit — The standard testing framework for Java, which discovers methods marked @Test, runs them and reports each failure with the test's name.
- test runner — A tool that finds test files, runs the tests in them and reports which passed and which failed.
- pytest — A popular Python testing framework that finds test functions automatically and reports failures with detailed comparisons.
- assert statement — A statement, assert condition, that does nothing if the condition is true and raises AssertionError if it is false.
- test discovery — How a test runner finds tests without being told: pytest collects files named test_*.py and, inside them, functions named test_*.
- pure function — A function whose result depends only on its arguments and which has no side effects, so the same inputs always give the same output.
- type predicate — A return type written param is Type, which tells the compiler that when the function returns true, the argument has that type.
Taught in
- Unit And Integration Testing — Modern API Architecture: REST, GraphQL & gRPC
- Mocking Dependencies — Modern API Architecture: REST, GraphQL & gRPC
- CodeBuild: The Build That Passed With Failing Tests — AWS Certified DevOps Engineer – Professional (DOP-C02) Exam Prep
- Environment Protection and Manual Approvals — Continuous Integration & Continuous Delivery Pipelines
- Overlay Architecture: Portals, Stacking Contexts & Backdrops — Design Systems & Component Engineering
- Automated Accessibility Testing: axe-core & Testing Library — Design Systems & Component Engineering
- Visual Regression Testing: Playwright & Snapshot Differencing — Design Systems & Component Engineering
- Continuous Integration — DevOps & Site Reliability Engineering
- Testing Strategies And The Test Pyramid — DevOps & Site Reliability Engineering
- Manual Object Mapping — Modern .NET Core, C# & Enterprise Microservices
- Unit Testing with xUnit and NSubstitute — Modern .NET Core, C# & Enterprise Microservices
- Behavior-Driven Development with Reqnroll — Modern .NET Core, C# & Enterprise Microservices
- Full-Stack System Verification: Capstone Integration — Production Full-Stack Engineering
- The Standard testing Package — Concurrent Systems Programming with Go
- Serving HTTP with net/http Server — Concurrent Systems Programming with Go
- Containerizing and Testing the Final Build — Concurrent Systems Programming with Go
- Testing Basics With JUnit 5 — Modern Java: Virtual Threads & High-Throughput Services
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Tying It All Together — Modern Java: Virtual Threads & High-Throughput Services
- Native Test Runner: node:test & node:assert/strict — Node.js Internals & Backend Services
- Testing With pytest — Hands-on Python: Complete Video Course & Book
- The Test Pyramid And Where It Is Wrong — Automated Testing, TDD & Quality Engineering
- Integration Testing With Containers — Automated Testing, TDD & Quality Engineering
- Testing With Real Databases — Automated Testing, TDD & Quality Engineering
- Code Review As A Quality Gate — Automated Testing, TDD & Quality Engineering
- Why TypeScript: Contracts, Erased Types And Soundness — Production TypeScript
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
References
- Python docs: unittest
- Martin Fowler: UnitTest
- Testing Library introduction
- Unit testing best practices for .NET
- Testing in .NET
- Node.js: Test runner
- Package testing
- JUnit 5.14.4 User Guide
- pytest: getting started
- TypeScript Playground