Mockups are a central tool in software development to simplify testing and control dependencies. Especially when working with external APIs, testing can become somewhat difficult, as one has to rely on the availability and behavior of these services. Using mockups allows you to simulate and control these dependencies. This way, you have full control over the return values and can conduct tests even when there is no internet connection.
Key Takeaways
- Mockups help simulate external dependencies.
- They allow tests to be conducted independently of the availability of external services.
- Replacing real API calls with mockups speeds up the testing process.
Step-by-Step Guide
1. Create the Sample Project
First, create a new Python project where you can implement a simple weather example. In this example, you will simulate an API call that should retrieve the temperature from a weather service. To get started, you need a function that retrieves the temperature.

2. Create the Temperature Retrieval Function
Implement a function that retrieves a value from an external API. In this case, you will assume the temperature is simulated as 18.1 degrees Celsius to test conditions later.

3. Implement the Conditional Logic
Now write a simple logic that evaluates the temperature. It should check if it is hot, warm, or cold. Define simple boundaries for this evaluation: above 28 degrees is hot, above 18 degrees is warm, and anything below is cold.

4. Set Up Mockups
To replace the API dependency, you'll need the unittest.mock module. This allows you to redirect the temperature retrieval function so that it always returns a predefined value. Import mock and create a mockup version of your temperature function.
5. Apply the Mockups
At this point, set the return values for the mockup function. Let's assume you want to ensure that the function returns 18 degrees first. With this, you can then verify the test for the cold condition.

6. Write Tests for Different Temperature Scenarios
Create several tests to cover the various temperature effects. For example: one test for returning 18 degrees (cold), one for 22 degrees (warm), and one for 32 degrees (hot). This ensures that your logic works correctly in all cases.

7. Run Tests and Check Results
Run your tests and check whether the mockup approach was successful. All tests should confirm that the boundaries you defined in temperature control are effective. This shows that your setup is correct and that dependencies have been successfully avoided.

8. Reflect on the Benefits of Mockups
Take this opportunity to reflect on how mockups have enabled you to conduct tests independently of external factors. Consider how this method can help in other projects and scenarios.

Summary – Using Mockups in Python
By using mockups, you have regained control over your tests. By simulating API dependencies, you were able to write more stable and faster tests. This will make it easier for you to conduct condition tests in the future and increase the efficiency of your software development.
Frequently Asked Questions
How do mockups help in testing?Mockups allow simulating external dependencies, enabling tests to be conducted independently of external services.
Can I use mockups for other data types as well?Yes, mockups can be used to simulate all kinds of return values, regardless of the data type.
Are mockups only useful for API requests?No, mockups are versatile and useful for all kinds of functions that have external dependencies.