Tested pipenv with GitHub Actions

This recently got a pull request and thought that I had to write a test.

table of contents

--I wrote a test code using unittest! --I tried to automate the test with GitHub Actions!

I wrote a test code using unittest!

Reference: https://qiita.com/aomidro/items/3e3449fde924893f18ca

Code I wrote: [https://github.com/sun-yryr/Rec-adio/blob/feature/test/test/test_func.py](https://github.com/sun-yryr/Rec-adio/ blob / feature / test / test / test_func.py)

Python has a standard module, unittest. I wrote a test code using this.

Please note that there are many parts covered by operation.

Write each test code as a subclass of ʻunittest.TestCase`

import unittest
import Module to test as f
class TestCalculate(unittest.TestCase):
    def test_add(self):
    	#Addition test
    	self.assertEqual(f.add(3, 5), 8)
    		
    def test_sub(self):
    	#Subtraction test
    	self.assertEqual(f.sub(3, 5), -2)

The available assert methods are [here](https://docs.python.org/ja/3/library/unittest.html#assert-methods You can find it at library / unittest.html # assert-methods)).

The method for testing must start with test.

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

The test runs by calling main.

I think that most of the time you write test code by dividing it into multiple files.

Running python -m unittest discover -v will run all the tests in the current directory.

Registered in pipenv scripts

Since I am using pipenv, I will register it in scripts.

The above command needs to be executed in the folder where the test code is located, so add some ideas.

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

You need to type cd to move to the test code folder first, so run it with bash -c.

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

OK

If you get OK, you're done.

I tried to automate the test with GitHub Actions!

If you make a test, you will automate it, right?

Select Actions → New workflow → Python application.

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

The template is easy to use and amazing ...

You will have a yaml file with the following template applied.

    # 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

I will set it appropriately.

    name: Python application

It's a name, let's name it whatever you like.

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

Decide which operation in which branch will run the Action. It's usually okay as it is

    runs-on: ubuntu-latest

I think it's the image to run on. (I'm not sure, so skip it)


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

I only know if I specify the version of 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

One set with name and run. It seems that the command specified by run runs.

This is what you set

    # 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

Summary

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

It worked, so it's OK! Tuning is another opportunity!

Recommended Posts

Tested pipenv with GitHub Actions
Tested with Python
[Golang] Create docker image with Github Actions
Automatic test of Pipenv + Pytest on Github Actions
Tested with boto3 + mock
Build a data analysis environment with Kedro + MLflow + Github Actions
GitHub Actions Python cache sample
Creating an environment that automatically builds with Github Actions (Android)
Deploy your Go app on Google App Engine with GitHub Actions
Prepare pipenv environment with amazon Linux 2
pytorch @ python3.8 environment construction with pipenv
Create github pages with lektor Part 1