Skip to main content
Version: Next

Poetry Run Test Workflow

View Source

Run Python tests using Poetry dependency management with a single Python version.

Overviewโ€‹

This workflow executes Python tests using Poetry for dependency management. It's designed for projects that use Poetry as their package manager and need to run tests with a specific Python version.

When to Useโ€‹

  • โœ… Your project uses Poetry for dependency management
  • โœ… You need to test with a single Python version
  • โœ… You want Poetry to manage test dependencies
  • โœ… You prefer Poetry's virtual environment handling

Workflow Architectureโ€‹

Inputsโ€‹

Required Inputsโ€‹

InputTypeDescription
python_versionstringPython version to use (e.g., '3.11', '3.12')
test_typestringType of tests to run (e.g., 'unit-test', 'integration-test')
all_test_items_pathsstringPaths to test files or directories

Optional Inputsโ€‹

InputTypeDefaultDescription
poetry_versionstring'latest'Poetry version to install
pytest_argsstring''Additional pytest arguments
coverage_formatstring'xml'Coverage report format (xml, html, term)

Outputsโ€‹

OutputDescription
coverage_reportPath to generated coverage report
test_resultsTest execution results summary

Usage Examplesโ€‹

Basic Usageโ€‹

name: CI

on: [push, pull_request]

jobs:
test:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_poetry_run_test.yaml@master
with:
python_version: '3.11'
test_type: unit-test
all_test_items_paths: test/unit_test/

With Custom Poetry Versionโ€‹

jobs:
test:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_poetry_run_test.yaml@master
with:
python_version: '3.12'
test_type: integration-test
all_test_items_paths: test/integration_test/
poetry_version: '1.7.0'

With Additional Pytest Argumentsโ€‹

jobs:
test:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_poetry_run_test.yaml@master
with:
python_version: '3.11'
test_type: unit-test
all_test_items_paths: test/unit_test/
pytest_args: '-v --tb=short --strict-markers'

Complete CI Pipelineโ€‹

name: Complete CI

on: [push, pull_request]

jobs:
unit-tests:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_poetry_run_test.yaml@master
with:
python_version: '3.11'
test_type: unit-test
all_test_items_paths: test/unit_test/
coverage_format: xml

integration-tests:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_poetry_run_test.yaml@master
with:
python_version: '3.11'
test_type: integration-test
all_test_items_paths: test/integration_test/
pytest_args: '-v --maxfail=1'

How It Worksโ€‹

Step 1: Environment Setupโ€‹

The workflow sets up the Python environment and installs Poetry:

- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python_version }}
- uses: snok/install-poetry@v1
with:
version: ${{ inputs.poetry_version }}

Step 2: Dependency Installationโ€‹

Poetry installs all dependencies from pyproject.toml:

poetry install --with dev

Step 3: Test Executionโ€‹

Runs tests using Poetry's virtual environment:

poetry run pytest ${{ inputs.all_test_items_paths }} \
--cov \
--cov-report=${{ inputs.coverage_format }} \
${{ inputs.pytest_args }}

Step 4: Artifact Uploadโ€‹

Uploads coverage reports and test results as artifacts.

Project Requirementsโ€‹

pyproject.toml Configurationโ€‹

Your project must have a pyproject.toml file:

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = "My Python project"

[tool.poetry.dependencies]
python = "^3.11"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Test Structureโ€‹

Organize tests in a clear directory structure:

project/
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ poetry.lock
โ”œโ”€โ”€ src/
โ”‚ โ””โ”€โ”€ my_package/
โ”‚ โ””โ”€โ”€ __init__.py
โ””โ”€โ”€ test/
โ”œโ”€โ”€ unit_test/
โ”‚ โ””โ”€โ”€ test_module.py
โ””โ”€โ”€ integration_test/
โ””โ”€โ”€ test_integration.py

Best Practicesโ€‹

1. Lock File Managementโ€‹

Always commit poetry.lock to ensure reproducible builds:

git add poetry.lock
git commit -m "Update dependencies"

2. Dependency Groupsโ€‹

Use Poetry dependency groups for test dependencies:

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.0"

3. Coverage Configurationโ€‹

Configure coverage in pyproject.toml:

[tool.coverage.run]
source = ["src"]
omit = ["*/tests/*", "*/test_*.py"]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise NotImplementedError",
]

4. Pytest Configurationโ€‹

Configure pytest in pyproject.toml:

[tool.pytest.ini_options]
testpaths = ["test"]
python_files = "test_*.py"
python_classes = "Test*"
python_functions = "test_*"
addopts = "-v --strict-markers"

Troubleshootingโ€‹

Poetry Installation Failsโ€‹

Symptoms:

  • Poetry installation errors
  • Version conflicts

Solutions:

  1. Specify exact Poetry version:
    poetry_version: '1.7.0'
  2. Check Python version compatibility
  3. Review Poetry installation logs

Dependency Installation Failsโ€‹

Symptoms:

  • poetry install fails
  • Lock file conflicts

Solutions:

  1. Update poetry.lock:
    poetry lock --no-update
  2. Check dependency compatibility
  3. Review pyproject.toml syntax

Tests Not Foundโ€‹

Symptoms:

  • "No tests collected" error
  • Pytest can't find tests

Solutions:

  1. Verify test path is correct:
    all_test_items_paths: test/unit_test/
  2. Check test file naming (must start with test_)
  3. Ensure __init__.py files exist in test directories

Coverage Report Missingโ€‹

Symptoms:

  • No coverage report generated
  • Coverage artifact not uploaded

Solutions:

  1. Verify pytest-cov is installed
  2. Check coverage configuration
  3. Ensure tests actually run

Comparison with Other Test Workflowsโ€‹

Featurerw_poetry_run_testrw_run_testrw_poetry_run_test_with_multi_py_versions
Dependency ManagerPoetrypip/uvPoetry
Python VersionsSingleSingleMultiple
Lock Filepoetry.lockrequirements.txtpoetry.lock
Virtual EnvPoetry managedActions managedPoetry managed
Best ForPoetry projectsSimple projectsPoetry + multi-version

Additional Resourcesโ€‹