17
loading...
This website collects cookies to deliver better user experience
This is Day 20 of the #100DaysOfPython challenge.
hello-pytest-github-actions
.$ git clone https://github.com/okeeffed/hello-pytest.git hello-pytest-github-actions
$ cd hello-pytest-github-actions
# Install the deps
$ pipenv install
# Prepare the file for the GitHub action
$ mkdir -p .github/workflows
$ touch .github/workflows/pytest.yml
$ pipenv run pytest
============================== test session starts ==============================
platform darwin -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /path/to/code/blog-projects/hello-pytest
collected 3 items
tests/test_math.py ... [100%]
=============================== 3 passed in 0.01s ===============================
Pipfile
, we want to add a [scripts]
section to add a test
script:[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
pytest = "*"
[requires]
python_version = "3.9"
[scripts]
test = "pytest"
test
script will simply call pytest
. We will use this script within our GitHub action.-v
flag to our call to pytest
.pipenv run test -v
from our terminal:$ pipenv run test -v
========================== test session starts ===========================
platform darwin -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/dennisokeeffe/code/blog-projects/hello-pytest/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/dennisokeeffe/code/blog-projects/hello-pytest-github-actions
collected 3 items
tests/test_math.py::test_add PASSED [ 33%]
tests/test_math.py::test_subtract PASSED [ 66%]
tests/test_math.py::test_multiply PASSED [100%]
=========================== 3 passed in 0.02s ============================
.github/workflows/pytest.yml
file that we created earlier, add the following:# .github/workflows/app.yaml
name: PyTest
on: push
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Check out repository code
uses: actions/checkout@v2
# Setup Python (faster than using Python container)
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.x"
- name: Install pipenv
run: |
python -m pip install --upgrade pipenv wheel
- id: cache-pipenv
uses: actions/cache@v1
with:
path: ~/.local/share/virtualenvs
key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
- name: Install dependencies
if: steps.cache-pipenv.outputs.cache-hit != 'true'
run: |
pipenv install --deploy --dev
- name: Run test suite
run: |
pipenv run test -v
PyTest
.push
event to the repository.ubuntu-latest
.3.x
.pipenv
and wheel
.Be sure at this stage that you have set up your own origin remote for the repo.
pytest
testing framework to test our code.amseaman