Configure Continuous Integration

Now we add tests to the package you just built, automate running them, and finally have GitLab run them for you on every push. The Continuous Integration Setup chapter of the Kiwi template documentation covers the same ground in more depth.

Creating tests

Create a folder called tests next to plain_magic, containing an empty __init__.py (two underscores each side) and a file test_number_magic.py:

tests/test_number_magic.py
from plain_magic.number_magic import addition


def test_addition():
    # given
    first_summand = 1.0
    second_summand = 2.0

    # when
    result = addition(first_summand, second_summand)

    # then
    assert result == 3.0

This is the same given/when/then shape you used in Part 2 Tasks. Note the import on the first line refers to your package name — if you called your folder something other than plain_magic, adjust it to match.

Your project should now look like this:

some_directory/
   plain_magic/
      __init__.py
      cli.py
      number_magic.py
   tests/
      __init__.py
      test_number_magic.py
   pyproject.toml

Install pytest and run it from your project directory (some_directory — not your operating system’s home folder), with your virtual environment active:

pip install pytest
pytest

How do you know it worked? You see:

===================== test session starts ======================
collected 1 item

tests/test_number_magic.py .                              [100%]

====================== 1 passed in 0.01s =======================

If you get no tests ran, you’re in the wrong directory. If the build complains about Multiple top-level packages discovered, your pyproject.toml is missing the [tool.setuptools.packages.find] section from the previous chapter.

Configure Tox

tox automates the whole install-and-test procedure: it builds your package, installs it into a fresh environment, and runs the tests there. That “fresh” part is the value — it catches problems that your own machine hides, like a file you forgot to commit or a dependency you happen to already have installed.

Create tox.ini in some_directory:

tox.ini
[tox]
envlist = py3
isolated_build = True

[testenv]
deps = pytest
commands = pytest -v

Reading that: envlist is which Python versions to test against; deps is what to install for testing; commands is what to run.

Then install and run it:

pip install tox
tox

How do you know it worked? The last lines say:

py3: OK (3.24=setup[2.98]+cmd[0.26] seconds)
congratulations :) (3.28 seconds)

If a test fails here but passed under plain pytest, that’s tox doing its job — something works locally that wouldn’t work for anyone else.

Your project should now look like this:

some_directory/
   plain_magic/
      __init__.py
      cli.py
      number_magic.py
   tests/
      __init__.py
      test_number_magic.py
   pyproject.toml
   tox.ini

Configure GitLab to run the tests

Finally, let’s have GitLab run all of that automatically on every push. Add a file named .gitlab-ci.yml in some_directory (note the leading dot — see Prerequisites for creating dot-files safely):

.gitlab-ci.yml
image: python:3.12-slim

stages:
  - test

test:
  stage: test
  script:
    - pip install tox
    - tox

This says: start from a machine image that has Python 3.12, then run those two commands. That’s all CI is — the same commands you just ran by hand, executed for you on a clean server.

Commit and push all of it (pyproject.toml, tox.ini, .gitlab-ci.yml, plain_magic/, tests/) through a merge request as usual.

How do you know it worked? Open your project on git.tu-berlin.de and go to Build → Pipelines in the left sidebar. A new entry appears for your push; it shows a spinner, then a green tick when the tests pass. Click into it to read the log — this is the same output you saw locally.

If the Pipelines page stays empty, your project probably isn’t in the kiwi-git-workshops group, which is where the build machines are attached. Ask your instructor to move it there.

For more detail on any of this, see the template’s CI documentation.