Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Mocking Technique | Unittest Framework
Python Advanced Concepts
course content

Course Content

Python Advanced Concepts

Python Advanced Concepts

1. Modules and Imports
2. Error Handling
3. File Handling
4. Pytest Framework
5. Unittest Framework
6. Iterators and Generators

book
Mocking Technique

Testing code that depends on external services, configurations, or has side effects can be challenging. Mocking is a powerful technique that simulates the behavior of complex real-world interactions within tests without their complexity and unpredictability.

For instance, if you need to test a money exchange service, one option could involve using your own funds to simulate transactions and observe the outcomes. However, this method can be risky and costly. A more efficient and risk-free alternative is to use mocking to emulate the process of sending money.

Testing a Money Transfer Service

The Testing Approach:

  1. Mock the Account Balances: use mocking to simulate different account states, such as insufficient funds or ample funds, to test how the transfer method responds;
  2. Verify Transactions: confirm that the appropriate amounts are debited and credited from the respective accounts during a transfer;
  3. Handle Exceptions: ensure that the method correctly handles scenarios where the transfer cannot be completed.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt