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
| Name | Description | Default | Type |
| --- | --- | --- | --- |
| allow-failure | Should the pipeline continue when one or more tests fail? | false | boolean |
| image | The image used to run the tests. | ${CI_TOOLS_REPO}/tools:${CI_TOOLS_COMPONENT_VERSION} | string |
| junit-report | Path to the JUnit report shown in the GitLab test summary. | python-unit-tests-junit.xml | string |
| name | The name of the test job. | python-unit-tests | string |
| needs | The jobs that this job depends on. | [] | array |
| path | The directory where unittest discovery starts. | . | string |
| pattern | The file name pattern used by unittest discovery. | test*.py | string |
| rules | The rules for the test job. | [{"exists":["**/test*.py"],"when":"on_success"},{"when":"never"}] | array |
| stage | The stage for the test job. | test | string |