Skip to main content

Python unit tests

Discover and run Python unittest tests, then publish the results in the GitLab test summary.

The default CI Tools tools image includes unittest-xml-reporting. The component starts discovery at the repository root, matches test*.py, enables Python color output, and saves one JUnit report.

Run Python unit tests​

Include common and python-unit-tests in .gitlab-ci.yml:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/python-unit-tests@stable

Add a discoverable test such as tests/test_sample.py:

import unittest


class SampleTest(unittest.TestCase):
def test_adds_numbers(self) -> None:
self.assertEqual(2 + 3, 5)

The test:python-unit-tests job discovers the test from . and uploads python-unit-tests-junit.xml. GitLab displays the result in the merge request test summary.

Select a test directory​

Set path when tests live below one directory:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/python-unit-tests@stable
inputs:
path: tests

Set pattern to change the discovery file pattern. The default is test*.py.

Inputs​

NameDescriptionDefaultType
------------
allow-failureShould the pipeline continue when one or more tests fail?falseboolean
imageThe image used to run the tests.${CI_TOOLS_REPO}/tools:${CI_TOOLS_COMPONENT_VERSION}string
junit-reportPath to the JUnit report shown in the GitLab test summary.python-unit-tests-junit.xmlstring
nameThe name of the test job.python-unit-testsstring
needsThe jobs that this job depends on.[]array
pathThe directory where unittest discovery starts..string
patternThe file name pattern used by unittest discovery.test*.pystring
rulesThe rules for the test job.[{"exists":["**/test*.py"],"when":"on_success"},{"when":"never"}]array
stageThe stage for the test job.teststring