- Write the tests
- Test suite folder structure and naming conventions
Contract tests consist of two parts — consumer tests and provider tests. A simple example of a consumer and provider relationship is between the frontend and backend. The frontend would be the consumer and the backend is the provider. The frontend consumes the API that is provided by the backend. The test helps ensure that these two sides follow an agreed upon contract and any divergence from the contract triggers a meaningful conversation to prevent breaking changes from slipping through.
Consumer tests are similar to unit tests with each spec defining a requests and an expected mock responses and creating a contract based on those definitions. On the other hand, provider tests are similar to integration tests as each spec takes the request defined in the contract and runs that request against the actual service which is then matched against the contract to validate the contract.
You can check out the existing contract tests at:
spec/contracts/consumer/specsfor the consumer tests.
spec/contracts/provider/specsfor the provider tests.
Before running the consumer tests, go to
spec/contracts/consumer and run
npm install. To run all the consumer tests, you just need to run
npm test -- /specs. Otherwise, to run a specific spec file, replace
/specs with the specific spec filename.
Before running the provider tests, make sure your GDK (GitLab Development Kit) is fully set up and running. You can follow the setup instructions detailed in the GDK repository. To run the provider tests, you use Rake tasks that can be found in
./lib/tasks/contracts. To get a list of all the Rake tasks related to the provider tests, run
bundle exec rake -T contracts. For example:
$ bundle exec rake -T contracts rake contracts:merge_requests:pact:verify:diffs_batch # Verify provider against the consumer pacts for diffs_batch rake contracts:merge_requests:pact:verify:diffs_metadata # Verify provider against the consumer pacts for diffs_metadata rake contracts:merge_requests:pact:verify:discussions # Verify provider against the consumer pacts for discussions rake contracts:merge_requests:test:merge_requests[contract_merge_requests] # Run all merge request contract tests
To keep the consumer and provider test suite organized and maintainable, it’s important that tests are organized, also that consumers and providers are named consistently. Therefore, it’s important to adhere to the following conventions.
Having an organized and sensible folder structure for the test suite makes it easier to find relevant files when reviewing, debugging, or introducing tests.
The consumer tests are grouped according to the different pages in the application. Each file contains various types of requests found in a page. As such, the consumer test files are named using the Rails standards of how pages are referenced. For example, the project pipelines page would be the
Project::Pipeline#index page so the equivalent consumer test would be located in
When defining the location to output the contract generated by the test, we want to follow the same file structure which would be
contracts/project/pipelines/ for this example. This is the structure in
consumer/fixtures as well.
The provider tests are grouped similarly to our controllers. Each of these tests contains various tests for an API endpoint. For example, the API endpoint to get a list of pipelines for a project would be located in
provider/pact_helpers/project/pipelines/get_list_project_pipelines_helper.rb. The provider states are grouped according to the different pages in the application similar to the consumer tests.
When writing the consumer and provider tests, there are parts where a name is required for the consumer and provider. Since there are no restrictions imposed by Pact on how these should be named, a naming convention is important to keep it easy for us to figure out which consumer and provider tests are involved during debugging. Pact also uses the consumer and provider names to generate the generated contracts in the
As mentioned in the folder structure section, consumer tests are grouped according to the different pages in the application. As such, consumer names should follow the same naming format using the Rails standard. For example, the consumer test for
Project::Pipeline#index would be
ProjectPipeline#index as the consumer name. Since Pact uses this name to name the contracts it generates, the colons (
::) are dropped as colons are not valid characters in file names.
These are the API endpoints that provides the data to the consumer so they are simply named according to the API endpoint they pertain to. Be mindful that this name is as descriptive as possible. For example, if we’re writing a test for the
GET /groups/:id/projects endpoint, we don’t want to simply name it “Projects endpoint” as there is a
GET /projects endpoint as well that also fetches a list of projects the user has access to across all of GitLab. An easy way to name them is by checking out our API documentation and naming it the same way it is named in there. So the
GET /groups/:id/projects would be called
List a group’s projects and
GET /projects would be called
List all projects. Subsequently, the test files are named
There are some cases where the provider being tested may not be documented so, in those cases, fall back to choosing a name that is as descriptive as possible to ensure it’s easy to tell what the provider is for.
|Tests||Folder structure||Naming convention|
|Consumer Test||Follows the Rails reference standards. For example, ||Follows the Rails naming standard. For example, |
|Provider Test||Grouped like the Rails controllers. For example, ||Follows the API documentation naming scheme. For example, |