Create your FREE Codefresh account and start making pipelines fast. Create Account

Run your tests in parallel with Codefresh and Knapsack Pro

3 min read

One of the most well-known problems when it comes to testing applications is the amount of time required by all test suites. Integration tests, in particular, are usually very slow to execute and depending on the type of application, several minutes (or even hours in extreme cases) are needed in order to get the final execution result.

You can reduce the test execution time with several techniques, but one of the most effective methods is running your tests in parallel. Knapsack Pro is a dedicated solution of test parallelization that can cut down the amount of test execution time by spreading test suites into multiple build nodes. Some of the most important features of Knapsack Pro are:

  • Test parallelization for Ruby and Javascript applications (more languages coming)
  • Support for popular test runners (rspec, cucumber, cypress, jest etc)
  • Dynamic allocation of tests to build nodes (a.k.a. Queue mode)
  • Support for short-lived build nodes (such as preemptible VMs on cloud providers)
  • Fallback mode (run tests even when Knapsack API is not available)

It is very easy to use Knapsack Pro with Codefresh and split your tests in as many build nodes as you want:

Parallel tests

In the example above we have used 5 parallel build executions to parallelize the test phase of the project.

Split your test executions in Codefresh pipelines

Codefresh already supports parallel pipeline steps out of the box. But we have recently added two new enhancements to the Codefresh YAML syntax that can make parallel pipelines even easier. These are the scale and matrix keywords.

The “scale” syntax can be easily used when you have multiple parallel steps that are mostly similar and only differ in one or more properties. The “matrix” syntax is the familiar way of creating matrix pipelines where you define all environment parameters and Codefresh will automatically create all possible combinations. For example, the following pipeline runs with 3 different versions of GO and 2 versions of the CGO switch.

The resulting pipeline will have 6 parallel steps for all the possible combinations.

Knapsack Pro can take advantage of this functionality by leveraging its API which only needs two parameters:

  • KNAPSACK_PRO_CI_NODE_TOTAL
  • KNAPSACK_PRO_CI_NODE_INDEX

The first parameter is needed only once in a pipeline and defines how many build nodes will be used to split tests. The second parameter should be declared for each node and contains a number that defines which node is that (0, 1, 2, 3 and so on).

By using these two parameters Knapsack Pro will automatically split your tests between nodes. There are two modes for the split. The Regular/Standard mode splits tests in a static manner by measuring how much time each test file takes by looking at the previous builds. The static set of tests is allocated only once to each parallel node before starting tests.

The dynamic/queue mode gives to each node only a subset of the tests and then monitors their execution within the same build before asking for another set of tests from the queue. A fast node (that finishes tests quickly) will then fetch more tests while a slow node will get fewer tests. This mode is great if the build nodes are not equal in resources or you have test files that sometimes take more or less time (often end to end tests can vary in time execution).

Codefresh supports both Knapsack Pro modes. Here is a full example that brings all this together:

This pipeline splits Cypress tests into two nodes:

Knapsack parallel tests

Notice the values for KNAPSACK_PRO_CI_NODE_TOTAL and KNAPSACK_PRO_CI_NODE_INDEX. In order to run this pipeline in Codefresh, you also need a Knapsack Pro token that you can get after creating an account.

For more details see the integration page of Knapsack. Knapsack Pro tests also work great with service containers for running tests against databases or other external services. Here you can find articles dedicated to Knapsack Pro configuration for Ruby on Rails project on Codefresh and for Cypress end to end test runner.

Ready to try Codefresh, the CI/CD platform for Docker/Kubernetes/Helm? Create Your Free Account Today!

Kostis Kapelonis

About Kostis Kapelonis

Kostis is a software engineer/technical-writer dual class character. He lives and breathes automation, good testing practices and stress-free deployments.

Leave a Reply

* All fields are required. Your email address will not be published.