# Increase Test Fidelity By Avoiding Mocks

DevFeed: [Increase Test Fidelity By Avoiding Mocks](<https://devfeed.tech/articles/increase-test-fidelity-by-avoiding-mocks-23850.md>)

Original publisher: [Read original article](<http://testing.googleblog.com/2024/02/increase-test-fidelity-by-avoiding-mocks.html>)

Author: Google Testing Bloggers (noreply@blogger.com)

Published: 2024-02-27T18:43:00Z

Content type: tutorial

Language: en

Sources: [Google Testing Blog](<https://devfeed.tech/sources/google-testing-blog.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [Mocking](<https://devfeed.tech/topics/mocking.md>), [Software Engineering](<https://devfeed.tech/topics/software-engineering.md>), [Database](<https://devfeed.tech/topics/database.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [andrew-trenk](<https://devfeed.tech/tags/andrew-trenk.md>), [article](<https://devfeed.tech/tags/article.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [dillon-bly](<https://devfeed.tech/tags/dillon-bly.md>), [in-memory-database](<https://devfeed.tech/tags/in-memory-database.md>), [software](<https://devfeed.tech/tags/software.md>), [software-engineering](<https://devfeed.tech/tags/software-engineering.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>), [tott](<https://devfeed.tech/tags/tott.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

This Google Testing on the Toilet article explains that test fidelity is higher when tests use the real dependency implementation. If that is impractical, it recommends using a fake, such as an in-memory database, and using a mock only when neither a real implementation nor a fake is suitable. Mocks can reduce fidelity because their inline behavior may diverge from the real dependency.

## Source excerpt

This article was adapted from a Google Testing on the Toilet (TotT) episode. You can download a printer-friendly version of this TotT episode and post it in your office. By Andrew Trenk and Dillon Bly Replacing your code's dependencies with mocks can make unit tests easier to write and faster to run. However, among other problems, using mocks can lead to tests that are less effective at catching bugs. The fidelity of a test refers to how closely the behavior of the test resembles the behavior of the production code. A test with higher fidelity gives you higher confidence that your code will work properly. When specifying a dependency to use in a test, prefer the highest-fidelity option. Learn more in the Test Doubles chapter of the Software Engineering at Google book. Try to use the real implementation. This provides the most fidelity, because the code in the implementation will be executed in the test. There may be tradeoffs when using a real implementation: they can be slow, non-deterministic, or difficult to instantiate (e.g., it connects to an external server). Use your judgment to decide if a real implementation is the right choice. Use a fake if you can't use the real implementation. A fake is a lightweight implementation of an API that behaves similarly to the real implementation, e.g., an in-memory database. A fake ensures a test has high fidelity, but takes effort to write and maintain; e.g., it needs its own tests to ensure that it conforms to the behavior of the real implementation. Typically, the owner of the real implementation creates and maintains the fake. Use a mock if you can't use the real implementation or a fake. A mock reduces fidelity, since it doesn't execute any of the actual implementation of a dependency; its behavior is specified inline in a test (a technique known as stubbing), so it may diverge from the behavior of the real implementation. Mocks provide a basic level of confidence that your code works properly, and can be especially use