Basic tox config
Let’s start with a basic config that will run unittest and measure coverage.
[tox] envlist = py3.7,py3.8 [testenv] usedevelop = true deps = coverage commands = coverage erase coverage run --source=src setup.py test coverage report
Important option here is usedevelop = true. With this it won’t install your package to virtualnenv and coverage can be measured. Another thing is to erase previous coverage reports.
Codestyle
Next, I wanted to check the codestyle. I use pycodestyle, flake8 and pylint. Configs for those you can find here and here. So let’s add these dependencies and commands to run before tests.
[tox] envlist = py3.7,py3.8 [testenv] usedevelop = true deps = coverage pycodestyle flake8 pylint commands = pycodestyle src tests flake8 src tests pylint src tests coverage erase coverage run --source=src setup.py test coverage report
Now I have all measurements that I want to run automatically whenever I test the whole project.
Separate tests from codestyle
Let’s make it a little more under control. Running code style and tests for every version of python is ineffective. So let’s make checking code style separate from tests. Tests should run on every python version so they are under default testenv block.
[tox] envlist = py3.{7,8},codestyle,flake8,lint minversion = 3.7 [testenv] usedevelop = true deps = coverage commands = coverage erase coverage run --source=src setup.py test coverage report [testenv:codestyle] deps = pycodestyle commands = pycodestyle src tests [testenv:flake8] deps = flake8 commands = flake8 src tests [testenv:lint] deps = pylint commands = pylint src tests --rcfile=.pylintrc
After running tox with this config I will get such results.
py3.7: commands succeeded py3.8: commands succeeded codestyle: commands succeeded flake8: commands succeeded lint: commands succeeded congratulations :)
Integrate tox with PyCharm
PyCharm can run this configuration, but unfortunately, I won’t see tests view like with normal tests run. PyCharm only partially supports tox. For some reason, it’s not integrated with unittest run under tox. But we can use pytest to run my tests. But still, if we run it with a standard coverage tool it won’t help.
I found out that running tests with pytest command and pytest-cov plugin work as I wanted.
[tox] envlist = py3.{7,8},codestyle,flake8,lint minversion = 3.7 [testenv] usedevelop = true deps = pytest pytest-cov commands = pytest --cov=src --cov-report=term-missing [testenv:codestyle] deps = pycodestyle commands = pycodestyle src tests [testenv:flake8] deps = flake8 commands = flake8 src tests [testenv:lint] deps = pylint commands = pylint src tests --rcfile=.pylintrc
Bellow, you see how passed test results are presented in PyCharm. It’s exactly what I was aiming for.
Integrate tox with GitHub Actions
The last step is to use tox not only on my local machine but also on GitHub’s actions when I push my code.
As you can see below I used a tox environment to split separate steps. Now I run them inside of GitHub steps with -e option to choose only specific steps to run.
name: Gilded Rose Kata on: push jobs: build: runs-on: ubuntu-latest strategy: matrix: python-version: [3.7,3.8] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip pip install tox tox -e py${{ matrix.python-version }} --notest - name: Tests Python ${{ matrix.python-version }} run: | tox -e py${{ matrix.python-version }} - name: CodeStyle run: | tox -e codestyle -e flake8 -e lint
You can find the whole integration here.