REST-EZ supports re-using specs & their result via the this.runSpec()
method
You can re-use an existing spec in your suite with JS:
1linkmeta:
2link name: star wars api using yaml anchors
3linkconfiguration:
4link scheme: http
5link host: swapi.dev
6linkspecs:
7link - name: fetch film by film ID
8link request:
9link path: /api/films/{filmId}/
10link method: get
11link path_params:
12link - name: filmId
13link value: 1
14link response:
15link status_code: 200
16link - name: fetch person for film 2
17link request:
18link path: /api/people/{personId}/
19link method: get
20link before_test:
21link run_type: inline
22link inline:
23link function: !js/asyncFunction >
24link async function() {
25link var response = await this.runSpec('fetch film by film ID');
26link // name: "A New Hope"
27link this.suite.film = JSON.parse(response.body);
28link var characterUrl = this.suite.film.characters[0];
29link var personId = characterUrl.match(/\/api\/people\/(\d+)\//)[1];
30link this.test.path_params = { personId };
31link }
32link response:
33link status_code: 200
Intrasuite dependencies can be specified to allow using specs in a different suite
Use the spec_dependencies
suite-level property to enable this. Paths are relative to current working directory of Node process.
ℹ️ NOTE: that these imported specs will only be available when you run them using
this.runSpec
.
1linkmeta:
2link name: Suite Relying on a Dependency
3linkspec_dependencies:
4link - specs/existing-suite.yml
5linkconfiguration:
6link scheme: http
7link host: swapi.dev
8linkspecs:
9link - name: fetch person for film 2
10link request:
11link path: /api/people/{personId}/
12link method: get
13link before_test:
14link run_type: inline
15link inline:
16link function: !js/asyncFunction >
17link async function() {
18link var response = await this.runSpec('fetch film by film ID');
19link // name: "A New Hope"
20link this.suite.film = JSON.parse(response.body);
21link var characterUrl = this.suite.film.characters[0];
22link var personId = characterUrl.match(/\/api\/people\/(\d+)\//)[1];
23link this.test.path_params = { personId };
24link }
25link response:
26link status_code: 200
1linkmeta:
2link name: Suite With an Existing Spec
3linkconfiguration:
4link scheme: http
5link host: swapi.dev
6linkspecs:
7link - name: fetch film by film ID
8link request:
9link path: /api/films/{filmId}/
10link method: get
11link path_params:
12link - name: filmId
13link value: 1
14link response:
15link status_code: 200
💡 To use a path relative to suite's path, use the
locate_files_relative
parameter of the suitemeta
section.
See also: Example: Run Specs from External File
Root-level suite property.
A list of file paths that this suite relies on to use runSpec
method.
1linkspec_dependencies:
2link - specs/some-suite.yml
3link - specs/some-other-suite.yml
4link# ...
runSpec(name, options)
string
required Name of spec to run (identified by name
property of the spec)object
optional Override the original spec (e.g. headers
, query_params
, path_params
, etc.)Returns Promise<Response>
The response generated by the spec
1link/**
2link * Run an existing spec in this suite
3link * or an external suite connected via interspec_dependencies suite config
4link *
5link * @param {string} specName Name of spec to run (identified by `name` property of the spec)
6link * @param {object} options Override the original spec
7link * @param {string} options.path The URL
8link * @param {string} options.method HTTP request method
9link * @param {array} options.headers Object of key/value pairs to set as headers
10link * @param {array} options.path_params Object of key/value pairs to set in the path
11link * @param {array} options.query_params Object of key/value pairs to add as query params ?name=value&name2=value2
12link * @param {object} options.payload Request body payload
13link *
14link * @return Promise<Response>
15link */
16linkfunction runSpec(specName, options) { }
1link# ...
2link - name: generate auth token
3link request:
4link path: /auth/login
5link method: post
6link payload:
7link body:
8link type: json
9link content:
10link email: foo@bar.com
11link password: F2#@9!mls
12link response:
13link status_code: 201
14link json_data:
15link - name: $.token
16link value: !!js/regexp /^[A-Z0-9]{32}$
17link - name: fetch a person
18link request:
19link path: /user
20link method: get
21link before_test:
22link run_type: inline
23link inline:
24link function: !js/asyncFunction >
25link async function() {
26link var response = await this.runSpec('generate auth token');
27link var { token } = JSON.parse(response.body);
28link this.test.headers = {
29link 'Authorization': `Bearer ${token}`
30link };
31link }
32link response:
33link status_code: 200