[Pytest 101] 01 Introduction to Pytest & Mock
An introduction to Pytest and the core concepts of Mock

Writing unit tests is a well-known best practice in software development, but actually getting started often feels overwhelming.
Where do you begin? What tools should you use? And how do you use them?
For beginners, these first steps can easily become roadblocks.
Frameworks and Plugins
Name | Purpose | Installation Command |
---|---|---|
pytest | Testing framework | poetry add pytest --dev |
pytest-mock | Plugin for mocking objects | poetry add pytest-mock --dev |
pytest-cov | Plugin for generating coverage | poetry add pytest-mock --dev |
Usage
Before getting started, make sure your Poetry environment is properly set up:
poetry install --no-root
Commonly Used Flags
Flag | Purpose | Reference |
---|---|---|
-s |
Show print output | How to capture stdout/stderr output |
--cov |
Enable code coverage for folders | |
--cov-report |
Set format for coverage report | Reporting - pytest-cov |
--log-cli-level=DEBUG |
Show debug-level logs during test | How to manage logging |
Run a specific test file:
poetry run pytest -s --log-cli-level=DEBUG ${file}
Run tests in a specific folder:
poetry run pytest -s --log-cli-level=DEBUG ${folder}
Run a specific test function:
poetry run pytest -s --log-cli-level=DEBUG ${file}::${test_name}
Combined Usage
Display coverage in terminal:
poetry run pytest --log-cli-level=DEBUG --cov=. --cov-report=term-missing ${folder}
Generate HTML coverage report:
poetry run pytest --cov-report=html:cov_html --cov=. ${folder}
Open the coverage report:
open ./cov_html/index.html
What is Mock
Python comes with a built-in module called unittest.mock , which is used to replace parts of your system under test and make assertions about how they are used.
MagicMock
Think of it as an enhanced version of mock.
MagicMock is used to simulate any Python object—whether it’s a class, function, or module—without needing to define an interface in advance.
Python is a dynamically typed language, meaning types are determined at runtime instead of compile time. MagicMock is designed to take advantage of this flexibility. In statically typed languages, you often need to define an interface or abstract class first before mocking it. With MagicMock, you can dynamically create methods and attributes as needed—no prior definitions required.
Not only can it simulate simple data, but it can also mock more complex objects like database connections or network requests. You can configure MagicMock based on the needs of your tests.
Core Concepts
- Dynamic Attribute Creation: Accessing a non-existent attribute will automatically return a new MagicMock object.
- Automatic Method Creation: Calling a non-existent method will also create a new MagicMock object on the fly.
- Configurable Return Values and Side Effects:
You can use
return_value
orside_effect
to control the behavior of a mock object. - Call Tracking: MagicMock keeps track of how it has been used—number of calls, arguments passed, etc.
- Magic Method Support: It handles Python’s special methods like str, len, getitem, and allows configuring them as well.