Mocking data is a common technique for testing software that depends on external sources of data, such as databases, web services, or APIs. Mocking data means creating fake or simulated data that mimics the real data, but without actually interacting with the external source. Mocking data can be useful for some scenarios, such as:

  • Isolating the unit under test from other components or dependencies that are not relevant for the test.
  • Speeding up the test execution by avoiding network calls or database queries that can be slow or unreliable.
  • Controlling the test input and output by creating predictable and consistent data that can be easily verified.

However, mocking data also has some serious drawbacks and risks, such as:

  • Introducing errors or inconsistencies between the mock data and the real data, which can lead to false positives or false negatives in the test results.
  • Reducing the coverage and confidence of the test, by not testing the actual behavior and logic of the external source or the interaction with it.
  • Increasing the maintenance and complexity of the test, by requiring extra code and logic to create and manage the mock data.

In this blog post, I will argue that mocking data is a bad practice for testing software, and that you should avoid it as much as possible. I will also show you some examples of how to test software that depends on external data sources without mocking data, using Python and PyTest.

continue reading on medium.com

⚠️ This post links to an external website. ⚠️