skillfed

flexmock

flexmock is a testing library for Python that makes it easy to create mocks, stubs and fakes.

flexmock v0.13.0 149.0K downloads/30d#11,007 on PyPI65
Permissive license BSD-2-Clause Active released

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 flexmock

uv

uv add flexmock

poetry

poetry add flexmock

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: TestingTopic :: Software Development :: Testing :: MockingTopic :: Software Development :: Testing :: UnitTyping :: Typed

Tags

python mocking librarymock objects testingspy and stub frameworkpytest mock alternativeunittest mock replacementfake object generatortest double creation
testingmockingtype-annotated

More Testing packages