For the complete documentation index, see llms.txt. This page is also available as Markdown.

Enable parallelism on a Run Tests step

You can enable parallelism on a Run Tests step.

By default, Test Intelligence reduces test time by running only the necessary tests to validate your code changes. You can enable parallelism and test splitting in your Run Tests steps to further optimize test times.

With parallelism, you specify how you want Harness to divide similar workloads. When you use parallelism and test splitting with Test Intelligence, Harness divides the work after test selection. This means that your test execution time is reduced by both test selection and parallelism.

Example: Time saved by combining TI with test splitting

Suppose you have a pipeline that runs 100 tests, and each test takes about one second to run. Here's how TI and parallelism can reduce your test times:

  • By default, without TI or parallelism, all 100 tests run in sequence, taking 100 seconds.

  • With TI, test selection reduces the number of tests based on the detected changes. Supposing only 20 out of the 100 tests are required, the build with TI runs 20 tests in sequence, taking 20 seconds. This reduces test run time by 80%.

  • With TI and parallelism, the selected tests are divided into a number of workloads. Supposing a maximum of four workloads and 20 selected tests, the 20 tests are split into four concurrently-running groups. It takes only five seconds to run the tests, reducing test run time by 95% compared to the default.

Note that while parallelism for TI can improve the total time it takes to run all tests, some tests may still take a long time to run if, by their nature, they are intensive, long-running tests.

Enable test splitting for TI

To enable test splitting for TI, you must:

Define a parallelism strategy

In the context of test splitting, the parallelism strategy defines the number of workloads into which tests can be divided. Each parallel instance (or workload) is a duplicate of the stage where you've defined a parallelism strategy, but each instance runs different tests.

Define the parallelism strategy on the stage with the Run Tests step.

  1. In your pipeline, select the stage where your tests run, and then select the Advanced tab.

  2. Under Looping Strategies, select Parallelism.

  3. Set the parallelism value to the number of workloads that you want to divide your tests into. For example, if you want to create four workloads, set parallelism: 4.

  4. Optional: Define maxConcurrency. This is a strategy to optimize parallelism.

Define the parallelism strategy (strategy.parallelism) on the stage with the RunTests step.

Optimize parallelism

In general, a higher parallelism value means a faster pipeline run time, because the tests can be divided into more parallel instances. However, this depends on your test suite and resource limitations in your build infrastructure. For example, if you try to run 10 groups of tests, but your build infrastructure can't handle 10 parallel instances, the pipeline can fail or take longer than expected.

To optimize your parallelism strategy:

  • Try different parallelism values to determine your infrastructure's limits. Parallelism impacts resource allocation for the pipeline. A pipeline with five sequential stages can require fewer resources than a pipeline running five parallel instances of a stage, because the second pipeline has to run all five instances at once.

  • Use maxConcurrency to control the flow of parallel instances and avoid overtaxing infrastructure resources. Concurrency limits the number of parallel instances that can run at once and queues additional instances.

    • For example, if you set parallelism: 12, Harness attempts to run 12 instances of the stage at once. If you set parallelism: 12 and maxConcurrency: 3, Harness generates 12 instances of the stage, but only runs three instances at a time. The remaining nine instances are queued, and the queued instances start running as space clears in the concurrency limit (when prior instances finish).

    • Concurrency allows you to divide tests into more workloads without overloading your system resources.

    • There are resource requirements to generate parallel instances (even if they are not all running at the same time) and handle queues. Try different combinations of parallelism and maxConcurrency values to determine your ideal configuration.

Enable Test Splitting on the Run Tests step

On the Run Tests step, set Enable Test Splitting (enableTestSplitting: true).

The Test Split Strategy is optional. The default test split strategy is class timing. If you would rather split tests by test count, you can set the Test Split Strategy to Test Count.

Class timing uses test times from previous runs to determine how to split the test workload for the current build. Test count uses simple division to split the tests into workloads. However, the maximum possible number of workloads is determined by the parallelism strategy you specified on the step or stage. For example, if you set parallelism: 5, then the tests are split into a maximum of five workloads.

Differentiate report names

With stage-level parallelism, results files are automatically output as separate stage artifacts.

If you want the individual results files to have unique names, you can use an expression or variable in the results file name, such as result_<+strategy.iteration>.xml or result_${HARNESS_NODE_INDEX}.xml.

You might need to modify your test tool configurations to support this report naming convention, and you must modify the Report Paths (reports.paths) value to include the Harness expression, such as <+strategy.iteration>. For example:

Differentiate parallel runs in logs

You can also use environment variables and expressions to differentiate parallel runs in build logs.

  1. Add two stage variables:

    • HARNESS_STAGE_INDEX: <+strategy.iteration> - This is the index of the current parallel run.

    • HARNESS_STAGE_TOTAL: <+strategy.iterations> - This is the total parallel runs.

  2. Add a preCommand to echo the variables' values so you can easily see the values in build logs.

YAML example: Test splitting for TI

This example shows a stage that uses Harness Cloud build infrastructure and runs Test Intelligence on a Java codebase. The parallelism strategy is set at the stage level.

Last updated

Was this helpful?