Skip to main content

Flake8 Mock plugin

Remember that a mock’s job is to say, “You got it, boss” whenever anyone calls it. It will do real work, like raising an exception, when one of its convenience methods is called, like assert_called_once_with. But it won’t do real work when you call a method that only resembles a convenience method, such as assert_called_once (no _with!). Sometimes developers may not notice that they are using a non-existent mock method, because they are not getting an output error telling them so. And for some reason they can also forget to verify that the test cases fail before writing implementation code.

This plugin checks for possible non-existent mock methods when you run flake8, the Python code checker.

Inspired by http://engineeringblog.yelp.com/2015/02/assert_called_once-threat-or-menace.html.

Installation

You can install or upgrade flake8-mock with these commands:

$ pip install flake8-mock
$ pip install --upgrade flake8-mock

List of non-existent methods checked

  • assert_calls

  • not_called

  • called_once

  • called_once_with

Plugin for Flake8

When both flake8 and flake8-mock are installed, the plugin is available in flake8:

$ flake8 --version
5.0.4 (flake8-mock: 0.4, ...)

Example output

Once you run flake8, you can have something like:

$ flake8 test_file.py
test_file.py:27:1: M001 assert_calls is a non-existent mock method.
test_file.py:28:1: M001 called_once_with is a non-existent mock method.
test_file.py:39:1: M001 not_called is a non-existent mock method.
test_file.py:40:1: M001 assert_called is a non-existent mock method.

Credits

Collaborators

Changes

0.4 (05-12-2023)

  • Fix compatibility with flake8 >= 5.0.0.

0.3 (09-10-2016)

  • Don’t warn on assert_not_called, assert_called or assert_called_once.

  • Use ASCII only in README.rst

0.2 (12-16-2015)

  • Add Python 3 compatibility.

0.1 (10-20-2015)

  • First release.

0.1dev0 (10-19-2015)

  • First dev release.

Metadata

Release files for flake8-mock 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-mock 0.4
File Size Uploaded
flake8-mock-0.4.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-mock 0.4
File Interpreter ABI Platform
flake8_mock-0.4-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / flake8-mock-0.4.tar.gz

Download URL flake8-mock-0.4.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4a05bac5f66e77661994880dd050705132d19000f17d928a894dfd92d55d4867
BLAKE2b-256 checksum
How to use checksums
2a1839ccc52955b94cd61c8719292753ef4fd74d79c8d572b97e61de3a56beeb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / flake8_mock-0.4-py3-none-any.whl

Download URL flake8_mock-0.4-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a67c3d22b2e7873c72d3f01d3eb5d06405cd09dc1abea74a0bf6fcf29095e8e6
BLAKE2b-256 checksum
How to use checksums
cbe8b2447852d1ffa97675869ceea3588f87a0a56a0e37c65ae660f5ea827f00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page