Key takeaway
- Unit testing explained: what a unit is, how it differs from other test levels, good test traits, frameworks, trade-offs and how AI agents change it.
Unit testing is a software testing method in which the smallest testable parts of an application, usually a function, method or class, are tested in isolation to confirm that each behaves as intended. Programmers write these tests themselves, using their normal tools and a unit testing framework. Martin Fowler notes that the term is loosely defined and that each team decides what counts as a unit (Fowler, UnitTest).
For a CTO or engineering head the question is less "should we write unit tests" than "what do we expect them to catch, and what must other test levels cover?" Unit tests are the fastest, cheapest check in the delivery pipeline, so they run first and often. They do not replace system testing or user acceptance testing, and they are the first line of defence for code written by AI coding agents as well as by people.
What counts as a unit, and what makes a unit test good?
Object-oriented teams often treat a class as the unit, and functional teams a function. Fowler describes two styles. Solitary tests replace a unit's collaborators with test doubles, so a fault elsewhere cannot fail the test. Sociable tests use the real collaborators. He keeps doubles for awkward cases such as remote services, where they remove non-determinism (Fowler, UnitTest).
Microsoft's .NET guidance lists the traits of a good unit test, according to its docs:
- Fast: it runs in milliseconds.
- Isolated: it has no dependency on outside factors such as a file system or database.
- Repeatable: it returns the same result when nothing has changed.
- Self-checking: it detects pass or fail without human interaction.
- Timely: it does not take disproportionately long to write compared with the code under test.
The same page advises naming a test after the method under test, the scenario and the expected behaviour, and keeping logic such as loops and conditions out of tests, using parameterised tests instead (Microsoft Learn, unit testing best practices).
How does unit testing differ from the other test levels?
Each level answers a different question, and a unit test only answers the first.
| Test level | Question it answers | Typical owner | Read more |
|---|---|---|---|
| Unit | Does this function or class behave as intended on its own? | Developer | This guide |
| Integration and component | Do several parts work together, for example code and a database? | Developer or QA | Database testing |
| System | Does the whole assembled product meet its requirements? | QA | System testing |
| Acceptance | Does the business accept the release? | Business users and QA | User acceptance testing |
| Smoke and sanity | Is a new build stable enough to test, and did a fix work? | QA and developers | Smoke vs sanity testing |
The test pyramid idea, described by Fowler, is to write many more low-level unit tests than high-level, GUI-driven ones, because UI tests are slower, costlier and more brittle. He treats high-level tests as a second line of defence and suggests reproducing a bug found there with a unit test before fixing it. He also notes the argument weakens if high-level tests are fast, reliable and cheap to change (Fowler, TestPyramid). Moving these checks earlier in the lifecycle is the theme of shift-left testing.
How do teams write and run unit tests?
Isolate the unit with test doubles
When a unit depends on a database, a network service or code not yet written, a double stands in for it. Microsoft's docs define a stub as a controllable replacement for a dependency, a mock as a fake that decides whether the test passes or fails, and fake as the generic term for either. Python ships unittest.mock with Mock, MagicMock and patch() for the same purpose (Python docs).

Write the test first, or straight after
In test-driven development the test comes before the code. Teams that do not work this way still write tests as each module is finished and run the suite on every merge. See our guide to test-driven development for the red, green, refactor loop.
Run the suite in CI on every change
Fowler describes a compile suite run while coding and a commit suite of all unit tests run in continuous integration. His point is that the suites must be fast enough that developers do not avoid running them. He cites a rule of thumb of no more than ten minutes for the commit suite, and says there is no absolute answer (Fowler, UnitTest).
Measure coverage, but read it carefully
Coverage shows which code the tests execute, not whether they assert anything useful. There is no universal target. Cover the critical logic and the paths most likely to break first, and see test coverage measurement for how coverage types differ.
Which unit testing frameworks should you consider?
Pick the framework your language community already uses. The table gives what each project says about itself and the date we read it (13 November 2026); confirm versions on the project page before standardising.
| Language | Framework | What its own page says |
|---|---|---|
| Java | JUnit | The repository covers the JUnit Platform, Jupiter and Vintage. It lists release 6.1.3 dated 7 August 2026. |
| Python | pytest | Small, readable tests, automatic test discovery, fixtures and detailed assert reporting. Needs Python 3.10 or later, or PyPy 3. |
| .NET | NUnit | A unit-testing framework for .NET languages, ported originally from JUnit. Version 3 is the current production release. |
| JavaScript | Jest | Tests use test and expect, run with npm test. The page says Jest is not supported by Vite and recommends Vitest as a Jest-compatible alternative. |
Older lists of unit testing tools often include coverage and commercial plugins. Treat those as separate decisions from the test framework, and check each vendor's own page before relying on a feature.
What should leaders weigh before expanding unit testing?
| Consideration | What to do | Trade-off |
|---|---|---|
| Cost of writing tests | Test the logic that matters, not every condition. | Time up front, less debugging later. The cost of a late defect is usually higher, but we cite no figure. |
| Over-mocking | Use real collaborators where they are stable and fast, as Fowler allows. | Heavy mocking can pass while the real system fails. |
| Slow suites | Keep the commit suite quick and move slower checks to later stages. | Fast feedback against breadth of checks per commit. |
| Coverage targets | Use coverage to find untested risk, not as a score. | A number is easy to report and easy to game. |
| Legacy code with no seams | Add tests around the code you change, and refactor to allow isolation. | Slow start; a full retrofit is rarely worth it. |
When not to rely on unit tests alone: user journeys, performance, security and integration with third-party systems need other test levels. A green unit suite says each part works in isolation, not that the product does.
Where do AI agents fit in unit testing?
AI coding agents can draft unit tests and generate code, which raises the value of a dependable suite: tests are the quickest independent check on generated changes. Treat agent-written tests like any other code, reviewed and run in CI, and be wary of tests that were generated to match the code rather than the requirement. The wider picture is in agentic software testing.
XenonStack's ElixirClaw, an agentic OS platform to build, orchestrate and govern AI agents and automation workflows while keeping humans in control, is where such agents can run under review. AgentQA describes itself as automated quality assurance for AI agents, and ElixirData keeps governed context, versioned policy and decision traces in one decision layer above AI agents.
How should you adopt unit testing across a team?
- Agree what a unit means for your languages and architecture.
- Choose one framework per language and put it in the project template.
- Run the suite on every merge in CI and block merges on failure.
- Write a failing test before fixing each bug, so the bug cannot return unnoticed.
- Review test quality in code review, not only the coverage figure.
- Track suite run time and flaky tests, and fix them before adding more.
To map one testing decision your agents should make, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is unit testing in simple terms?
It is testing the smallest part of a program, such as one function or class, on its own to confirm it does what it should. Each team defines what a unit is.
How is unit testing different from integration testing?
A unit test checks one piece of code in isolation, often with test doubles. An integration test checks that several pieces, such as code and a database, work together.
What makes a good unit test?
According to Microsoft's docs, it is fast, isolated, repeatable, self-checking and timely. It should check one behaviour so a failure points at the cause.
What is the difference between a stub and a mock?
Per Microsoft's docs, a stub is a controllable replacement for a dependency, while a mock is a fake the test asserts against to decide pass or fail.
Which unit testing framework should we use?
Use the common one for your language, such as JUnit for Java, pytest for Python, NUnit for .NET or Jest for JavaScript, and confirm current versions on each project's page.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE