This diagram demonstrates the relative priority of each test type we use. e2e
stands for end-to-end.
As of 2025-02-03, we have the following estimated distribution of tests per level:
Test level Community Edition Enterprise Edition Community + Enterprise Edition Black-box tests at the system level (aka end-to-end or QA tests) 401 (0.14%) 303 (0.10%) 704 (0.24%) White-box tests at the system level (aka system or feature tests) 8,362 (2.90%) 4,082 (1.41%) 12,444 (4.31%) Integration tests 39,716 (13.76%) 17,411 (6.03%) 57,127 (19.79%) Unit tests 139,504 (48.32%) 78,955 (27.35%) 218,459 (75.66%) Unit testsFormal definition: https://en.wikipedia.org/wiki/Unit_testing
These kind of tests ensure that a single unit of code (a method) works as expected (given an input, it has a predictable output). These tests should be isolated as much as possible. For example, model methods that don’t do anything with the database shouldn’t need a DB record. Classes that don’t need database records should use stubs/doubles as much as possible.
Code path Tests path Testing engine Notesapp/assets/javascripts/
spec/frontend/
Jest More details in the Frontend Testing guide section. app/finders/
spec/finders/
RSpec app/graphql/
spec/graphql/
RSpec app/helpers/
spec/helpers/
RSpec app/models/
spec/models/
RSpec app/policies/
spec/policies/
RSpec app/presenters/
spec/presenters/
RSpec app/serializers/
spec/serializers/
RSpec app/services/
spec/services/
RSpec app/uploaders/
spec/uploaders/
RSpec app/validators/
spec/validators/
RSpec app/views/
spec/views/
RSpec app/workers/
spec/workers/
RSpec bin/
spec/bin/
RSpec config/
spec/config/
RSpec config/initializers/
spec/initializers/
RSpec config/routes.rb
, config/routes/
spec/routing/
RSpec config/puma.example.development.rb
spec/rack_servers/
RSpec db/
spec/db/
RSpec db/{post_,}migrate/
spec/migrations/
RSpec More details in the Testing Rails migrations guide. Gemfile
spec/dependencies/
, spec/sidekiq/
RSpec lib/
spec/lib/
RSpec lib/tasks/
spec/tasks/
RSpec rubocop/
spec/rubocop/
RSpec spec/support/
spec/support_specs/
RSpec Frontend unit tests
Unit tests are on the lowest abstraction level and typically test functionality that is not directly perceivable by a user.
graph RL plain[Plain JavaScript]; Vue[Vue Components]; feature-flags[Feature flags]; license-checks[License Checks]; plain---Vuex; plain---GraphQL; Vue---plain; Vue---Vuex; Vue---GraphQL; browser---plain; browser---Vue; plain---backend; Vuex---backend; GraphQL---backend; Vue---backend; backend---database; backend---feature-flags; backend---license-checks; class plain tested; class Vuex tested; classDef node color:#909090,fill:#f0f0f0,stroke-width:2px,stroke:#909090 classDef label stroke-width:0; classDef tested color:#000000,fill:#a0c0ff,stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; subgraph " " tested; mocked; class tested tested; endWhen to use unit tests
Component tests cover the state of a single component that is perceivable by a user depending on external signals such as user input, events fired from other components, or application state.
graph RL plain[Plain JavaScript]; Vue[Vue Components]; feature-flags[Feature flags]; license-checks[License Checks]; plain---Vuex; plain---GraphQL; Vue---plain; Vue---Vuex; Vue---GraphQL; browser---plain; browser---Vue; plain---backend; Vuex---backend; GraphQL---backend; Vue---backend; backend---database; backend---feature-flags; backend---license-checks; class Vue tested; classDef node color:#909090,fill:#f0f0f0,stroke-width:2px,stroke:#909090 classDef label stroke-width:0; classDef tested color:#000000,fill:#a0c0ff,stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; subgraph " " tested; mocked; class tested tested; endWhen to use component tests
shallowMount()
Formal definition: https://en.wikipedia.org/wiki/Integration_testing
These kind of tests ensure that individual parts of the application work well together, without the overhead of the actual app environment (such as the browser). These tests should assert at the request/response level: status code, headers, body. They’re useful, for example, to test permissions, redirections, API endpoints, what view is rendered, and so forth.
Code path Tests path Testing engine Notesapp/controllers/
spec/requests/
, spec/controllers
RSpec Request specs are preferred over legacy controller specs. Request specs are encouraged for API endpoints. app/mailers/
spec/mailers/
RSpec lib/api/
spec/requests/api/
RSpec app/assets/javascripts/
spec/frontend/
Jest More details below Frontend integration tests
Integration tests cover the interaction between all components on a single page. Their abstraction level is comparable to how a user would interact with the UI.
graph RL plain[Plain JavaScript]; Vue[Vue Components]; feature-flags[Feature flags]; license-checks[License Checks]; plain---Vuex; plain---GraphQL; Vue---plain; Vue---Vuex; Vue---GraphQL; browser---plain; browser---Vue; plain---backend; Vuex---backend; GraphQL---backend; Vue---backend; backend---database; backend---feature-flags; backend---license-checks; class plain tested; class Vue tested; class Vuex tested; class GraphQL tested; class browser tested; linkStyle 0,1,2,3,4,5,6 stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; classDef node color:#909090,fill:#f0f0f0,stroke-width:2px,stroke:#909090 classDef label stroke-width:0; classDef tested color:#000000,fill:#a0c0ff,stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; subgraph " " tested; mocked; class tested tested; endWhen to use integration tests
index.js
files in app/assets/javascripts/pages/
): Testing the page bundles ensures the corresponding frontend components integrate well.GitLab is transitioning from controller specs to request specs.
In an ideal world, controllers should be thin. However, when this is not the case, it’s acceptable to write a system or feature test without JavaScript instead of a controller test. Testing a fat controller usually involves a lot of stubbing, such as:
controller.instance_variable_set(:@user, user)
and use methods deprecated in Rails 5.
White-box tests at the system level (formerly known as System / Feature tests)Formal definitions:
These kind of tests ensure the GitLab Rails application (for example, gitlab-foss
/gitlab
) works as expected from a browser point of view.
Note that:
These tests should only be used when:
For instance, to test the breadcrumbs on a given page, writing a system test makes sense since it’s a small component, which cannot be tested at the unit or controller level.
Only test the happy path, but make sure to add a test case for any regression that couldn’t have been caught at lower levels with better tests (for example, if a regression is found, regression tests should be added at the lowest level possible).
Tests path Testing engine Notesspec/features/
Capybara + RSpec If your test has the :js
metadata, the browser driver is Selenium, otherwise it’s using RackTest. Frontend feature tests
In contrast to frontend integration tests, feature tests make requests against the real backend instead of using fixtures. This also implies that database queries are executed which makes this category significantly slower.
See also:
graph RL plain[Plain JavaScript]; Vue[Vue Components]; feature-flags[Feature flags]; license-checks[License Checks]; plain---Vuex; plain---GraphQL; Vue---plain; Vue---Vuex; Vue---GraphQL; browser---plain; browser---Vue; plain---backend; Vuex---backend; GraphQL---backend; Vue---backend; backend---database; backend---feature-flags; backend---license-checks; class backend tested; class plain tested; class Vue tested; class Vuex tested; class GraphQL tested; class browser tested; linkStyle 0,1,2,3,4,5,6,7,8,9,10 stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; classDef node color:#909090,fill:#f0f0f0,stroke-width:2px,stroke:#909090 classDef label stroke-width:0; classDef tested color:#000000,fill:#a0c0ff,stroke:#6666ff,stroke-width:2px,stroke-dasharray: 5, 5; subgraph " " tested; mocked; class tested tested; endWhen to use feature tests
A :js
flag is added to the test to make sure the full environment is loaded:
scenario 'successfully', :js do
sign_in(create(:admin))
end
The steps of each test are written using (capybara methods).
XHR (XMLHttpRequest) calls might require you to use wait_for_requests
in between steps, such as:
find('.form-control').native.send_keys(:enter)
wait_for_requests
expect(page).not_to have_selector('.card')
Consider not writing a system test
If we’re confident that the low-level components work well (and we should be if we have enough Unit & Integration tests), we shouldn’t need to duplicate their thorough testing at the System test level.
It’s very easy to add tests, but a lot harder to remove or improve tests, so one should take care of not introducing too many (slow and duplicated) tests.
The reasons why we should follow these best practices are as follows:
Formal definitions:
GitLab consists of multiple pieces such as GitLab Shell, GitLab Workhorse, Gitaly, GitLab Pages, GitLab Runner, and GitLab Rails. All these pieces are configured and packaged by Omnibus GitLab.
The QA framework and instance-level scenarios are part of GitLab Rails so that they’re always in-sync with the codebase (especially the views).
Note that:
Every new feature should come with a test plan.
Tests path Testing engine Notesqa/qa/specs/features/
Capybara + RSpec + Custom QA framework Tests should be placed under their corresponding Product category
See end-to-end tests for more information.
Note that qa/spec
contains unit tests of the QA framework itself, not to be confused with the application’s unit tests or end-to-end tests.
Smoke tests are quick tests that may be run at any time (especially after the pre-deployment migrations).
These tests run against the UI and ensure that basic functionality is working.
GitLab QA orchestratorSee Smoke Tests for more information.
GitLab QA orchestrator is a tool that allows you to test that all these pieces integrate well together by building a Docker image for a given version of GitLab Rails and running end-to-end tests (using Capybara) against it.
Learn more in the GitLab QA orchestrator README.
EE-specific testsEE-specific tests follows the same organization, but under the ee/spec
folder.
As many things in life, deciding what to test at each level of testing is a trade-off:
Another way to see it is to think about the “cost of tests”, this is well explained in this article and the basic idea is that the cost of a test includes:
There are cases where the behavior you are testing is not worth the time spent running the full application, for example, if you are testing styling, animation, edge cases or small actions that don’t involve the backend, you should write an integration test using Frontend integration tests.
Return to Testing documentation
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4