Skip to main content

Bash unit tests

Run tests with the Bash testing framework and publish their results in the GitLab test summary. The default CI Tools tools image includes bashunit; a custom image must provide Bash and bashunit on PATH.

Run Bash tests​

Include common and bashunit in .gitlab-ci.yml:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/bashunit@stable

Add tests/example_test.sh:

function test_adds_numbers() {
assert_equals 5 "$((2 + 3))"
}

The test:bashunit job discovers *_test.sh files below the repository root. It runs their tests and uploads bashunit-junit.xml, including assertion failures. A failed test fails the job by default. A missing test directory or a pattern matching no files also fails the job.

Select test files​

Use path and pattern to select another directory or file name pattern:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/bashunit@stable
inputs:
path: tests/unit
pattern: "*Test.sh"
junit-report: reports/bashunit.xml
rules:
- exists:
- "tests/unit/*Test.sh"
when: on_success
- when: never

Change rules when changing the file name pattern: the default rule checks for **/*_test.sh. Relative report paths start at the project root. The report is retained for one week and uploaded even when assertions fail. Invalid discovery paths fail before a report is created.

Use name, image, stage, needs, allow-failure, and rules to configure the job, as with python-unittest. The separate bash-unit-tests component runs standalone *.ci-test.sh scripts; use bashunit for tests written with bashunit assertions.

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.bashunit-junit.xmlstring
nameThe name of the test job.bashunitstring
needsThe jobs that this job depends on.[]array
pathThe directory to search for Bash test files..string
patternThe file name pattern used to discover Bash test files.*_test.shstring
rulesThe rules for the test job.[{"exists":["**/*_test.sh"],"when":"on_success"},{"when":"never"}]array
stageThe stage for the test job.teststring