Github actions: should I test my python project with pytest or with tox? - pytest

I have a Python project that is automatically tested with the following Github Actions file:
name: pytest
on: push
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python: ["3.8", "3.9"]
steps:
- uses: actions/checkout#v2
- name: Setup Python
uses: actions/setup-python#v2
with:
python-version: ${{ matrix.python }}
- name: Install pytest
run: python -m pip install pytest
- name: Install requirements
run: python -m pip install -r requirements.txt
- name: Run pytest
run: python -m pytest
It works fine, but I saw that other projects use an action for tox, which then runs pytest. I understand that tox automatically installs a virtual environment, but this is already done by GitHub Actions.
Is there a reason to replace my current Github Actions file with a file that runs tox?

You can run tox both local and on CI/GHA, so you can test your code against several Python versions without duplicating any test setups.
That is the main benefit.
PS: I am one of the tox maintainers.

Related

Github Actions: warning about set-output, but not using it

I am using a GitHub actions to "build" a python app (running linting, code coverage, and tests). At the end of the action I get the following warning:
1 warning
build
The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
but my python-app.yml does not use set-output:
name: Python application
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout#v3
- name: Set up Python 3.10
uses: actions/setup-python#v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with pylint
run: |
pylint src
continue-on-error: false
- name: Test with pytest
run: |
pytest
- name: pytest coverage
run:
pytest --cov=./ --cov-report=xml:tests/coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action#v3
and so I am not sure how to amend my .yml in order to make it compliant for the future.
In your workflow, there may be indirect dependencies on actions that have yet not been updated for GITHUB_OUTPUT against the set-output deprecation.
You need to check all the actions in your workflow one by one for version updates with the set-output fix.
In your case, visiting https://github.com/actions/setup-python reveals that there's a new version available. And, searching for the set-output string in the repo results in relevant references e.g. issues, commit, etc. For example, this issue (https://github.com/actions/setup-python/issues/578) verifies that it has been fixed in #v4.
So, as of now, using #v4 should fix it i.e.:
- uses: actions/setup-python#v4
The actions are being updated gradually. Hopefully, all of them will be updated soon and we won't be seeing that warning anymore.

How to Define Conda Build output on Github Actions and Cache Installs

I am trying to build a conda package on multiple OS and python versions using Github Actions.
I am struggling with what would seem to be a simple thing. I need to set the output so I can upload it as an artifact (not in the action yet). Since each OS the path is different (windows vs mac vs linux). I wanted to put it in a local folder. Which I do by using the --croot ./output_build.
When I do that, I still am struggling on building the artifact uploader:
Here is what I tried:
- uses: actions/upload-artifact#v2
with:
name: build-${{ matrix.os }}-${{matrix.python-version}}-${{ github.sha }}
path: build/output_build/*.tar.bz2
retention-days: 10
The action states that no files exist.
name: Python Package using Conda
on: [push]
jobs:
build-linux:
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.7']
max-parallel: 5
steps:
- uses: actions/checkout#v2
- name: Set up Python
uses: actions/setup-python#v2
with:
python-version: ${{ matrix.python-version }}
- name: Add conda to system path
run: |
# $CONDA is an environment variable pointing to the root of the miniconda directory
echo $CONDA/bin >> $GITHUB_PATH
- name: Install dependencies using Miniconda
run: |
conda env update --file environment.yml --name base
- name: Install Conda-Build
run: |
cd ./build
mkdir output_build
conda install conda-build
conda update conda
conda update conda-build
conda build --croot ./output_build -c conda-forge internalpackagerecipe
cd ./ouput_build
dir

Pytest not finding tests on GitHub actions

I'm trying to generate code coverage for a python package I work on and created a GitHub action to do this, which uses the codecov-action. The workflow looks like this:
name: Code Coverage
on: [push]
jobs:
run:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout#master
- name: Setup Python
uses: actions/setup-python#master
with:
python-version: 3.7
- name: Install dependencies
run: |
pip install pytest
pip install pytest-cov
pip install -r requirements.txt
- name: Generate coverage report
run: |
pytest --cov=./epispot --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action#v1
with:
verbose: true
fail_ci_if_error: true
When it's run, it outputs an error, which you can view here. Pytest outputs error code 5 - meaning that no tests were found - but when I run it on my computer, it works properly. Why is this happening?
It may be not the case with you but I had this issue when I was adding a Python test in a branch but added a Github action with pytest to master branch. So, pytest was not finding any tests because there were in fact none.
After I merged master branch with the workflow into that branch with a test, Python build with pytest passed.

GitHub Action pb with environment variable

I set up environment variable in my GitHub workflow to force PlatformIO use repo clone just before but does work.
can somebody see something wrong in my code.
this is my code:
name: Build
on: [push]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Set up Python
uses: actions/setup-python#v1
with:
python-version: '3.x'
- name: Install dependencies
run:
|
python -m pip install --upgrade pip
pip install platformio
- name: Check out Luos repository
uses: actions/checkout#v2
- name: Create env repo
run:
|
cd ..
git clone https://github.com/Luos-io/LuosHAL.git
git clone https://github.com/Luos-io/Examples.git
export PLATFORMIO_LIB_EXTRA_DIRS=/home/runner/work/Luos
- name: Run PlatformIO
run:
|
platformio run -d Examples/Projects/Button/
Thanks for your help.
I find the pb:
export PLATFORMIO_LIB_EXTRA_DIRS=/home/runner/work/Luos need to be in the same run as platformio run -d Examples/Projects/Button/

How can I fix An action could not be found at the URI 'https://api.github.com/repos/actions/checkout/tarball/v1

I wrote some code to test my project and I want to run the test on GitHub Actions
I have the following workflow definition.
name : Testing
on: push
jobs:
registrar:
runs-on: ubuntu-latest
steps:
- name: Run Django unit tests
uses: actions/checkout#v2
run:|
pip3 install --user -r requirements.txt
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py test
The workflow failed with the following error.
An action could not be found at the URI 'https://api.github.com/repos/actions/checkout/tarball/v1 run:| pip3 install --user -r requirements.txt python3 manage.py makemigrations python3 manage.py migrate python3 manage.py test'
How do I fix this?
Try this:
name : Testing
on: push
jobs:
registrar:
runs-on: ubuntu-latest
steps:
- name: checkout the repository
uses: actions/checkout#v2
- name: Run Django unit tests
run: |
pip3 install --user -r requirements.txt
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py test
It splits your step into two steps, one for the checkout and one for the tests.
The checkout action should be seperate from your custom commands.
It also changes the spacing so that the run has the same intent as the name.
The last thing I changed is that I added a space before the pipe.