Skip to content

Latest commit

 

History

History
181 lines (135 loc) · 4.11 KB

File metadata and controls

181 lines (135 loc) · 4.11 KB

forge-ci-python-tools-pytest

Run pytest tests without coverage reporting.

Usage

jobs:
  test:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-pytest.yml@v1
    with:
      dependencies-cache-key: ${{ needs.dependencies.outputs.dependencies-cache-key }}

Inputs

Input Type Required Default Description
python-version string No '3.12' Python version to use
working-directory string No '.' Working directory for tests
dependencies-cache-key string No '' Cache key from dependency-manager job
pytest-args string No '' Additional pytest arguments
runs-on string No 'ubuntu-latest' Runner to use

Outputs

None

Behavior

  1. Restore Cache: Restores dependencies if cache key provided
  2. Add to PATH: Adds .venv/bin to PATH for pytest access
  3. Run pytest: Executes tests with optional custom arguments
    • Default: pytest -q (quiet mode)
    • With args: pytest {custom-args}

Examples

With Cached Dependencies

jobs:
  dependencies:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-dependency-manager.yml@v1

  test:
    needs: dependencies
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-pytest.yml@v1
    with:
      dependencies-cache-key: ${{ needs.dependencies.outputs.dependencies-cache-key }}

With Custom Arguments

jobs:
  test:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-pytest.yml@v1
    with:
      pytest-args: '-v --tb=short tests/unit/'

Parallel Tests with Matrix

jobs:
  dependencies:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-dependency-manager.yml@v1

  test:
    needs: dependencies
    strategy:
      matrix:
        test-dir: [tests/unit, tests/integration, tests/e2e]
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-pytest.yml@v1
    with:
      dependencies-cache-key: ${{ needs.dependencies.outputs.dependencies-cache-key }}
      pytest-args: '${{ matrix.test-dir }}'

Standalone (No Cache)

jobs:
  test:
    uses: whisller/forge-ci/.github/workflows/forge-ci-python-tools-pytest.yml@v1

Configuration

pytest configuration in pyproject.toml:

[tool.pytest.ini_options]
minversion = "7.0"
addopts = "-ra -q --strict-markers"
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
markers = [
    "slow: marks tests as slow",
    "integration: marks tests as integration tests",
]

Or separate pytest.ini:

[pytest]
minversion = 7.0
addopts = -ra -q --strict-markers
testpaths = tests
python_files = test_*.py *_test.py
markers =
    slow: marks tests as slow
    integration: marks tests as integration tests

Troubleshooting

pytest Not Found

Problem: pytest: command not found

Cause: pytest not installed in dependencies.

Solution: Add pytest to dev dependencies:

# Poetry
poetry add --group dev pytest

# uv
uv add --dev pytest

# venv
echo "pytest>=7.0.0" >> requirements-dev.txt

Import Errors

Problem: ModuleNotFoundError when running tests.

Cause: Project not installed or PYTHONPATH not set.

Solution: Ensure project is installed in development mode:

# Poetry (automatic with poetry install)
poetry install

# uv
uv sync

# venv with pyproject.toml
pip install -e .

# venv with setup.py
pip install -e .

No Tests Found

Problem: pytest exits with "no tests ran in X seconds"

Cause: Test discovery not finding test files.

Solution: Check test discovery settings:

[tool.pytest.ini_options]
# Ensure testpaths points to correct directory
testpaths = ["tests"]

# Check naming patterns match your test files
python_files = ["test_*.py", "*_test.py"]

See Also