Skip to main content

pytest-mccabe

https://travis-ci.org/The-Compiler/pytest-mccabe.svg?branch=master

pytest plugin for checking cyclomatic complexity of python source with mccabe.

NOTE: I (@The-Compiler) stopped using this plugin in 2016. While I will still review pull requests and release new versions if needed by the community, I do not have the time to continue maintaining this plugin myself. You might want to consider switching to pytest-flake8 or tox + flake8 instead (see some arguments on why).

Usage

install via:

pip install pytest-mccabe

if you then type:

pytest --mccabe

every file ending in .py will be discovered and run through mccabe, starting from the command line arguments.

Simple usage example

Consider you have this (deliberately bad and complex) code:

# module.py

import random
import os.path

def some_function():
    num = random.random()
    if 0 <= num < 0.1:
        print("1")
    elif 0.1 <= num < 0.2:
        print("2")
    elif 0.2 <= num < 0.3:
        print("3")
    elif 0.3 <= num < 0.4:
        print("4")
    elif 0.4 <= num < 0.5:
        print("5")
    elif 0.5 <= num < 0.6:
        print("6")
    elif 0.6 <= num < 0.7:
        print("7")
    elif 0.7 <= num < 0.8:
        print("8")
    elif 0.8 <= num < 0.9:
        print("9")
    elif 0.9 <= num < 1:
        print("10")

Running pytest with pytest-mccabe installed shows you this function is considered too complex:

$ pytest -q --mccabe module.py
F
============================== FAILURES ==============================
____________________________ mccabe-check ____________________________
.../module.py:4: C901 'some_function' is too complex (11)

Configuring mccabe complexity per project and file

You may configure the maximum complexity for your project by adding an mccabe-complexity entry to pytest config file (e.g. setup.cfg) like this:

[pytest]
mccabe-complexity=15

Rerunning with the above example will now look better:

$ pytest -q --mccabe foo.py
.
1 passed in 0.00 seconds

If you have some files where you want to set a higher complexity than the project-wide one, you can start a mccabe-complexity line with a glob-pattern:

[pytest]
mccabe-complexity =
    *.py 7
    magic.py 10

Ignoring certain functions

You can exclude certain functions from the complexity check by adding comments like this:

def some_function():  # noqa
    ...

def another_function():  # pragma: no mccabe
    ...

(both will work - # noqa is mainly there for flake8 compatibility)

Running mccabe checks and no other tests

You can restrict your test run to only perform “mccabe” tests and not any other tests by typing:

pytest --mccabe -m mccabe

This will only run tests that are marked with the “mccabe” keyword which is added for the mccabe test items added by this plugin.

If you are using pytest < 2.4, then use the following invocation to the same effect:

pytest --mccabe -k mccabe

Notes

The repository of this plugin is at https://github.com/The-Compiler/pytest-mccabe

For more info on pytest see https://pytest.org

The code is based on Florian Schulze’s excellent pytest-flakes - Thanks!

Changes

0.1

  • Initial release.

1.0

  • Drop support for Python 2.6, 3.2, 3.3 and 3.4

  • Add support for Python 3.5, 3.6 and 3.7

  • Make this plugin compatible with pytest 4.x and 5.x

  • Drop support for “setup.py test”

  • Remove outdated pytest-cache dependency

  • Various code/CI cleanups

2.0

  • Drop support for Python 2.7

  • Drop support for pytest < 5.4

  • Add support for Python 3.8 (no changes required)

  • Fix deprecation warnings with pytest >= 5.4 (thanks to Martin Thoma for the PR!)

  • Various code/CI cleanups

Release files for pytest-mccabe 2.0

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

Source distribution (sdist)

Source distribution for pytest-mccabe 2.0
File Size Uploaded
pytest-mccabe-2.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-mccabe 2.0
File Interpreter ABI Platform
pytest_mccabe-2.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 12.9 kB

Release files / pytest-mccabe-2.0.tar.gz

Download URL pytest-mccabe-2.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
050a1f1c145202ea87f0c0ad7b529f088098040fe2eb8e5d797d36be86e0fa20
BLAKE2b-256 checksum
How to use checksums
9d442f99366f1bf6f38cfb35da47446f364dfe25c8b7e5a781c0f3bb61840205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

Release files / pytest_mccabe-2.0-py2.py3-none-any.whl

Download URL pytest_mccabe-2.0-py2.py3-none-any.whl
Size 6.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9b1708efd52b8724dedcfe48ec668ab5bda2f7ffb1c85a2edcd4f36541307d04
BLAKE2b-256 checksum
How to use checksums
5497052f65a1b55131f4f3b7c028cd31fe5ec4acff296c33e9bfe17bddc5f129
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

2.0 This release

2 release files

1.0

2 release files

0.1

2 release files

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