flexmock

0.13.0 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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()

view raw JSON →