Unit testing vs functional testing
A unit test checks one small piece of code, such as a function or a class, in isolation, and is usually written by the developer alongside the code. A functional test checks a behaviour the specification promises, through the system's interface, without regard to how the code is built. One proves a part, and the other proves a promise.
The longer version
The names come from two different classifications, which is why the comparison is slippery. Unit testing is the testing of the smallest piece of code that can be tested in isolation, and it is a test level: a statement about how much of the system is under test. The ISTQB glossary defines it as "a test level that focuses on evaluating the smallest part of code testable in isolation". The ISTQB Foundation Level syllabus, which calls it component testing, adds that it is normally performed by developers in their development environments. Functional testing is a test type, a statement about what is being checked. The syllabus says it evaluates the functions that a component or system should perform, the "what" of the test object, and that test types can be applied at all test levels. A unit test that compares a function's output with its requirement is therefore functional testing at the unit level. The everyday contrast uses the words more narrowly: unit test for the developer's isolated check of code, and functional test for a check of a whole feature through the product's interface.
A delivery charge shows the narrower contrast at work. The specification says that orders over fifty pounds are delivered free and smaller orders pay a flat fee. A unit test calls the function that works out the charge with a basket of forty-nine pounds, then fifty, then fifty-one, and checks the number returned each time. It needs no browser and no database, and when it fails it points at one function. A functional test opens the shop, fills a basket to fifty-one pounds, goes to the checkout and reads the charge on the page. It knows nothing about the function and would still be valid after a complete rewrite. It also passes through everything the unit test went around: the basket total, the tax rounding and the template that prints the figure.
Each kind is blind where the other sees. Every unit can pass while the feature fails, since the units may be joined wrongly or the page may show a charge worked out before a voucher was applied. A functional test can pass while a unit is wrong for an input that no specified case reaches. The two are also derived from different things. The developer derives unit tests from the design of the code, often with knowledge of its branches, and replaces the unit's collaborators with test doubles to keep it isolated. A tester, or a developer acting as one, derives functional tests from the requirements and acceptance criteria and treats the product as a black box. Neither substitutes for the other. Martin Fowler advises that a bug exposed by a high-level test should be replicated with a unit test before it is fixed, so that the unit test keeps the bug from coming back.
Side by side, a unit test is derived from the design of the code, checks one function or class, replaces its collaborators with test doubles, runs fast enough to repeat on every save, is written by the developer and fails at one piece of code. A functional test is derived from the requirements, checks one feature through the interface, uses the assembled product, runs more slowly, is written by a tester or a developer and fails at a behaviour a user would notice. When to use which: write unit tests for logic with many cases and edges, such as calculations, parsing and validation rules, where trying every input through a browser would take far too long. Write functional tests for each behaviour the specification promises, at least one per acceptance criterion, so that the feature is proved the way a user meets it. Most teams want both, with many unit tests underneath, fewer functional tests above them, and a new unit test each time a functional test exposes a bug in one unit.
Common questions
What is the difference between unit testing and functional testing?
Unit testing checks one small piece of code in isolation, usually by the developer who wrote it and with knowledge of how it is built. Functional testing checks that the software does what its specification says, judged from outside through its interface. Unit testing names how much is under test, and functional testing names what is being checked, so a single test can be both.
What is unit testing?
Unit testing is the testing of the smallest piece of code that can be tested in isolation, such as a function, a method or a class. The ISTQB glossary defines it as a test level that focuses on evaluating the smallest part of code testable in isolation, and the ISTQB Foundation Level syllabus treats the name as another word for component testing.
Can a unit test be a functional test?
A unit test is a functional test whenever it compares the behaviour of the unit with what a requirement says the unit should do. The ISTQB Foundation Level syllabus states that test types, functional testing among them, can be applied at all test levels. In everyday speech, though, a functional test usually means a check of a feature through the user interface or the API.
Who writes unit tests and who writes functional tests?
Developers write unit tests, generally alongside the code being tested. Functional tests at the level of the whole system are written by testers, by developers or by both, working from the requirements and acceptance criteria and not from the code. On a team without dedicated testers the same people write both kinds, and the difference lies in what each test is derived from.
Which should run first, unit tests or functional tests?
Unit tests run first, since they are quick, need no deployed system and point at a single piece of code when they fail. Functional tests through the interface need the product assembled and running, so they come after the build has passed its unit tests. A pipeline ordered this way stops at the cheapest failure before spending time on slower checks.
What is the difference between unit testing and QA testing?
Unit testing is one kind of check, done by developers on single pieces of code. QA testing is the wider work of testing the product, usually by testers, and it includes functional, exploratory, regression and acceptance testing of the assembled system. Passing unit tests are evidence that the parts work, and they do not replace testing the product the way a user meets it.
Is unit testing white-box or black-box testing?
Unit testing is usually white-box testing, since the developer writes it with knowledge of the code's structure and branches. Functional testing is usually black-box, derived from the requirements without reference to the code. The labels describe how a test is designed, so a unit test written purely from a function's documented contract is black-box in design although it runs at the unit level.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.