Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Mocking Techniques in Unittest: Simulating Dependencies | Mastering Unittest Framework
Quizzes & Challenges
Quizzes
Challenges
/
Python Structural Programming

bookMocking Techniques in Unittest: Simulating Dependencies

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

class MoneyTransferService:
    def transfer(self, source_account, destination_account, amount):
        if source_account.balance < amount:
            raise ValueError("Insufficient funds")
        source_account.balance -= amount
        destination_account.balance += amount
        return True

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.
import unittest
from unittest.mock import MagicMock

class TestMoneyTransferService(unittest.junit.TestCase):
    def setUp(self):
        self.transfer_service = MoneyTransferService()
        self.source_account = MagicMock()
        self.destination_account = MagicMock()

    def test_successful_transfer(self):
        # Set up the account balances
        self.source_account.balance = 1000
        self.destination_account.balance = 500

        # Perform the transfer
        successful = self.transfer_service.transfer(self.source_account, self.destination_account, 200)
        
        # Check balances
        self.assertTrue(successful)
        self.assertEqual(self.source_account.balance, 800)
        self.assertEqual(self.destination_account.balance, 700)

    def test_failed_transfer_due_to_insufficient_funds(self):
        # Set up the account balances
        self.source_account.balance = 100
        self.destination_account.balance = 500

        # Attempt to transfer more than the source balance
        with self.assertRaises(ValueError):
            self.transfer_service.transfer(self.source_account, self.destination_account, 200)
        
        # Verify that balances remain unchanged
        self.assertEqual(self.source_account.balance, 100)
        self.assertEqual(self.destination_account.balance, 500)
Завдання

Swipe to start coding

Your task is to write unit tests for the MoneyTransferService class using the unittest framework and unittest.mock. You must:

  • Create a test class that inherits from unittest.TestCase;
  • Use MagicMock to simulate account objects;
  • Implement two test methods with these exact names:
    • test_successful_transfer;
    • test_failed_transfer_due_to_insufficient_funds.
  • In test_successful_transfer, test a transfer where the source has enough funds, and verify both the return value and the updated balances;
  • In test_failed_transfer_due_to_insufficient_funds, test a transfer where the source does not have enough funds, verify that a ValueError is raised, and check that balances remain unchanged.

Note: The names of the test functions must match exactly as shown above. Your tests should verify the behavior by checking values and exception handling directly, not by using regex or pattern matching.

Рішення

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

bookMocking Techniques in Unittest: Simulating Dependencies

Свайпніть щоб показати меню

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

class MoneyTransferService:
    def transfer(self, source_account, destination_account, amount):
        if source_account.balance < amount:
            raise ValueError("Insufficient funds")
        source_account.balance -= amount
        destination_account.balance += amount
        return True

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.
import unittest
from unittest.mock import MagicMock

class TestMoneyTransferService(unittest.junit.TestCase):
    def setUp(self):
        self.transfer_service = MoneyTransferService()
        self.source_account = MagicMock()
        self.destination_account = MagicMock()

    def test_successful_transfer(self):
        # Set up the account balances
        self.source_account.balance = 1000
        self.destination_account.balance = 500

        # Perform the transfer
        successful = self.transfer_service.transfer(self.source_account, self.destination_account, 200)
        
        # Check balances
        self.assertTrue(successful)
        self.assertEqual(self.source_account.balance, 800)
        self.assertEqual(self.destination_account.balance, 700)

    def test_failed_transfer_due_to_insufficient_funds(self):
        # Set up the account balances
        self.source_account.balance = 100
        self.destination_account.balance = 500

        # Attempt to transfer more than the source balance
        with self.assertRaises(ValueError):
            self.transfer_service.transfer(self.source_account, self.destination_account, 200)
        
        # Verify that balances remain unchanged
        self.assertEqual(self.source_account.balance, 100)
        self.assertEqual(self.destination_account.balance, 500)
Завдання

Swipe to start coding

Your task is to write unit tests for the MoneyTransferService class using the unittest framework and unittest.mock. You must:

  • Create a test class that inherits from unittest.TestCase;
  • Use MagicMock to simulate account objects;
  • Implement two test methods with these exact names:
    • test_successful_transfer;
    • test_failed_transfer_due_to_insufficient_funds.
  • In test_successful_transfer, test a transfer where the source has enough funds, and verify both the return value and the updated balances;
  • In test_failed_transfer_due_to_insufficient_funds, test a transfer where the source does not have enough funds, verify that a ValueError is raised, and check that balances remain unchanged.

Note: The names of the test functions must match exactly as shown above. Your tests should verify the behavior by checking values and exception handling directly, not by using regex or pattern matching.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

single

some-alt