Detect CI via environment variable
CI systems typically set the environment variable CI
as a de facto standard for easy CI detection.
Here are details of several popular CI services:
- GitHub Actions:
GITHUB_ACTIONS
andCI
are set totrue
- Travis-CI
- AppVeyor
- CircleCI
- GitLab
- Bitbucket Pipeline
In general, across programming languages, test frameworks allow behavior changes based on the environment variables set by the CI system.
Python Pytest
Pytest handles conditional tests well.
This allows skipping a subset of tests on CI by detecting the CI
environment variable:
import os
import pytest
CI = os.environ.get('CI') in ('True', 'true')
@pytest.mark.skipif(CI, reason="not a test for CI")
def test_myfun():
...
CMake CTest
CTest can also use CI environment variables to adjust test behavior.
if("$ENV{CI}")
set(NO_GFX on)
endif()
add_test(NAME menu COMMAND menu)
set_tests_properties(menu PROPERTIES DISABLED $<BOOL:${NO_GFX}>)
Enclose "$ENV{}"
in
quotes
in case the environment variable is empty or not defined, or the configuration step may syntax error.