Skip to content

Commit a142ce0

Browse files
committed
Testcase example added
1 parent 8294b45 commit a142ce0

4 files changed

Lines changed: 95 additions & 18 deletions

File tree

docs/assets/testcase-eximg-01.png

56.5 KB
Loading

docs/examples/testcase-examples.md

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,114 @@ Case-wise more example can be found in [https://github.com/chkware/cli](https://
1313

1414
[Testcase specification document reference](/references/testcase-reference)
1515

16-
Testcase is an experimental feature, in _`PRE_ALPHA`_ stage.
16+
### A minimal Testcase with in-file request
1717

18-
### Testcase with in-file request
18+
```yaml
19+
---
20+
version: 'default:testcase:0.7.2'
21+
22+
request:
23+
url: "https://reqres.in/api/users"
24+
method: POST
25+
body[json]: {
26+
'name': My Name,
27+
'job': My Job,
28+
}
29+
30+
spec:
31+
asserts:
32+
- {type: AssertEqual, actual: $_response.code, expected: 201}
33+
- {type: AssertIsMap, actual: $_response.body}
34+
```
35+
36+
The above testcase spec. doc define a request that makes a `POST` call to `https://reqres.in/api/users` URL with a body `{"name": "My Name", "job": "My Job"}`.
37+
38+
After the request server responses with following:
39+
40+
![reqres.in response](../assets/testcase-eximg-01.png)
41+
42+
On the `asserts`, we are asserting that
43+
44+
- The response code is 201, or the resource was created
45+
- The response if a map / dictionary
46+
47+
Assertion [reference with examples](/references/testcase-reference#assertions) can be found here.
48+
49+
### A minimal Testcase with out-file request
50+
51+
A http spec. doc can be written on separate file, that does same as above request. Let's call it `same-request.chk`. The http spec. doc by default exposes `$_response` object.
1952

2053
```yaml
54+
# file: same-request.chk
55+
---
56+
version: 'default:http:0.7.2'
57+
58+
request:
59+
url: "https://reqres.in/api/users"
60+
method: POST
61+
body[json]: {
62+
'name': My Name,
63+
'job': My Job,
64+
}
65+
```
66+
67+
Now we can point out above file to execute before make assertions like below (assuming both file lives on same directory).
68+
69+
```yaml
70+
# file: same-testcase.chk
2171
---
2272
version: 'default:testcase:0.7.2'
2373
74+
spec:
75+
execute:
76+
file: "./same-request.chk"
77+
asserts:
78+
- {type: AssertEqual, actual: $_response.code, expected: 201}
79+
- {type: AssertIsMap, actual: $_response.body}
80+
```
81+
82+
Please notice the `$_response` in the testcase doc. This variable is available after the request gets executed as local variable.
83+
84+
### A Testcase with out-file request passing data
85+
86+
To pass data from a testcase spec. doc to a http spec. doc, we first need to add some variables. These variables can also have some default value. Say in the following case, we defined `name` and `job` variables with default value. So, if no value passed these values will be passed to server as part of request body.
87+
88+
```yaml
89+
# file: same-request.chk
90+
---
91+
version: 'default:http:0.7.2'
92+
2493
variables:
25-
Name: 'Morpheus'
26-
Job: 'leader'
27-
Response: ~
28-
Server: https://reqres.in/api/v1
94+
name: My Name
95+
job: My Job
2996
3097
request:
31-
url: "{$Server}/users"
98+
url: "https://reqres.in/api/users"
3299
method: POST
33100
body[json]: {
34-
'name': $Name,
35-
'job': $Job,
101+
'name': $name,
102+
'job': $job,
36103
}
37-
return: ~
104+
```
105+
106+
Now we can point out which value we want to pass using `with` statement in `execute` block.
107+
108+
```yaml
109+
# file: same-testcase.chk
110+
---
111+
version: 'default:testcase:0.7.2'
38112
39113
spec:
40114
execute:
41-
file: ~
42-
result: $Response
43-
115+
file: "./same-request.chk"
116+
with:
117+
name: Her Name
118+
job: Her Job
44119
asserts:
45-
- {type: AssertEqual, actual: $Response.code, expected: 201}
46-
- {type: AssertIsMap, actual: $Response.body}
120+
- {type: AssertEqual, actual: $_response.code, expected: 201}
121+
- {type: AssertIsMap, actual: $_response.body}
47122
```
48123

49-
Assertion [reference with examples](/references/testcase-reference#assertions) can be found here.
124+
Please notice that if we do not set a `with` then request will be sent with default value.
125+
126+
Assertion [reference with examples](/references/testcase-reference#assertions) can be found here.

docs/references/testcase-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ expose:
5656
```yaml
5757
# file: some-request.chk
5858
---
59-
version: 'default:testcase:0.7.2'
59+
version: 'default:http:0.7.2'
6060
6161
variables:
6262
Name: 'Morpheus'

docs/tutorials/feature-test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Let's continue to test our [_XKCD.com JSON API http client_](/tutorials/http-cli
5555

5656
Let us go through the testcase spec. we wrote. If you call the API `https://xkcd.com/614/info.0.json` in any Firefox, the response will be shown like this:
5757

58-
![CHKware](../assets/http-resp-xkcdcom.png)
58+
![xkcd.com response](../assets/http-resp-xkcdcom.png)
5959

6060
Please notice that the response contains node `num` and `year` on it. Thus on the spec. in `asserts` we are doing three assertions.
6161
- If response we got, has code 200; otherwise whether the response was successful.

0 commit comments

Comments
 (0)