flexmock
flexmock is a testing library for Python that makes it easy to create mocks, stubs and fakes.
What it is and what it does
flexmock is a testing library that lets you create mock, stub, spy, and fake objects for unit testing without pulling in external dependencies. It reads like English sentences rather than API calls—for example, you can write `flexmock(Train).should_receive("get_destination").and_return("Paris").once()` to assert that a method is called exactly once with a specific return value. The library works with unittest, pytest, and doctest out of the box, and its entire public API is type-annotated for editor support and static analysis.
You use flexmock when you need to isolate code under test by replacing real objects with controlled doubles. Mocks let you verify that methods were called with expected arguments; spies let you wrap the real method and check how many times it ran; stubs replace parts of objects or classes; and fakes generate placeholder objects. Since it has no external dependencies and only uses the Python standard library, it's lightweight and straightforward to add to any test suite.
Use it for:
- Verify that a method was called with specific arguments and return value in a unit test.
- Spy on real method calls to count invocations or check they never happen with certain inputs.
- Replace a database or API client with a stub to test business logic in isolation.
- Generate fake objects to populate test fixtures without writing boilerplate classes.
- Assert that exception-raising methods are called the correct number of times.
- Test code that depends on external services by mocking their interfaces.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
flexmock creates mock, stub, spy, and fake objects for testing Python code, with zero external dependencies and full type annotations.
Yes. flexmock is actively maintained, has no external dependencies, supports current Python versions (3.10+), carries a permissive license, and integrates seamlessly with all major test runners. It's a solid choice if you prefer readable, English-like mock declarations over the standard library's unittest.mock API.
Install
flexmock on PyPI
pip
pip install flexmockuv
uv add flexmockpoetry
poetry add flexmockInstalling flexmock
Before you install
Low friction install with no runtime dependencies. Actively maintained as of 2026-01-10, last commit 2026-08-09, and marked Production/Stable.
License in practice
BSD-2-Clause permissive license allows broad use, modification, and distribution with minimal restrictions.
Quickstart
pip install flexmock
from flexmock import flexmock
flexmock(SomeClass).should_receive("method_name").and_return("value").once()
Requires Python 3.10 or later (supports 3.10, 3.11, 3.12, 3.13, 3.14, and PyPy3).
Verify before relying
- Whether the English-like declaration syntax is genuinely easier to learn than unittest.mock for developers new to mocking.
- Real-world performance characteristics when mocking complex object hierarchies or large test suites.
Package facts
| License | BSD-2-Clause (permissive) |
| Python support | supports the current Python release (<4.0.0,>=3.10.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 216 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 149,003/month — #11,007 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: flexmock-0.13.0-py3-none-any.whl
Keywords: mock, testing, test, unittest, pytest
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
mockitoMockito is a spying and stubbing framework for…
permissive · top 5,000 on PyPI
pretendPretend provides a lightweight library for…
permissive · top 15,000 on PyPI
pytest-mockProvides a pytest fixture that wraps the mock…
permissive · top 1,000 on PyPI
wiremockPython client library for interacting with…
permissive · top 15,000 on PyPI
stubsProvides a context-manager syntax for…
permissive · top 15,000 on PyPI
pytest-stubA pytest plugin that replaces package, module,…
permissive · top 15,000 on PyPI
TestSlideTestSlide is a test framework for Python that…
permissive · top 15,000 on PyPI
mockmock provides a backport of Python's standard…
permissive · top 1,000 on PyPI
pyfakefspyfakefs mocks the Python file system modules…
permissive · top 5,000 on PyPI
mock-firestoreProvides an in-memory mock of the Google Cloud…
permissive · top 15,000 on PyPI