Use Run Tests step for Python
Set up TI for Python codebases.
This page contains instructions for using Test Intelligence (v1) with the Run Tests step.
While Run Tests step remains backwards compatible, Harness recommends using the newer Test step (Test Intelligence v2) for simplified user experience.
Enable TI for Python
You can enable TI for Python in three steps:
Add a Run Tests step. or use our new Test
Add the 'Run Tests' step
Add the Run Tests step to the Build stage in a CI pipeline.
In the Run Tests step, you must select Run only selected tests (runOnlySelectedTests: true). For information about each setting, go to Run Tests step settings.
- step:
type: RunTests
name: Run Python Tests
identifier: Run_Python_Tests
spec:
language: Python
buildTool: Pytest ## Specify pytest or unittest.
runOnlySelectedTests: true ## Must be 'true' to enable TI.
preCommand: |- ## Optional
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install -r requirements/test.txt
python3 -m pip install -e .For additional YAML examples, go to Pipeline YAML examples
Trigger test selection
After adding the Run Tests step, trigger test selection. You need to run your pipeline twice to trigger test selection.
Add test splitting
Once you start saving time with test selection, you can further optimize test times by enabling parallelism (test splitting) for TI.
You can also configure TI to ignore tests or files.
Pipeline YAML examples
This example shows a pipeline that uses Harness Cloud build infrastructure and runs tests on Python with pytest and Test Intelligence.
This example shows a pipeline that uses a Kubernetes cluster build infrastructure and runs tests on Python with pytest and Test Intelligence.
Run Tests step settings
The following information explains how to configure most settings for the Run Tests step. You might not need all settings for all scenarios; some settings are optional, and some settings are only available for specific languages, build tools, or build infrastructures.
Container Registry and Image
The build environment must have the necessary binaries for the Run Tests step to execute your test commands. Depending on the stage's build infrastructure, Run Tests steps can use binaries that exist in the build environment, or use Container Registry and Image to pull an image, such as a public or private Docker image, that contains the required binaries. You can also install tools at runtime in Pre-Command, provided the build machine or image can execute the necessary commands, such as curl commands to download files.
The stage's build infrastructure determines whether these fields are required or optional:
Kubernetes cluster build infrastructure: Container Registry and Image are always required.
Local runner build infrastructure: Run Tests steps can use binaries available on the host machine. The Container Registry and Image are required if the machine doesn't have the binaries you need.
Self-managed AWS/GCP/Azure VM build infrastructure: Run Tests steps can use binaries that you've made available on your build VMs. The Container Registry and Image are required if the VM doesn't have the necessary binaries. These fields are located under Additional Configuration for stages that use self-managed VM build infrastructure.
Harness Cloud build infrastructure: Run Tests steps can use binaries available on Harness Cloud machines, as described in the image specifications. The Container Registry and Image are required if the machine doesn't have the binaries you need. These fields are located under Additional Configuration for stages that use Harness Cloud build infrastructure.
What are the expected values for Container Registry and Image
For Container Registry settings, provide a Harness container registry connector, such as a Docker connector, that connects to a container registry, such as Docker Hub, where the Image is located.
For Image, provide the FQN (fully-qualified name) or artifact name and tag of the Docker image to use when this step runs commands, for example us-docker.pkg.dev/gar-prod-setup/harness-public/harness/cache:latest or maven:3.8-jdk-11. If you don't include a tag, Harness uses the latest tag. Depending on the connector and feature flags set, an FQN may be required.
You can use any Docker image from any Docker registry, including Docker images from private registries. Different container registries require different name formats, for example:
Docker Registry: Input the name of the artifact you want to deploy, such as
library/tomcat. Wildcards aren't supported. FQN is required for images in private container registries.ECR: Input the FQN of the artifact you want to deploy. Images in repos must reference a path, for example:
40000005317.dkr.ecr.us-east-1.amazonaws.com/todolist:0.2.GAR: Input the FQN of the artifact you want to deploy. Images in repos must reference a path starting with the project ID that the artifact is in, for example:
us-docker.pkg.dev/gar-prod-setup/harness-public/harness/cache:latest.
Configuring a Container Registry and Image settings.
Pulling images from JFrog Artifactory Docker registries
If you need to pull images from a JFrog Artifactory Docker registry, create a Docker connector that connects to your JFrog instance. Don't use the Harness Artifactory connector - The Artifactory connector only supports JFrog non-Docker registries.
To create a Docker connector for a JFrog Docker registry:
Go to Connectors in your Harness project, organization, or account resources, and select New Connector.
Select Docker Registry under Artifact Repositories.
Enter a Name for the connector. The Description and Tags are optional.
For Provider Type, Select Other.
In Docker Registry URL, enter your JFrog URL, such as
https://mycompany.jfrog.io.In the Authentication settings, you must use Username and Password authentication.
Username: Enter your JFrog username.
Password: Select or create a Harness text secret containing the password corresponding with the Username.
Complete any other settings and save the connector. For information all Docker Registry connector settings, go to the Docker connector settings reference.
One completed, please remember to use the FQN location of the image unless you have set the appropriate feature flags as listed above
Language
Select Python.
Build Tool
Select the build automation tool: Pytest or Unittest.
Build Arguments
This setting is optional for Python. You can provide additional runtime arguments for tests, such as --junitxml=out_report.xml.
If you include Build Arguments for Python:
You don't need to repeat the build tool, such as
pytestorunittest; this is declared in Build Tool.You don't need to include coverage flags (
--covorcoverage).Python 3 is required. If you use another command, such as
python, to invoke Python 3, you must add an alias, such aspython3 = "python".The Python 3 binary is required. Python 3 is preinstalled on Harness Cloud runners. For other build infrastructures, the binary must be preinstalled on the build machine, available in the specified Container Registry and Image, or manually installed at runtime in Pre-Command.
Test Report Paths
This setting is optional for Python. You can use this setting if you want your test reports to be stored somewhere other than the default location or have a different name than the default report name.
You can specify one or more paths to files that store test results in JUnit XML format. Glob is supported.
You can add multiple paths. If you specify multiple paths, make sure the files contain unique tests to avoid duplicates.
Test Splitting (parallelism)
Used to enable test splitting (parallelism) for TI.
Stage-level parallelism is recommended for Python.
Pre-Command, Post-Command, and Shell
Pre-Command: You can enter commands for setting up the environment before running the tests, such as:
Post-Command: You can enter commands used for cleaning up the environment after running the tests.
Shell: If you supplied a script in Pre-command or Post-command, select the corresponding shell script type.
Run Only Selected Tests
This option must be selected (true) to enable Test Intelligence.
If this option is not selected (false), TI is disabled and all tests run on every build.
Test Globs
You can override the default test globs pattern. For example, if the default is *_test.py or test_*.py, you can override it with any other pattern, such as .test.py. Because test selection is at the file-level, the test globs pattern references file names. You can include directory structures, such as microservice1/**/test_*.py.
Environment Variables
You can inject environment variables into the step container and use them in the step's commands. You must input a Name and Value for each variable.
You can reference environment variables in the Build Arguments, Pre-Command, or Post-Command scripts by name, such as $var_name.
Variable values can be fixed values, runtime inputs, or expressions. For example, if the value type is expression, you can input a value that references the value of some other setting in the stage or pipeline.

PYTHONPATH
You can set PYTHONPATH in the step's envVariables, if required. For example:
Additional container settings
Settings specific to containers are not applicable in a stages that use VM or Harness Cloud build infrastructure.
Image Pull Policy
If you specified a Container Registry and Image, you can specify an image pull policy:
Always: The kubelet queries the container image registry to resolve the name to an image digest every time the kubelet launches a container. If the kubelet encounters an exact digest cached locally, it uses its cached image; otherwise, the kubelet downloads (pulls) the image with the resolved digest, and uses that image to launch the container.
If Not Present: The image is pulled only if it isn't already present locally.
Never: The image is not pulled.
Run as User
If you specified a Container Registry and Image, you can specify the user ID to use for running processes in containerized steps.
For a Kubernetes cluster build infrastructure, the step uses this user ID to run all processes in the pod. For more information, go to Set the security context for a pod.
Privileged
For container-based build infrastructures, you can enable this option to run the container with escalated privileges. This is equivalent to running a container with the Docker --privileged flag.
Set Container Resources
These settings specify the maximum resources used by the container at runtime. These setting are only available for container-based build infrastructures, such as a Kubernetes cluster build infrastructure.
Limit Memory: The maximum memory that the container can use. You can express memory as a plain integer or as a fixed-point number using the suffixes
GorM. You can also use the power-of-two equivalentsGiandMi. The default is500Mi.Limit CPU: The maximum number of cores that the container can use. CPU limits are measured in CPU units. Fractional requests are allowed. For example, you can specify one hundred millicpu as
0.1or100m. The default is400m. For more information go to Resource units in Kubernetes.
Timeout
The timeout limit for the step. Once the timeout is reached, the step fails and pipeline execution proceeds according to any Step Failure Strategy settings or Step Skip Condition settings.
Troubleshoot Test Intelligence
Go to the CI Knowledge Base for questions and issues related to Test Intelligence, including:
Last updated
Was this helpful?

