Skip to main content
Version: Next

Pre-Building Test Workflow

View Source

Test package installation before deployment.

Overviewโ€‹

This workflow validates that the built Python package can be installed and imported correctly before publishing to PyPI, preventing broken package deployments.

When to Useโ€‹

  • โœ… You want to validate package installation
  • โœ… You need to test package imports
  • โœ… You're preparing for PyPI deployment
  • โœ… You want to catch packaging errors early

Inputsโ€‹

InputTypeDefaultDescription
package_pathstring'dist/'Path to built packages
python_versionstring'3.11'Python version for testing
test_importbooleantrueTest package import
package_namestring''Package name to import

Outputsโ€‹

OutputDescription
installation_successWhether installation succeeded
import_successWhether import test succeeded

Usage Examplesโ€‹

Basic Usageโ€‹

jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master

test-install:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
package_name: my_package

Multi-Version Testingโ€‹

jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master

test-install-py311:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
python_version: '3.11'
package_name: my_package

test-install-py312:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
python_version: '3.12'
package_name: my_package

Complete Build and Deploy Pipelineโ€‹

jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master

test-install:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
package_name: my_package

publish:
needs: test-install
if: needs.test-install.outputs.installation_success == 'true'
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_push_pypi.yaml@master
secrets:
pypi_token: ${{ secrets.PYPI_API_TOKEN }}

How It Worksโ€‹

Step 1: Download Packageโ€‹

Downloads built package from artifacts:

- uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/

Step 2: Install Packageโ€‹

Installs the wheel package:

pip install dist/*.whl

Step 3: Test Importโ€‹

Tests that package can be imported:

import sys
try:
import my_package
print(f"Successfully imported {my_package.__name__}")
print(f"Version: {my_package.__version__}")
sys.exit(0)
except ImportError as e:
print(f"Failed to import: {e}")
sys.exit(1)

Step 4: Validate Metadataโ€‹

Checks package metadata:

pip show my-package

Best Practicesโ€‹

1. Test All Supported Python Versionsโ€‹

strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']

2. Test Both Wheel and Source Distributionโ€‹

- name: Test wheel installation
run: pip install dist/*.whl

- name: Test sdist installation
run: pip install dist/*.tar.gz

3. Validate Package Entry Pointsโ€‹

# Test CLI entry points
import subprocess
result = subprocess.run(['my-command', '--version'], capture_output=True)
assert result.returncode == 0

4. Check Dependenciesโ€‹

pip check  # Verify no dependency conflicts

Troubleshootingโ€‹

Installation Failsโ€‹

Symptoms:

  • Package installation errors
  • Dependency conflicts

Solutions:

  1. Check package dependencies:

    [project]
    dependencies = [
    "requests>=2.28.0",
    ]
  2. Verify package structure

  3. Test locally:

    pip install dist/*.whl

Import Failsโ€‹

Symptoms:

  • ImportError after installation
  • Module not found

Solutions:

  1. Verify package name matches:

    import my_package  # Must match package name
  2. Check package structure:

    src/
    โ””โ”€โ”€ my_package/
    โ””โ”€โ”€ __init__.py
  3. Ensure __init__.py exists