7-13. pytest + GitHub Actions による CI/CD テスト自動化環境

コードの変更時に品質チェック(Linting)および単体テストが自動実行される堅牢な開発環境です。

Python

# test_math_utils.py (テストコード)
import pytest

def multiply(a: int, b: int) -> int:
    return a * b

def test_multiply():
    assert multiply(3, 4) == 12
    assert multiply(-1, 5) == -5

@pytest.mark.parametrize("a, b, expected", [(2, 3, 6), (0, 5, 0), (10, 10, 100)])
def test_multiply_parametrized(a, b, expected):
    assert multiply(a, b) == expected

YAML

# .github/workflows/python-ci.yml (CIパイプライン定義)
name: Python Automation CI

on: [push, pull_request]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pytest pytest-cov
      - name: Run Unit Tests
        run: pytest --cov=.

コメント

タイトルとURLをコピーしました