pytest_test_categories.formatting

Formatting utilities for test distribution reports.

This module provides pure functions for formatting test distribution data for terminal output. All functions are testable without pytest dependencies.

These utilities follow functional programming principles: - Pure functions with no side effects - Depend only on input parameters - Return formatted strings - Fully testable with simple assertions

Example

>>> pluralize_test(1)
'test'
>>> pluralize_test(5)
'tests'
>>> format_distribution_row('Small', 10, 50.0)
'      Small      10 tests (50.00%)'

Attributes

Functions

format_distribution_row(size, count, percentage)

Format a single row of the distribution table.

get_status_message(percentages)

Get the status message based on distribution percentages.

pluralize_test(count)

Return 'test' or 'tests' based on count.

Module Contents

pytest_test_categories.formatting.format_distribution_row(size, count, percentage)[source]

Format a single row of the distribution table.

This is a pure function that formats a distribution row with consistent spacing and formatting.

Parameters:
  • size (str) – The test size category name (e.g., ‘Small’, ‘Medium’).

  • count (int) – Number of tests in this category.

  • percentage (float) – Percentage of tests in this category.

Returns:

Formatted row string with fixed-width columns.

Return type:

str

Example

>>> format_distribution_row('Small', 10, 50.0)
'      Small      10 tests (50.00%)'
>>> format_distribution_row('Medium', 1, 5.5)
'      Medium      1 test  (5.50%)'
pytest_test_categories.formatting.get_status_message(percentages)[source]

Get the status message based on distribution percentages.

This is a pure function that determines which status message to display based on the distribution percentages. It prioritizes the most severe deviation from target distribution.

The priority order is: 1. Large/XLarge percentage too high (>8%) 2. Small percentage critically low (<50%) 3. Medium percentage too high (>20%) 4. Small percentage moderately low (<75%) 5. All good (success message)

Parameters:

percentages (pytest_test_categories.distribution.stats.TestPercentages) – The current test distribution percentages.

Returns:

List of message lines to display (suitable for join or iteration).

Return type:

list[str]

Example

>>> from pytest_test_categories.distribution.stats import TestPercentages
>>> percentages = TestPercentages(small=80.0, medium=15.0, large=5.0, xlarge=0.0)
>>> lines = get_status_message(percentages)
>>> 'Great job!' in ' '.join(lines)
True
pytest_test_categories.formatting.pluralize_test(count)[source]

Return ‘test’ or ‘tests’ based on count.

This is a pure function that returns the correct singular or plural form of ‘test’ based on the count value.

Parameters:

count (int) – The number of tests.

Returns:

‘test’ if count is 1, ‘tests’ otherwise.

Return type:

str

Example

>>> pluralize_test(1)
'test'
>>> pluralize_test(0)
'tests'
>>> pluralize_test(5)
'tests'
pytest_test_categories.formatting.CRITICAL_SMALL_PCT: Final[float] = 50.0[source]
pytest_test_categories.formatting.CRITICAL_SMALL_WARNING: Final[str] = Multiline-String[source]
Show Value
"""    Status: Warning! Distribution needs improvement:
      Small tests are only {small:.2f}% of the suite (target: 75-85%)
      This indicates tests may be too complex. Consider:
      • Breaking down medium tests into smaller units
      • Testing more specific behaviors individually
      • Moving complex setup into fixtures or helpers
"""
pytest_test_categories.formatting.LARGE_XLARGE_WARNING: Final[str] = Multiline-String[source]
Show Value
"""    Status: Warning! Distribution needs improvement:
      Large/XLarge tests are {large_xlarge_percentage:.0f}% of the suite (target: 2-8%)
      This indicates too many complex tests. Consider:
      • Breaking large tests into smaller focused tests
      • Moving test setup into fixtures
      • Using test parameterization for repeated scenarios
"""
pytest_test_categories.formatting.MAX_LARGE_XLARGE_PCT: Final[float] = 8.0[source]
pytest_test_categories.formatting.MAX_MEDIUM_PCT: Final[float] = 20.0[source]
pytest_test_categories.formatting.MEDIUM_WARNING: Final[str] = Multiline-String[source]
Show Value
"""    Status: Warning! Distribution needs improvement:
      Medium tests are {medium:.2f}% of the suite (target: 10-20%)
      This suggests test complexity is creeping up. Consider:
      • Identifying shared setup that could be simplified
      • Looking for tests that could be split into smaller units
      • Reviewing test dependencies and fixture usage
"""
pytest_test_categories.formatting.MIN_SMALL_PCT: Final[float] = 75.0[source]
pytest_test_categories.formatting.MODERATE_SMALL_WARNING: Final[str] = Multiline-String[source]
Show Value
"""    Status: Warning! Distribution needs improvement:
      Small tests are only {small:.2f}% of the suite (target: 75-85%)
      This indicates tests may be too complex. Consider:
      • Breaking down medium tests into smaller units
      • Testing more specific behaviors individually
      • Moving complex setup into fixtures or helpers
"""
pytest_test_categories.formatting.SUCCESS_MESSAGE: Final[str] = Multiline-String[source]
Show Value
"""    Status: Great job! Your test distribution is on track.
"""