debian-mirror-gitlab/doc/user/project/merge_requests/fail_fast_testing.md

99 lines
3.9 KiB
Markdown
Raw Normal View History

2020-07-28 23:09:34 +05:30
---
stage: Verify
group: Testing
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-07-28 23:09:34 +05:30
type: reference, howto
---
# Fail Fast Testing **(PREMIUM)**
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/198550) in GitLab 13.1.
For applications that use RSpec for running tests, we've introduced the `Verify/Failfast`
[template to run subsets of your test suite](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates/Verify/FailFast.gitlab-ci.yml),
based on the changes in your merge request.
The template uses the [test_file_finder (`tff`) gem](https://gitlab.com/gitlab-org/ci-cd/test_file_finder/)
that accepts a list of files as input, and returns a list of spec (test) files
that it believes to be relevant to the input files.
`tff` is designed for Ruby on Rails projects, so the `Verify/FailFast` template is
configured to run when changes to Ruby files are detected. By default, it runs in
2021-10-27 15:23:28 +05:30
the [`.pre` stage](../../../ci/yaml/index.md#stage-pre) of a GitLab CI/CD pipeline,
2020-07-28 23:09:34 +05:30
before all other stages.
## Example use case
Fail fast testing is useful when adding new functionality to a project and adding
new automated tests.
Your project could have hundreds of thousands of tests that take a long time to complete.
You may be confident that a new test will pass, but you have to wait for all the tests
to complete to verify it. This could take an hour or more, even when using parallelization.
Fail fast testing gives you a faster feedback loop from the pipeline. It lets you
know quickly that the new tests are passing and the new functionality did not break
other tests.
## Requirements
This template requires:
- A project built in Rails that uses RSpec for testing.
- CI/CD configured to:
- Use a Docker image with Ruby available.
2021-09-30 23:02:18 +05:30
- Use [Pipelines for merge requests](../../../ci/pipelines/merge_request_pipelines.md#configure-pipelines-for-merge-requests)
- [Pipelines for Merged Results](../../../ci/pipelines/pipelines_for_merged_results.md#enable-pipelines-for-merged-results)
2020-07-28 23:09:34 +05:30
enabled in the project settings.
2021-11-11 11:23:49 +05:30
- A Docker image with Ruby available. The template uses `image: ruby:2.6` by default, but you [can override](../../../ci/yaml/includes.md#override-included-configuration-values) this.
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
## Configuring Fast RSpec Failure
2020-07-28 23:09:34 +05:30
2021-06-08 01:23:25 +05:30
We use the following plain RSpec configuration as a starting point. It installs all the
2020-07-28 23:09:34 +05:30
project gems and executes `rspec`, on merge request pipelines only.
```yaml
rspec-complete:
stage: test
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- bundle install
- bundle exec rspec
```
2021-09-30 23:02:18 +05:30
To run the most relevant specs first instead of the whole suite, [`include`](../../../ci/yaml/index.md#include)
2020-07-28 23:09:34 +05:30
the template by adding the following to your CI/CD configuration:
```yaml
include:
- template: Verify/FailFast.gitlab-ci.yml
```
2020-10-24 23:57:45 +05:30
To customize the job, specific options may be set to override the template. For example, to override the default Docker image:
```yaml
include:
- template: Verify/FailFast.gitlab-ci.yml
rspec-rails-modified-path-specs:
image: custom-docker-image-with-ruby
```
2020-07-28 23:09:34 +05:30
### Example test loads
For illustrative purposes, let's say our Rails app spec suite consists of 100 specs per model for ten models.
If no Ruby files are changed:
2021-06-08 01:23:25 +05:30
- `rspec-rails-modified-paths-specs` does not run any tests.
- `rspec-complete` runs the full suite of 1000 tests.
2020-07-28 23:09:34 +05:30
If one Ruby model is changed, for example `app/models/example.rb`, then `rspec-rails-modified-paths-specs`
2021-06-08 01:23:25 +05:30
runs the 100 tests for `example.rb`:
2020-07-28 23:09:34 +05:30
- If all of these 100 tests pass, then the full `rspec-complete` suite of 1000 tests is allowed to run.
2021-06-08 01:23:25 +05:30
- If any of these 100 tests fail, they fail quickly, and `rspec-complete` does not run any tests.
2020-07-28 23:09:34 +05:30
The final case saves resources and time as the full 1000 test suite does not run.