LoginSignup
2
4

More than 3 years have passed since last update.

GitHubActionsでpipenvのテストをした

Last updated at Posted at 2020-04-11

これ に最近プルリクが来て、テスト書かないとな〜って思ったのがきっかけです

目次

  • unittestを使ってテストコードを書いてみた!
  • GitHub Actionsでテストの自動化をしてみた!

unittestを使ってテストコードを書いてみた!

参考:https://qiita.com/aomidro/items/3e3449fde924893f18ca

書いたコード:https://github.com/sun-yryr/Rec-adio/blob/feature/test/test/test_func.py

Pythonには標準モジュールで unittest があります。これを使ってテストコードを書いてみました。

結構運用でカバーしてる部分が多いのでご注意ください

unittest.TestCase のサブクラスとして各テストコードを書いていきます

import unittest
import テストするモジュール as f
class TestCalculate(unittest.TestCase):
    def test_add(self):
        # 足し算のテスト
        self.assertEqual(f.add(3, 5), 8)

    def test_sub(self):
        # 引き算のテスト
        self.assertEqual(f.sub(3, 5), -2)

使用可能なassertメソッドは こちら にあります。

テスト用のメソッドは test で始まる必要があります。

if __name__ == "__main__":
    unittest.main()

mainを呼ぶことでテストが走ります。

複数のファイルに分割してテストコードを書くことがほとんどだと思います。

python -m unittest discover -v を実行することで、カレントディレクトリのテストが全部走ります。

pipenvのscriptsに登録

せっかくpipenvを使っているので、scriptsに登録します。

上記のコマンドはテストコードがあるフォルダで行う必要があるので一工夫加えます。

[scripts]
test = "bash -c \"cd test ; python -m unittest discover\""

テストコードのフォルダに移動する cd を先に打つ必要があるので、 bash -c で実行してあげます。

$ pipenv run 
....
----------------------------------------------------------------------
Run 4 tests in 0.386s

OK

OKが出れば完成です。

GitHub Actionsでテストの自動化をしてみた!

テストを作ったら自動化しますよね?

Actions → New workflow → Python application を選びます。

スクリーンショット 2020-04-11 21.38.40.png

テンプレートが使いやすくてすごいいい......

下記のようなテンプレートが適用されたyamlファイルができます。

    # This workflow will install Python dependencies, run tests and lint with a single version of Python
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

    name: Python application

    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]

    jobs:
      build:

        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v2
        - name: Set up Python 3.8
          uses: actions/setup-python@v1
          with:
            python-version: 3.8
        - name: Install dependencies
          run: |
            python -m pip install --upgrade pip
            pip install -r requirements.txt
        - name: Lint with flake8
          run: |
            pip install flake8
            # stop the build if there are Python syntax errors or undefined names
            flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
            # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
            flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
        - name: Test with pytest
          run: |
            pip install pytest
            pytest

適当に設定していきます。

    name: Python application

名前です、好きな名前にしましょう。

    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]

どのブランチのどの操作でActionを走らせるかを決めます。大抵そのままで平気です

    runs-on: ubuntu-latest

どのイメージで実行するか、だと思います。(よくわからないのでスキップ)


    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v1
      with:
        python-version: 3.8

pythonのバージョン指定をしてるくらいしかわからない

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt 
    - name: Lint with flake8
      run: |
        pip install flake8
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        pip install pytest
        pytest

name, run で1セット。runで指定したコマンドが走るみたいですね。

設定した物がこう

    # This workflow will install Python dependencies, run tests and lint with a single version of Python
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

    name: Python application

    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master, release ]

    jobs:
      build:

        runs-on: ubuntu-latest

        steps:
        - uses: actions/checkout@v2
        - name: Set up Python 3.6
          uses: actions/setup-python@v1
          with:
            python-version: 3.6
        - name: Install Pipenv
          run: |
            python -m pip install --upgrade pip
            pip install pipenv
        - name: Install dependencies
          run: |
            pipenv sync
        - name: Test with unittest
          run: |
            pipenv run test

まとめ

スクリーンショット 2020-04-11 21.49.14.png

動いたのでOK!チューニングは別の機会!

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4