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

Contenido del Curso

Python Advanced Concepts

Mocking in DetailsMocking in Details

In the previous chapter, we saw the importance of using Mock in testing. Now, let's take a closer look at the different capabilities and clarify any aspects that were unclear.

Introduction to MagicMock

MagicMock is an incredibly versatile implementation of Mock that supports magic methods. You can use it to mimic the behavior of complex objects in your tests as we saw in previous chapter.

In this example, MagicMock is used to simulate the get_account_balance method, which would typically retrieve data from a database.

Using mock.patch

@mock.patch is used for temporarily replacing the real implementations of objects in your code.

Imagine a function that checks user credentials against a database. You can use mock.patch to avoid hitting the actual database:

Mocking with Context Manager

At times, it's preferable to employ patch() as a context manager instead of a decorator, particularly when:

  • you need to mock an object for just a portion of the test;
  • or when excessive use of decorators or parameters is reducing the clarity of your tests.
Code Description
  • The BankAccount class has a method get_account_balance that would typically retrieve data from a database.
  • The patch() function is used as a context manager to mock the get_account_balance method for the duration of the with block.
  • This allows the test to run with the mocked method returning 1000, ensuring the balance attribute is set correctly.

Pytest Monkey Patch

The monkeypatch fixture in Pytest lets you temporarily modify classes, modules, or environments during a test.

The monkeypatch fixture allows you to safely modify and manage the environment during the test without causing side effects outside the test scope.

Code Description
  • This defines a test function named test_api_url that takes monkeypatch as an argument.
  • The monkeypatch.setenv method is used to temporarily set the environment variable API_URL to "https://testapi.com". This modification is only in effect during the test and is automatically reverted to its original state afterward, thanks to the monkeypatch fixture. This is particularly useful for testing code that behaves differently based on environment configurations without permanently affecting the actual environment.
1. Which of the following code snippets correctly mocks the get_balance method to return 500?
2. What does the following test check?

Which of the following code snippets correctly mocks the get_balance method to return 500?

Selecciona la respuesta correcta

What does the following test check?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 5
course content

Contenido del Curso

Python Advanced Concepts

Mocking in DetailsMocking in Details

In the previous chapter, we saw the importance of using Mock in testing. Now, let's take a closer look at the different capabilities and clarify any aspects that were unclear.

Introduction to MagicMock

MagicMock is an incredibly versatile implementation of Mock that supports magic methods. You can use it to mimic the behavior of complex objects in your tests as we saw in previous chapter.

In this example, MagicMock is used to simulate the get_account_balance method, which would typically retrieve data from a database.

Using mock.patch

@mock.patch is used for temporarily replacing the real implementations of objects in your code.

Imagine a function that checks user credentials against a database. You can use mock.patch to avoid hitting the actual database:

Mocking with Context Manager

At times, it's preferable to employ patch() as a context manager instead of a decorator, particularly when:

  • you need to mock an object for just a portion of the test;
  • or when excessive use of decorators or parameters is reducing the clarity of your tests.
Code Description
  • The BankAccount class has a method get_account_balance that would typically retrieve data from a database.
  • The patch() function is used as a context manager to mock the get_account_balance method for the duration of the with block.
  • This allows the test to run with the mocked method returning 1000, ensuring the balance attribute is set correctly.

Pytest Monkey Patch

The monkeypatch fixture in Pytest lets you temporarily modify classes, modules, or environments during a test.

The monkeypatch fixture allows you to safely modify and manage the environment during the test without causing side effects outside the test scope.

Code Description
  • This defines a test function named test_api_url that takes monkeypatch as an argument.
  • The monkeypatch.setenv method is used to temporarily set the environment variable API_URL to "https://testapi.com". This modification is only in effect during the test and is automatically reverted to its original state afterward, thanks to the monkeypatch fixture. This is particularly useful for testing code that behaves differently based on environment configurations without permanently affecting the actual environment.
1. Which of the following code snippets correctly mocks the get_balance method to return 500?
2. What does the following test check?

Which of the following code snippets correctly mocks the get_balance method to return 500?

Selecciona la respuesta correcta

What does the following test check?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 5
some-alt