{"id":9754,"library":"flexmock","title":"flexmock","description":"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.","status":"active","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/flexmock/flexmock","tags":["testing","mocking","stubbing","fakes","unittest","pytest"],"install":[{"cmd":"pip install flexmock","lang":"bash","label":"Install flexmock"}],"dependencies":[],"imports":[{"symbol":"flexmock","correct":"from flexmock import flexmock"},{"note":"flexmock_teardown is exposed directly at the top level of the flexmock package, not within a 'mock' submodule.","wrong":"from flexmock.mock import flexmock_teardown","symbol":"flexmock_teardown","correct":"from flexmock import flexmock_teardown"}],"quickstart":{"code":"import datetime\nfrom flexmock import flexmock, flexmock_teardown\n\ndef get_today_str():\n    return datetime.date.today().isoformat()\n\n# --- Example Test (usually in a test file) ---\n# The flexmock_teardown() is crucial for cleaning up mocks.\n# In pytest, use the flexmock fixture. In unittest, call it in tearDown.\n# For a standalone script, call it manually.\n\ntry:\n    # Mock the 'today' method of the datetime.date class\n    flexmock(datetime.date).should_receive('today').and_return(datetime.date(2023, 10, 26))\n\n    # Call the function under test\n    result = get_today_str()\n\n    # Assert the result\n    assert result == '2023-10-26'\n    print(f\"Test passed: Today's date is mocked to {result}\")\n\nfinally:\n    # Ensure mocks are cleaned up\n    flexmock_teardown()","lang":"python","description":"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."},"warnings":[{"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.","message":"flexmock 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.","severity":"breaking","affected_versions":"0.12.0, 0.12.1, 0.13.0"},{"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()`.","message":"Mocks 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.","severity":"gotcha","affected_versions":"All versions"},{"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(...)`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"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): ...`.","message":"As 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.","severity":"deprecated","affected_versions":">=0.12.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"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()`.","cause":"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.","error":"AttributeError: 'Mock' object has no attribute 'some_method'"},{"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.","cause":"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.","error":"AssertionError: expected 'method_name' to be called once but was called 0 times"},{"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.","cause":"This is a classic sign of mocks not being properly reset between test runs, leading to state leakage and interference between tests.","error":"My tests are flaky or failing intermittently when run in a suite, but pass individually."},{"fix":"To mock dictionary-like access, explicitly mock the `__getitem__` method: `flexmock(obj).should_receive('__getitem__').with_args('key').and_return('value')`.","cause":"You are attempting to access a mocked object using dictionary-like subscription (e.g., `mock_obj['key']`) without explicitly mocking the `__getitem__` method.","error":"TypeError: 'Mock' object is not subscriptable"}]}