ArticleslgStudy

computer science

Test double

Test double is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Test double rather than just read about it. In short: A test double is software used in software test automation that satisfies a dependency so that the test need not depend on production code. A test double provides functionality via an interface that the software under test cannot distinguish from production code.

Key takeaways

  • Test double belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Test double to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Test double from memory before moving on to harder problems.

Reference excerpt

A test double is software used in software test automation that satisfies a dependency so that the test need not depend on production code. A test double provides functionality via an interface that the software under test cannot distinguish from production code. A programmer generally uses a test double to isolate the behavior of the consuming code from the rest of the codebase. A test double is usually a simplified version of the production code and may include capabilities specific to testing. Test doubles are used to build test harnesses.

Uses A test double may be used to simplify tests, increase speed of execution or allow for deterministic results of an action. For example, a program that uses a database server is relatively slow and consumes significant system resources, which impedes testing productivity. A test might require data from the database that under normal system activity is regularly changing, so provides non-deterministic outputs for any given query. A test double can provide a static value instead of accessing a real database, thereby both avoiding network or system calls, and changing data. A test double may also be used to test part of the system that is ready for testing even if its dependencies are not. For example, in a system with modules Login, Home and User, suppose Login is ready for test, but the other two are not. The consumed functions of Home and User can be implemented as test doubles so that Login can be tested.

Caveats While test doubles are often used to facilitate unit testing there are limitation of using test doubles, the key one being that actual database connectivity or other external-access is not proven to work by those tests. To avoid errors that may be missed by this, other tests are needed that instantiate the code with the "real" implementations of the interfaces discussed above. These integration risks are typically covered by integration tests, system tests or system integration tests.

Implementation approaches When implementing test doubles, the typical approach involves two key steps:

Whenever external access is needed in production, an interface should be defined that describes the access available. See the dependency inversion principle for a discussion of the benefits of doing this regardless of TDD. The interface should be implemented in two ways, one of which really accesses the external process for use in production, and the other of which is a test double typically a mock or a fake. This approach enforces a unit-testable separation and drives more modular, testable and reusable code design.

Types Test doubles are categorization many ways.

General Although not universally accepted, Gerard Meszaros categorizes test doubles as:

Stub — provides static input Mock — verifies output via expectations defined before the test runs Spy — supports setting the output of a call before a test runs and verifying input parameters after the test runs Fake — a relatively full-function implementation that is better suited to testing than the production version (e.g., an in-memory database instead of a database server) Dummy value — a value that is required for the tested interface but for which the test case does not depend While there is no open standard for categories, Martin Fowler used these terms in his article, Mocks Aren't Stubs referring to Meszaros' book. Microsoft also used the same terms and definitions in an article titled, Exploring The Continuum Of Test Doubles.

Service For service-oriented architecture (SOA) systems and microservices, testers use test doubles that communicate with the system under test over a network protocol. These test doubles are called by different names by the tool vendors. A commonly used term is service virtualization. Other names used include API simulation, API mock, HTTP stub, HTTP mock, over the wire test double.

Verified fake A verified fake is a fake object whose behavior has been verified to match that of the real object using a set of tests that run against both the verified fake and the real implementation.

See also Mock object Software testing Service virtualization List of unit testing frameworks Object-oriented programming Test-driven development

References

External links Gerard Meszaros:

Test Double Test Double Patterns Mocks, Fakes, Stubs and Dummies Martin Fowler:

TestDouble, 17 January 2006 Open source:

ELF Spy - Fakes and Spies in C++ FakeIt - Mocks, Fakes and Spies in C++ Google Mock - Mocking in C++ jMock - Test Driven Development with Mocks Mockito - Mocking Framework for Java unittest.mock - Mocking with Python

Worked examples

Example 1 — a first encounter with Test double

Start with the simplest possible case. Write down what Test double claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Test double before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Test double ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Test double

In research
Test double appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Test double in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Test double is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software design patterns, Software development process, Software testing, so understanding it makes those chapters shorter.
In everyday life
Look for Test double outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Test double” →

How to study Test double in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Test double means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Test double out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Test double in simple terms?

A test double is software used in software test automation that satisfies a dependency so that the test need not depend on production code. A test double provides functionality via an interface that the software under test cannot distinguish from production code.

Why does Test double matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Test double?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Test double.

Tags

  • Software design patterns
  • Software development process
  • Software testing

Keep exploring