Skip to main content

CLI tool to assert that each pytest test function contains exactly one assert statement

Project description

assert-one-assert-per-pytest

Assert that each pytest test function contains exactly one assert.

Why One Assert Per Test?

The "one assert per test" pattern encourages writing focused, atomic tests that verify a single behavior. Benefits include:

  • Clear failure messages: When a test fails, you know exactly what behavior broke
  • Better test names: Tests naturally describe specific behaviors
  • Easier maintenance: Changes to one behavior don't affect unrelated assertions
  • Faster debugging: No need to hunt through multiple assertions

Installation

pip install assert-one-assert-per-pytest

Usage

Command Line

# Scan specific files
assert-one-assert-per-pytest test_example.py

# Scan directories recursively
assert-one-assert-per-pytest tests/

# Use glob patterns
assert-one-assert-per-pytest "tests/**/test_*.py"

# Exclude patterns
assert-one-assert-per-pytest tests/ --exclude "**/conftest.py"

# Verbose output
assert-one-assert-per-pytest tests/ --verbose

# Fail fast (exit on first finding)
assert-one-assert-per-pytest tests/ --fail-fast

# Warn only (always exit 0)
assert-one-assert-per-pytest tests/ --warn-only

GitHub Actions

- uses: 10U-Labs-LLC/assert-one-assert-per-pytest@v1
  with:
    files: "tests/"
    exclude: "**/conftest.py"
    verbose: "true"

As a Python Module

python -m assert_one_assert_per_pytest tests/

Output Format

Default output shows one finding per line:

path/to/test_file.py:10:test_example:0
path/to/test_file.py:25:test_another:3

Format: file_path:line_number:function_name:assert_count

Exit Codes

  • 0: No findings (or --warn-only specified)
  • 1: Findings detected
  • 2: Error (missing files, syntax errors, etc.)

What Counts as an Assert?

This tool counts Python assert statements at the immediate level of test functions. It also counts:

  • pytest.raises() context managers
  • pytest.warns() context managers

It does not count:

  • Assertions in nested functions or classes
  • Helper assertions in fixtures or utility functions

Options

Option Description
--exclude PATTERNS Glob patterns to exclude (comma-separated)
--quiet Suppress all output (exit code only)
--verbose Show detailed processing information
--fail-fast Exit after first finding
--warn-only Always exit 0, even with findings

License

Apache 2.0 - See LICENSE.txt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file assert_one_assert_per_pytest-20260104040549.tar.gz.

File metadata

File hashes

Hashes for assert_one_assert_per_pytest-20260104040549.tar.gz
Algorithm Hash digest
SHA256 da215f3b66caa8df12a83b8351aaba53b5c76a43b09551070b573ca9e7668869
MD5 07a4e4be3a40eb8f8f0be276aa7c8e29
BLAKE2b-256 5057f0c6ba435d0d90f57ff6497fa40ce68b333574ec8f6cf68659dda3fcb9a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for assert_one_assert_per_pytest-20260104040549.tar.gz:

Publisher: release.yml on 10U-Labs-LLC/assert-one-assert-per-pytest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file assert_one_assert_per_pytest-20260104040549-py3-none-any.whl.

File metadata

File hashes

Hashes for assert_one_assert_per_pytest-20260104040549-py3-none-any.whl
Algorithm Hash digest
SHA256 bbd7b97e680463615e2165dc9b0905620756cab2f392234d5ff7ede0a835e53b
MD5 fdf061770a2353900714c4a7471b7bab
BLAKE2b-256 c2120b7109581ed8e0847a449ef8b2f4f5a8b92bf4dc4859614a85e16821ff74

See more details on using hashes here.

Provenance

The following attestation bundles were made for assert_one_assert_per_pytest-20260104040549-py3-none-any.whl:

Publisher: release.yml on 10U-Labs-LLC/assert-one-assert-per-pytest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page