継続的インテグレーションによるテストの実行#
テストスイートをローカルで実行する ことは、コードを開発したり、新しい機能やコード・ベースの変更をテストしたりするときに便利です。しかし、テストをオンラインで実行するために、継続的インテグレーション(CI)もセットアップしたいです。CIを使えば、すべてのテストをクラウド上で実行できます。特定のオペレーティングシステム上でしかローカルにテストを実行できないかもしれませんが、CIを使用すると、Pythonのさまざまなバージョンや異なるオペレーティングシステム間で実行するテストを指定できます。
CIは、プルリクエストやリポジトリへのプッシュをトリガーすることもできます。 これは、あなた、あなたのメンテナチーム、またはコントリビューターが提出するすべてのプルリクエストをテストできることを意味します。 最終的にCIテストは、コードベースに変更が加えられても、あなたのコードが期待通りに実行され続けることを保証します。
CIとプルリクエスト#
CIは、外部の人があなたのソフトウェアに貢献している場合、非常に貴重です。 リポジトリに投稿されたすべてのプルリクエストに対してCIを実行するように設定できます。 CIは、あなたのリポジトリを新しい将来の貢献者によりフレンドリーなものにします。 開発環境の構築、テストの実行、ドキュメントのビルドをローカルで行うことなく、コードやドキュメントの修正などに貢献することができます。
Example GitHub Actions that runs tests#
Below is an example GitHub Actions that runs tests using nox across both Windows, Mac and Linux and on Python versions 3.9-3.11.
正しく動作させるには、このファイルをGitHubリポジトリのルートディレクトリに置く必要があります:
pyospackage/
├──.github/
└── workflows/
└── run-tests.yml # The name of this file can be whatever you wish
name: Pytest unit/integration
on:
pull_request:
push:
branches:
- main
# Use bash by default in all jobs
defaults:
run:
shell: bash
jobs:
build-test:
name: Test Run (${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
python-version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install nox
- name: List installed packages
run: pip list
- name: Run tests with pytest & nox
run: |
nox -s tests-${{ matrix.python-version }}
# You only need to upload code coverage once to codecov unless you have a
# more complex build that you need coverage for.
- name: Upload coverage to Codecov
if: ${{ matrix.os == 'ubuntu-latest' && matrix.python-version == '3.10'}}
uses: codecov/codecov-action@v3