Registry / testing / flexmock

flexmock

JSON →
library0.13.0pypypi✓ verified 86d ago

flexmock is a Python testing library that simplifies the creation of mocks, stubs, and fakes. It's designed to be flexible and expressive for isolating code under test. The current version is 0.13.0, and it maintains an active release cadence, frequently updating for Python version compatibility and dependency bumps.

pip install flexmock
INSTALL
IMPORT
SIG · FLEXMOCK
F
flexmock
testingpythonv0.13.0
Install
1.5s avg
Import
237ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.259s · 17.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.214s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

flexmock
from flexmock import flexmock
flexmock_teardown
from flexmock import flexmock_teardown
from flexmock.mock import flexmock_teardown
flexmock_teardown is exposed directly at the top level of the flexmock package, not within a 'mock' submodule.

This quickstart demonstrates how to mock a class method using `flexmock` to control the return value of `datetime.date.today()`. It highlights the importance of `flexmock_teardown()` for proper cleanup after tests, preventing mock leakage.

import datetime from flexmock import flexmock, flexmock_teardown def get_today_str(): return datetime.date.today().isoformat() # --- Example Test (usually in a test file) --- # The flexmock_teardown() is crucial for cleaning up mocks. # In pytest, use the flexmock fixture. In unittest, call it in tearDown. # For a standalone script, call it manually. try: # Mock the 'today' method of the datetime.date class flexmock(datetime.date).should_receive('today').and_return(datetime.date(2023, 10, 26)) # Call the function under test result = get_today_str() # Assert the result assert result == '2023-10-26' print(f"Test passed: Today's date is mocked to {result}") finally: # Ensure mocks are cleaned up flexmock_teardown()
Debug
Known issues
breakingflexmock v0.13.0 dropped support for Python 3.8. Earlier versions dropped 3.6 and 3.7. Ensure your Python environment meets the `flexmock` version requirements.
fix
Upgrade your Python interpreter to 3.9 or newer, or pin your flexmock version to `flexmock < 0.13.0` for Python 3.8, or `flexmock < 0.12.0` for Python 3.6/3.7.
affects: 0.12.0, 0.12.1, 0.13.0
gotchaMocks created with `flexmock` do not automatically clean up in all testing contexts. Failure to explicitly call `flexmock_teardown()` or use appropriate test runner integration can lead to mocks leaking between tests, causing flaky or incorrect test results.
fix
For `unittest` tests, call `flexmock_teardown()` in `tearDown()`. For `pytest`, either use the `flexmock` fixture which handles cleanup, or implement an `autouse` fixture calling `flexmock_teardown()`.
affects: All versions
gotchaThe `flexmock.new_instances()` method is specifically for mocking *class instantiation* (i.e., when `__init__` is called), not for mocking methods on an already existing class or a new instance after creation. Misunderstanding this can lead to mocks not being applied as expected.
fix
Use `flexmock(ClassName).new_instances(instance1, instance2)` to mock objects returned by `ClassName()`. To mock methods on an existing object or a class directly, use `flexmock(obj).should_receive(...)` or `flexmock(Class).should_receive(...)`.
affects: All versions
deprecatedAs of v0.12.0, flexmock's pytest integration switched to use native pytest hooks and a direct `flexmock` fixture. While older `pytest-flexmock` plugins might still function, the recommended approach is to rely on the library's built-in support.
fix
Remove explicit `pytest-flexmock` dependencies if you were using an older integration approach. Ensure `flexmock` is installed, and pytest will discover its plugin. Use `flexmock` as a fixture in your tests: `def test_something(flexmock): ...`.
affects: >=0.12.0
Errors
Common errors & fixes
AttributeError: 'Mock' object has no attribute 'some_method'
You are trying to call a method or access an attribute on a mocked object that was not explicitly defined or allowed by the mock. This means the mock did not correctly intercept the call or the method wasn't mocked.
fix
Ensure the `should_receive()` chain correctly targets the method you intend to mock, including its name and any arguments if necessary. For attributes, use `and_return()` directly or `set()`.
AssertionError: expected 'method_name' to be called once but was called 0 times
This error occurs during mock verification (`should_have_received`) when the mocked method was never actually called by the code under test, or was called on a different object than the one mocked.
fix
Double-check that your code under test indeed calls the method you mocked, and that it operates on the exact object you set up the mock for. Pay attention to class vs. instance mocks.
My tests are flaky or failing intermittently when run in a suite, but pass individually.
This is a classic sign of mocks not being properly reset between test runs, leading to state leakage and interference between tests.
fix
Ensure `flexmock_teardown()` is called after every test. If using `unittest`, call it in `tearDown()`. If using `pytest`, use the `flexmock` fixture in your tests (`def test_my_feature(flexmock): ...`) or create an `autouse` fixture to ensure `flexmock_teardown()` runs.
TypeError: 'Mock' object is not subscriptable
You are attempting to access a mocked object using dictionary-like subscription (e.g., `mock_obj['key']`) without explicitly mocking the `__getitem__` method.
fix
To mock dictionary-like access, explicitly mock the `__getitem__` method: `flexmock(obj).should_receive('__getitem__').with_args('key').and_return('value')`.
Upgrade
Version history
0.13.0latest on PyPI · released Jan 10, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources