Generated by GPT-5-mini| Mock (software) | |
|---|---|
| Name | Mock |
| Title | Mock |
| Developer | Michael Foord et al. |
| Initial release | 2006 |
| Programming language | Python |
| Operating system | Cross-platform |
| License | BSD License |
Mock (software) is a Python library for creating mock objects, test doubles, and spies to support unit testing in software development. It provides tools to replace parts of a system under test with controllable and observable substitutes, enabling isolated verification of behavior. Originating in the early 2000s, Mock became widely adopted within the Python community and influenced testing practices in projects across industry and open source.
Mock implements an API for creating test doubles and spys that emulate interfaces of real objects from libraries such as Python (programming language) standard modules and third-party packages like Django and NumPy. It integrates with testing frameworks including unittest, pytest, and nose to facilitate assertions about interactions with dependencies. Authors such as Michael Foord and contributors from organizations interact with ecosystems represented by GitHub repositories and continuous integration services like Travis CI and CircleCI during maintenance and release cycles.
Mock's core features include creation of MagicMocks and Mock instances that support attribute access, call tracking, and configurable return values. It provides support for context managers, iterator protocols, and descriptor behavior to mimic objects from libraries such as requests, SQLAlchemy, and Celery. The architecture centers on dynamic attribute generation and method wrapping, using metaprogramming facilities offered by Python (programming language) such as descriptors and the inspect module. Integration points enable patching via decorators and context managers to replace objects inside modules like os, subprocess, and logging during tests. The design emphasizes a minimal public surface for compatibility with unittest's TestCase and compatibility layers with external runners such as pytest and nose2.
Typical use cases include isolating units that interact with IO-bound libraries such as boto3, psycopg2, or smtplib; simulating time-dependent behavior in conjunction with libraries like freezegun; and stubbing database connectors such as SQLAlchemy engines during model tests. Mock is commonly used alongside continuous integration pipelines involving platforms like Travis CI and GitHub Actions to ensure deterministic tests for projects maintained by organizations including Mozilla, Red Hat, and Canonical. Integration with test runners enables parametrized testing patterns familiar to users of pytest fixtures and unittest setUp/tearDown semantics, while patching primitives allow targeted replacement of call sites in frameworks such as Flask and Django Rest Framework.
Compared to libraries like Mockito (Java) and Sinon.JS (JavaScript), Mock focuses on Python-specific idioms and leverages the dynamic nature of Python (programming language) for runtime behavior alteration. In the Python ecosystem, alternatives include unittest.mock (the standard library backport), third-party options such as flexmock and moto (for AWS), each emphasizing different trade-offs: unittest.mock offers standardization and broad availability, while Mock and other libraries may provide API conveniences or specialized utilities for projects like OpenStack and Ansible. Community discussions around Mock often reference interactions with packaging tools like pip and distribution platforms like PyPI when addressing compatibility and dependency management.
Basic usage involves creating a Mock, configuring return values, and asserting calls. For example, tests that replace a function in os.path or a method on a SQLAlchemy session use patch decorators to inject a mock object. Mock supports configuring side effects to raise exceptions from libraries such as requests to simulate HTTP failures, or to yield sequences when emulating iterators consumed by code that integrates with Pandas data pipelines. Examples in project repositories frequently demonstrate combining Mock with assertion helpers provided by unittest TestCase subclasses and matcher libraries used in projects like Celery tests.
Mock's development began as a third-party package authored by Michael Foord in the mid-2000s and evolved through community contributions hosted on platforms like SourceForge and GitHub. Over time, parts of Mock's API and ideas influenced Python's inclusion of a mock library into the standard library, leading to discussions among contributors and maintainers associated with organizations such as Python Software Foundation and projects within the OpenStack ecosystem. Release practices have followed semantic versioning patterns and adopted continuous integration and automated testing workflows used by projects maintained on GitHub and mirrored to package indexes like PyPI. Maintenance continues through pull requests and issue tracking managed by the community and major stakeholders in infrastructure projects such as Ubuntu and Debian packaging teams.
Category:Software testing