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

Зміст курсу

Python Advanced Concepts

Mocking TechniqueMocking 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.
Code Description
  1. First of all, import the necessary modules: unittest for creating and running tests, and MagicMock from unittest.mock;
  2. Defines a test class TestMoneyTransferservice that inherits from unittest.TestCase;
  3. The setUp method initializes new instances for each test method in the class. It creates an instance of MoneyTransferService and two MagicMock objects to represent source and destination bank accounts. MagicMock is used to simulate account objects without needing to create real account instances or database entries;
  4. test_successful_transfermethod tests a successful money transfer scenario.
  5. test_failed_transfer_due_to_insufficient_funds method tests the transfer failure due to insufficient funds.
    • Initializes the source account with only $100, which is less than the requested transfer amount of $200;
    • Attempts the transfer and expects a ValueError to be raised due to insufficient funds;
    • assertRaises(ValueError) Checks that a ValueError is indeed raised as expected when trying to transfer more than the available balance;
    • assertEqual Confirms that no changes were made to the account balances after the failed transfer attempt.

Все було зрозуміло?

Секція 5. Розділ 4
course content

Зміст курсу

Python Advanced Concepts

Mocking TechniqueMocking 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.
Code Description
  1. First of all, import the necessary modules: unittest for creating and running tests, and MagicMock from unittest.mock;
  2. Defines a test class TestMoneyTransferservice that inherits from unittest.TestCase;
  3. The setUp method initializes new instances for each test method in the class. It creates an instance of MoneyTransferService and two MagicMock objects to represent source and destination bank accounts. MagicMock is used to simulate account objects without needing to create real account instances or database entries;
  4. test_successful_transfermethod tests a successful money transfer scenario.
  5. test_failed_transfer_due_to_insufficient_funds method tests the transfer failure due to insufficient funds.
    • Initializes the source account with only $100, which is less than the requested transfer amount of $200;
    • Attempts the transfer and expects a ValueError to be raised due to insufficient funds;
    • assertRaises(ValueError) Checks that a ValueError is indeed raised as expected when trying to transfer more than the available balance;
    • assertEqual Confirms that no changes were made to the account balances after the failed transfer attempt.

Все було зрозуміло?

Секція 5. Розділ 4
some-alt