Improve Your Tests With the Python Mock Object Library (Overview)
When you’re writing robust code, tests are essential for verifying that your application logic is correct, reliable, and efficient. However, the value of your tests depends on how well they demonstrate these criteria. Obstacles such as complex logic and unpredictable dependencies make writing valuable tests difficult. The Python mock object library, unittest.mock
, can help you overcome these obstacles.
By the end of this course, you’ll be able to:
- Create Python mock objects using
Mock
- Assert that you’re using objects as you intended
- Inspect usage data stored on your Python mocks
- Configure certain aspects of your Python mock objects
- Substitute your mocks for real objects using
patch()
- Avoid common problems inherent in Python mocking
You’ll begin by seeing what mocking is and how it will improve your tests!
00:00 When you’re writing a robust program, tests are essential for verifying that your code is correct, reliable, and efficient. However, writing good tests can be challenging when you have complex logic or external dependencies that you have no control over.
00:18 But fortunately, Python gives you some good tools to overcome these challenges and write good tests using mocks. Welcome to this video course on the topic.
00:29
My name is Lee, and I will be your guide throughout these videos. Throughout this course, you’re going to learn what mocking is and why it’s useful, how to create Python mock objects, how to assert your program is using objects as intended, how to inspect data stored in mocks, how to configure mocks, how to use the patch()
method to substitute real objects with mocks, and finally, how to avoid common problems when mocking.
00:56 So, if none of this makes sense to you, that’s great—you’re in the perfect place to learn all about mocking and how to use it to create better tests.
Become a Member to join the conversation.