Skip to content

Commit

Permalink
Merge pull request #217 from buildkite/v1.1.x
Browse files Browse the repository at this point in the history
Update README and CHANGELOG for v1.1.0
  • Loading branch information
nprizal authored Nov 11, 2024
2 parents 586e304 + 99f9723 commit 6d75409
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog

## 1.1.0 - 2024-11-11
- Experimental support for Cypress. See [Cypress usage guide](./docs/cypress.md).
- Experimental support for Playwright. See [Playwright usage guide](./docs/playwright.md).
- Update `BUILDKITE_TEST_ENGINE_TEST_CMD` and `BUILDKITE_TEST_ENGINE_RETRY_CMD` for Jest. See [Jest usage guide](./docs/jest.md).
- Fix issue when retrying Jest tests with special characters
- Remove `**/node_modules` from default value of `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN`. Files inside `node_modules` will be ignore regardless the value of this environment variable.

## 1.0.0 - 2024-09-23
- ⚠️ **BREAKING** Rename all environment variables from `BUILDKITE_SPLITTER_*` to `BUILDKITE_TEST_ENGINE_*`. See [Migrating to 1.0.0](https://github.com/buildkite/test-splitter/tree/90b699918b11500336f8a0fce306da917fba7408?tab=readme-ov-file#migrating-to-100)
- ⚠️ **BREAKING** Add the `BUILDKITE_TEST_ENGINE_TEST_RUNNER` as required environment variable.
Expand Down
87 changes: 34 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
# Buildkite Test Engine Client

Buildkite Test Engine Client (bktec) is an open source tool to orchestrate your test suites. It uses your Buildkite Test Engine suite data to intelligently partition and parallelise your tests.
Buildkite Test Engine Client (bktec) is an open source tool to orchestrate your test suites. It uses your Buildkite Test Engine suite data to intelligently partition and parallelize your tests.

bktec supports RSpec and Jest.

## Migrating to 1.0.0

The following environment variables are now required
- `BUILDKITE_TEST_ENGINE_TEST_RUNNER`

The test runner to use for running tests. You will need to ensure that `BUILDKITE_TEST_ENGINE_TEST_RUNNER` presents in the environment. Currently `rspec` and `jest` are supported.

- `BUILDKITE_TEST_ENGINE_RESULT_PATH`

The location of where the runner should store test results. We introduced a new feature that requires bktec to read test results from the runner for retries and verification purposes. To enable this feature, it is necessary to configure the `BUILDKITE_TEST_ENGINE_RESULT_PATH` environment variable.

In addition, we have updated the default test command for RSpec to `bundle exec rspec --format progress --format json --out {{resultPath}} {{testExamples}}`. Test Splitter will automatically replace `{{resultPath}}` with the value specified in `BUILDKITE_TEST_ENGINE_RESULT_PATH`. If you want to customize the RSpec command, make sure to include `--format json --out {{resultPath}}` in the command.

Furthermore, version 1.0.0 introduces name changes to environment variables. To migrate to v1.0.0, You will need to update the following environment variables in your Pipeline:

| Old variable | New variable |
| ------------ | ------------ |
| `BUILDKITE_SPLITTER_API_ACCESS_TOKEN`| `BUILDKITE_TEST_ENGINE_API_ACCESS_TOKEN` |
| `BUILDKITE_SPLITTER_SUITE_SLUG`| `BUILDKITE_TEST_ENGINE_SUITE_SLUG` |
| `BUILDKITE_SPLITTER_RESULT_PATH`| `BUILDKITE_TEST_ENGINE_RESULT_PATH` |
| `BUILDKITE_SPLITTER_DEBUG_ENABLED` | `BUILDKITE_TEST_ENGINE_DEBUG_ENABLED` |
| `BUILDKITE_SPLITTER_RETRY_CMD` | `BUILDKITE_TEST_ENGINE_RETRY_CMD` |
| `BUILDKITE_SPLITTER_RETRY_COUNT` | `BUILDKITE_TEST_ENGINE_RETRY_COUNT`|
| `BUILDKITE_SPLITTER_SPLIT_BY_EXAMPLE` | `BUILDKITE_TEST_ENGINE_SPLIT_BY_EXAMPLE` |
| `BUILDKITE_SPLITTER_TEST_CMD` | `BUILDKITE_TEST_ENGINE_TEST_CMD` |
| `BUILDKITE_SPLITTER_TEST_FILE_EXCLUDE_PATTERN` | `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` |
| `BUILDKITE_SPLITTER_TEST_FILE_PATTERN` | `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` |
| `BUILDKITE_SPLITTER_TEST_RUNNER` | `BUILDKITE_TEST_ENGINE_TEST_RUNNER` |
bktec supports multiple test runners and offers various features to enhance your testing workflow. Below is a comparison of the features supported by each test runner:

| Feature | Rspec | Jest | Playwright | Cypress |
| -------------------------------------------------- | :---: | :--: | :--------: | :-----: |
| Filter test files |||||
| Automatically retry failed test |||||
| Split slow files by individual test example |||||

## Installation
The latest version of bktec can be downloaded from https://github.com/buildkite/test-engine-client/releases
Expand All @@ -48,9 +24,8 @@ The available Go binaries

## Using bktec

### ENV variables
bktec uses the following Buildkite provided environment variables.
Please make sure that the following environment variables are available in your testing environment, particularly if you use Docker or some other type of containerization to run your tests.
### Buildkite Pipeline environment variables
bktec uses the following Buildkite Pipeline provided environment variables.
| Environment Variable | Description|
| -------------------- | ----------- |
| `BUILDKITE_BUILD_ID` | The UUID of the Buildkite build. bktec uses this UUID along with `BUILDKITE_STEP_ID` to uniquely identify the test plan. |
Expand All @@ -60,28 +35,29 @@ Please make sure that the following environment variables are available in your
| `BUILDKITE_PARALLEL_JOB_COUNT` | The total number of parallel jobs created from a Buildkite parallel build step. <br>Make sure you configure `parallelism` in your pipeline definition. You can read more about Buildkite parallel build step on this [page](https://buildkite.com/docs/pipelines/controlling-concurrency#concurrency-and-parallelism). |
| `BUILDKITE_STEP_ID` | The UUID of the step group in Buildkite build. bktec uses this UUID along with `BUILDKITE_BUILD_ID` to uniquely identify the test plan.

<br>
In addition to the above variables, you must set the following environment variables.
> [!IMPORTANT]
> Please make sure that the above environment variables are available in your testing environment, particularly if you use Docker or some other type of containerization to run your tests.
| Environment Variable | Description |
| -------------------- | ----------- |
| `BUILDKITE_TEST_ENGINE_API_ACCESS_TOKEN ` | Buildkite API access token with `read_suites`, `read_test_plan`, and `write_test_plan` scopes. You can create an access token from [Personal Settings](https://buildkite.com/user/api-access-tokens) in Buildkite |
| `BUILDKITE_TEST_ENGINE_SUITE_SLUG` | The slug of your Buildkite Test Engine test suite. You can find the suite slug in the url for your suite. For example, the slug for the url: https://buildkite.com/organizations/my-organization/analytics/suites/my-suite is `my-suite` |
| `BUILDKITE_TEST_ENGINE_RESULT_PATH` | bktec uses this environment variable to tell the runner where to store the test result. Test Splitter reads the test result after each test run for retries and verification. For RSpec, the result is generated using the `--format json` and `--out` CLI options, while for Jest, it is generated using the `--json` and `--outputFile` options. We have included these options in the default test command for RSpec and Jest. If you need to customize your test command, make sure to append the CLI options to save the result to a file. Please refer to the `BUILDKITE_SPLITTER_TEST_CMD` environment variable for more details. <br> *Note: Test Splitter will not delete the file after running the test, however it will be deleted by Buildkite Agent as part of build lifecycle. *|
| `BUILDKITE_TEST_ENGINE_TEST_RUNNER` | The test runner to use for running tests. Currently `rspec` and `jest` are supported.
### Create API access token
To use bktec, you need a Buildkite API access token with `read_suites`, `read_test_plan`, and `write_test_plan` scopes. You can generate this token from your [Personal Settings](https://buildkite.com/user/api-access-tokens) in Buildkite. After creating the token, set the `BUILDKITE_TEST_ENGINE_API_ACCESS_TOKEN` environment variable with the token value.

<br>
The following environment variables can be used optionally to configure bktec.
```sh
export BUILDKITE_TEST_ENGINE_API_ACCESS_TOKEN=token
```

| Environment Variable | Default Value | Description |
| ---- | ---- | ----------- |
| `BUILDKITE_TEST_ENGINE_DEBUG_ENABLED` | `false` | Flag to enable more verbose logging. |
| `BUILDKITE_TEST_ENGINE_RETRY_CMD` | For RSpec:<br> The retry command by default is the same as the value defined in `BUILDKITE_TEST_ENGINE_TEST_CMD`<br> For Jest:<br> `yarn test --testNamePattern '{{testNamePattern}}' --json --testLocationInResults --outputFile {{resultPath}}`| The command to retry the failed tests. <br> For Rspec bktec will fill in the `{{testExamples}}` placeholder with the failed tests. If not set, bktec will use the same command defined in `BUILDKITE_TEST_ENGINE_TEST_CMD`.<br> For Jest, bktec will fill in `{{testNamePattern}}` with a regex of the failed tests. |
| `BUILDKITE_TEST_ENGINE_RETRY_COUNT` | `0` | The number of retries. bktec runs the test command defined in `BUILDKITE_TEST_ENGINE_TEST_CMD` and retries only the failed tests up to `BUILDKITE_TEST_ENGINE_RETRY_COUNT` times, using the retry command defined in `BUILDKITE_TEST_ENGINE_RETRY_CMD`. |
| `BUILDKITE_TEST_ENGINE_SPLIT_BY_EXAMPLE` | `false` | Flag to enable split by example. When this option is `true`, bktec will split the execution of slow test files over multiple partitions. Split by example is currently only available for Rspec. |
| `BUILDKITE_TEST_ENGINE_TEST_CMD` | For RSpec:<br/> `bundle exec rspec --format progress --format json --out {{resultPath}} {{testExamples}}`<br/> For Jest:<br/> `yarn test {{testExamples}} --json --testLocationInResults --outputFile {{resultPath}}` | Test command to run your tests. bktec will replace the `{{testExamples}}` placeholder with the test plan, and replace `{{resultPath}}` with the value set in `BUILDKITE_TEST_ENGINE_RESULT_PATH`. It is necessary to configure your Rspec with `--format json --out {{resultPath}}` when customizing the test command, because bktec needs to read the result after each test run. |
| `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` | For RSpec:<br> -<br> For Jest:<br> `node_modules` | Glob pattern to exclude certain test files or directories. The exclusion will be applied after discovering the test files using a pattern configured with `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN`. </br> *This option accepts the pattern syntax supported by the [zzglob](https://github.com/DrJosh9000/zzglob?tab=readme-ov-file#pattern-syntax) library.* |
| `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` | For Rspec:</br> `spec/**/*_spec.rb`</br> For Jest:</br> `**/{__tests__/**/*,*.spec,*.test}.{ts,js,tsx,jsx}` | Glob pattern to discover test files. You can exclude certain test files or directories from the discovered test files using a pattern that can be configured with `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN`.</br> *This option accepts the pattern syntax supported by the [zzglob](https://github.com/DrJosh9000/zzglob?tab=readme-ov-file#pattern-syntax) library.* |
### Configure Test Engine suite slug
To use bktec, you need to configure the `BUILDKITE_TEST_ENGINE_SUITE_SLUG` environment variable with your Test Engine suite slug. You can find the suite slug in the URL of your suite. For example, in the URL `https://buildkite.com/organizations/my-organization/analytics/suites/my-suite`, the slug is `my-suite`.

```sh
export BUILDKITE_TEST_ENGINE_SUITE_SLUG=my-slug
```

### Configure the test runner
To configure the test runner for bktec, please refer to the detailed guides provided for each supported test runner. You can find the guides at the following links:
- [RSpec](./docs/rspec.md)
- [Jest](./docs/jest.md)
- [Playwright](./docs/playwright.md)
- [Cypress](./docs/cypress.md)


### Running bktec
Expand All @@ -95,8 +71,13 @@ steps:
env:
BUILDKITE_TEST_ENGINE_SUITE_SLUG: my-suite
BUILDKITE_TEST_ENGINE_API_ACCESS_TOKEN: your-secret-token
BUILDKITE_TEST_ENGINE_TEST_RUNNER: rspec
BUILDKITE_TEST_ENGINE_RESULT_PATH: tmp/result.json
```

### Debugging
To enable debug mode, set the `BUILDKITE_TEST_ENGINE_DEBUG_ENABLED` environment variable to `true`. This will print detailed output to assist in debugging bktec.

### Possible exit statuses

bktec may exit with a variety of exit statuses, outlined below:
Expand Down
42 changes: 42 additions & 0 deletions docs/cypress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Using bktec with Cypress
To integrate bktec with Cypress, set the `BUILDKITE_TEST_ENGINE_TEST_RUNNER` environment variable to `cypress`.

```sh
export BUILDKITE_TEST_ENGINE_TEST_RUNNER=cypress
```

## Configure test command
By default, bktec runs Cypress with the following command:

```sh
npx cypress run --spec {{testExamples}}
```

In this command, `{{testExamples}}` is replaced by bktec with the list of test files to run. You can customize this command using the `BUILDKITE_TEST_ENGINE_TEST_CMD` environment variable.

To customize the test command, set the following environment variable:
```sh
export BUILDKITE_TEST_ENGINE_TEST_CMD="yarn cypress:run --spec {{testExamples}}"
```

## Filter test files
By default, bktec runs test files that match the `**/*.cy.{js,jsx,ts,tsx}` pattern. You can customize this pattern using the `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` environment variable. For instance, to configure bktec to only run Cypress test files inside a `cypress/e2e` directory, use:
```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN=cypress/e2e/**/*.cy.js
```

Additionally, you can exclude specific files or directories that match a certain pattern using the `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` environment variable. For example, to exclude test files inside the `cypress/component` directory, use:

```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN=cypress/component
```

You can also use both `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` and `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` simultaneously. For example, to run all Cypress test files with `cy.js`, except those in the `cypress/e2e` directory, use:

```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN=**/*.cy.js
export BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN=cypress/e2e
```

> [!TIP]
> This option accepts the pattern syntax supported by the [zzglob](https://github.com/DrJosh9000/zzglob?tab=readme-ov-file#pattern-syntax) library.
65 changes: 65 additions & 0 deletions docs/jest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Using bktec with Jest
To integrate bktec with Jest, set the `BUILDKITE_TEST_ENGINE_TEST_RUNNER` environment variable to `jest`. Then, specify the `BUILDKITE_TEST_ENGINE_RESULT_PATH` to define where the JSON result should be stored. bktec will instruct Jest to output the JSON result to this path, which is necessary for bktec to read the test results for retries and verification purposes.

```sh
export BUILDKITE_TEST_ENGINE_TEST_RUNNER=jest
export BUILDKITE_TEST_ENGINE_RESULT_PATH=tmp/jest-result.json
```

## Configure test command
By default, bktec runs Jest with the following command:

```sh
npx jest {{testExamples}} --json --testLocationInResults --outputFile {{resultPath}}
```

In this command, `{{testExamples}}` is replaced by bktec with the list of test files or tests to run, and `{{resultPath}}` is replaced with the value set in `BUILDKITE_TEST_ENGINE_RESULT_PATH`. You can customize this command using the `BUILDKITE_TEST_ENGINE_TEST_CMD` environment variable.

To customize the test command, set the following environment variable:
```sh
export BUILDKITE_TEST_ENGINE_TEST_CMD="yarn test {{testExamples}} --json --testLocationInResults --outputFile {{resultPath}}"
```

> [!IMPORTANT]
> Make sure to append `--json --testLocationInResults --outputFile {{resultPath}}` in your custom test command, as bktec requires this to read the test results for retries and verification purposes.
## Filter test files
By default, bktec runs test files that match the `**/{__tests__/**/*,*.spec,*.test}.{ts,js,tsx,jsx}` pattern. You can customize this pattern using the `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` environment variable. For instance, to configure bktec to only run Jest test files inside the `src/components` directory, use:

```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN=src/components/**/*.test.{ts,tsx}
```

Additionally, you can exclude specific files or directories that match a certain pattern using the `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` environment variable. For example, to exclude test files inside the `src/utilities` directory, use:

```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN=src/utilities
```

You can also use both `BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN` and `BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN` simultaneously. For example, to run all Jest test files with `spec.ts`, except those in the `src/components` directory, use:

```sh
export BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN=**/*.spec.ts
export BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN=src/components
```

> [!TIP]
> This option accepts the pattern syntax supported by the [zzglob](https://github.com/DrJosh9000/zzglob?tab=readme-ov-file#pattern-syntax) library.
## Automatically retry failed tests
You can configure bktec to automatically retry failed tests using the `BUILDKITE_TEST_ENGINE_RETRY_COUNT` environment variable. When this variable is set to a number greater than `0`, bktec will retry each failed test up to the specified number of times, using the following command:

```sh
npx yarn --testNamePattern '{{testNamePattern}}' --json --testLocationInResults --outputFile {{resultPath}}
```

In this command, `{{testNamePattern}}` is replaced by bktec with the list of failed tests to run, and `{{resultPath}}` is replaced with the value set in `BUILDKITE_TEST_ENGINE_RESULT_PATH`. You can customize this command using the `BUILDKITE_TEST_ENGINE_RETRY_CMD` environment variable.

To enable automatic retry and customize the retry command, set the following environment variable:
```sh
export BUILDKITE_TEST_ENGINE_RETRY_CMD="yarn test --testNamePattern '{{testNamePattern}}' --json --testLocationInResults --outputFile {{resultPath}}"
export BUILDKITE_TEST_ENGINE_RETRY_COUNT=2
```

> [!IMPORTANT]
> Make sure to append `--testNamePattern '{{testNamePattern}}' --json --testLocationInResults --outputFile {{resultPath}}` in your custom retry command.
Loading

0 comments on commit 6d75409

Please sign in to comment.