a pytest extension for easy mock setup
Project description
ExtendedMock Pytest Plugin
Welcome to the ExtendedMock Pytest Plugin! This plugin enhances Python's unittest.mock.MagicMock with additional functionality inspired by the setup and verification patterns found in popular .NET mocking libraries like Moq. If you've ever used Moq and loved its fluent API for configuring mock behavior, this plugin will feel like home.
What is ExtendedMock?
ExtendedMock is a drop-in replacement for unittest.mock.MagicMock that adds a setup method, allowing you to configure mock behavior for specific arguments. This makes it easier to write expressive and precise tests by defining exactly what your mock should return (or raise) when called with certain arguments.
Key Features:
- Fluent API: Configure mock behavior using a clean, chainable
setupmethod. - Argument-Specific Behavior: Define return values or exceptions for specific argument combinations.
- Seamless Integration: Works out of the box with
pytestand replacesMagicMockautomatically. - Familiar to .NET Developers: If you've used Moq, you'll feel right at home with the
setupmethod.
Usage
Basic Example
Here's how you can use ExtendedMock in your tests:
from unittest.mock import MagicMock
def test_extended_mock():
mock = MagicMock()
# Configure the mock to return "Hello, World!" when called with ("foo", bar=42)
mock.setup("foo", bar=42, return_value="Hello, World!")
# Call the mock with the configured arguments
result = mock("foo", bar=42)
assert result == "Hello, World!"
Raising Exceptions
You can also configure the mock to raise an exception for specific arguments:
def test_extended_mock_with_exception():
mock = MagicMock()
# Configure the mock to raise a ValueError when called with ("error",)
mock.setup("error", return_value=ValueError("Something went wrong!"))
# Call the mock with the configured arguments
with pytest.raises(ValueError):
mock("error")
Default Behavior
If the mock is called with arguments that don't match any configured setup, it falls back to the default MagicMock behavior:
def test_extended_mock_default_behavior():
mock = MagicMock()
# No setup configured, so it returns a new MagicMock instance
result = mock("unconfigured", args=123)
assert isinstance(result, MagicMock)
Why Use ExtendedMock?
If you've ever found yourself writing repetitive code to configure MagicMock instances for different argument combinations, ExtendedMock is here to save the day. It provides a more expressive and concise way to define mock behavior, making your tests easier to read and maintain.
For .NET Developers
If you're coming from the .NET world and have used libraries like Moq, you'll find the setup method very familiar. It mirrors the Setup and Returns methods in Moq, allowing you to define mock behavior in a similar way. For example:
// Moq in .NET
var mock = new Mock<IService>();
mock.Setup(x => x.DoSomething("foo", 42)).Returns("Hello, World!");
# ExtendedMock in Python
mock = MagicMock()
mock.setup("foo", 42, return_value="Hello, World!")
How It Works
The ExtendedMock class extends unittest.mock.MagicMock and adds a _setup_data dictionary to store configurations. When the mock is called, it checks if the arguments match any configured setup. If a match is found, it returns the configured value or raises the configured exception. If no match is found, it falls back to the default MagicMock behavior.
Contributing
We welcome contributions! If you have ideas for improvements or find a bug, please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Keywords
- pytest
- mocking
- unittest
- MagicMock
- Moq
- .NET
- testing
- fluent API
- mock setup
Happy testing! 🚀
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_extended_mock-0.1.1.tar.gz.
File metadata
- Download URL: pytest_extended_mock-0.1.1.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18c7442abee07d1cc72c2fb913318791cd27bc517cddab01a20581dd3a114cca
|
|
| MD5 |
fc927879cdb312e3440cbc12772fb32e
|
|
| BLAKE2b-256 |
df23761110ff3363d30063e730ba780c8700cb78f9b1d2536c940dfc3e106450
|
File details
Details for the file pytest_extended_mock-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pytest_extended_mock-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70749fa1e52c7c5c4c3002677534742a378fc635ec8ebcbcf6cfe4687f8d6dac
|
|
| MD5 |
205bb86fb14c0f454243e024b54bda5c
|
|
| BLAKE2b-256 |
d97a12b6a8e37f77f90d7715555536181d24ab12723f2a419fe62212e96a428e
|