Skip to content

Commit 18a0d74

Browse files
committed
add feature test tutorial
1 parent 3901044 commit 18a0d74

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

docs/assets/http-resp-xkcdcom.png

212 KB
Loading

docs/tutorials/feature-test.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: Write feature test for API
3+
---
4+
5+
:::note
6+
- **Prerequisite**: First, [setup **CHKware**](/setup) to continue
7+
- Find [More `testcase` examples](/examples/testcase-examples) here
8+
:::
9+
10+
Let's continue to test our [_XKCD.com JSON API http client_](/tutorials/http-client) that we covered earlier. Please do as follows:
11+
12+
- Create a file called `xkcd-joke-test.chk` on the same directory where your created `xkcd-joke.chk` early.
13+
- Open `xkcd-joke-test.chk` file, and add following spec.
14+
15+
As you can see, it's possible to combine both `http` request and `testcase` spec on same file. See [Testcase example](/examples/testcase-examples) for other asserts you can write.
16+
17+
```yaml
18+
---
19+
version: default:testcase:0.7.2
20+
request:
21+
url: https://xkcd.com/614/info.0.json
22+
method: GET
23+
24+
spec:
25+
asserts:
26+
- {type: AssertEqual, actual: $_response.code, expected: 200}
27+
- {type: AssertIsInt, actual: $_response.body.num}
28+
- {type: AssertEqual, actual: $_response.body.year, expected: 2009}
29+
```
30+
31+
Here notice the `verison` string **`version: default:testcase:0.7.2`**, this is important for this specification to be a [testcase specification](/references/testcase-reference).
32+
33+
- Open a terminal. Hit enter after writing following command on terminal.
34+
35+
```bash
36+
chk testcase xkcd-joke-test.chk
37+
```
38+
39+
You'll get output like following. Data will vary depending on the day you are doing it.
40+
41+
```bash
42+
File: xkcd-joke-test.chk
43+
44+
- Making request [Success]
45+
- Process data for assertion [Success]
46+
- Prepare exposable [Success]
47+
48+
---
49+
-> Running `AssertEqual` on `$_response.code` [Success]
50+
-> Running `AssertIsInt` on `$_response.body.num` [Success]
51+
-> Running `AssertEqual` on `$_response.body.year` [Success]
52+
```
53+
54+
You just tested a live API :rocket::star2:.
55+
56+
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:
57+
58+
![CHKware](../assets/http-resp-xkcdcom.png)
59+
60+
Please notice that the response contains node `num` and `year` on it. Thus on the spec. in `asserts` we are doing three assertions.
61+
- If response we got, has code 200; otherwise whether the response was successful.
62+
- If response body contains a node `num`, and does it hold an integer value.
63+
- If response body contains a node `year`, and does it hold value `2009`.
64+
65+
- Now If you add `--result` or `-r` flag to the command then it should show you the result in formatted output.
66+
```bash
67+
chk testcase xkcd-joke-test.chk --result
68+
```
69+
70+
You should be able to see following if no exception occurs.
71+
72+
```bash
73+
-> Running `AssertEqual` on `$_response.code` [Success]
74+
-> Running `AssertIsInt` on `$_response.body.num` [Success]
75+
-> Running `AssertEqual` on `$_response.body.year` [Success]
76+
```
77+
78+
- These above response are coming as formatted output. If you want to see all JSON just pass the `--no-fomat` or `-nf` option flag.
79+
```bash
80+
chk testcase xkcd-joke-test.chk --result --no-format
81+
```
82+
and response looks like this.
83+
84+
```bash
85+
[[{"name": "AssertEqual", "name_run": "AssertEqual_bf476fda9fa211ed8f6dca2350850d2e", "actual_original": "$_response.code", "is_success": true, "message": "", "assert_fn": ""}, {"name": "AssertIsInt", "name_run": "AssertIsInt_bf4783309fa211ed8f6dca2350850d2e", "actual_original": "$_response.body.num", "is_success": true, "message": "", "assert_fn": ""}, {"name": "AssertEqual", "name_run": "AssertEqual_bf4784709fa211ed8f6dca2350850d2e", "actual_original": "$_response.body.year", "is_success": true, "message": "", "assert_fn": ""}]]
86+
```
87+
---
88+
89+
This way you can write API feature test. More [testcase examples](/examples/testcase-examples) here.
90+
91+
:wink::tada::confetti_ball:

docs/tutorials/http-client.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ Let's call the [_XKCD.com JSON API_](https://xkcd.com/json.html) that returns a
104104
[{"code": 200, "version": "HTTP/1.1", "reason": "OK", "headers": {"Connection": "keep-alive", "Content-Length": "660", "Server": "nginx", "Content-Type": "application/json", "Last-Modified": "Fri, 27 Jan 2023 12:54:53 GMT", "ETag": "W/\"63d3c99d-5ac\"", "Expires": "Sat, 28 Jan 2023 21:16:28 GMT", "Cache-Control": "max-age=300", "Content-Encoding": "gzip", "Via": "1.1 varnish, 1.1 varnish", "Accept-Ranges": "bytes", "Date": "Sun, 29 Jan 2023 06:13:14 GMT", "Age": "0", "X-Served-By": "cache-dfw-kdfw8210071-DFW, cache-qpg1267-QPG", "X-Cache": "HIT, HIT", "X-Cache-Hits": "1, 1", "X-Timer": "S1674972795.677591,VS0,VE316", "Vary": "Accept-Encoding"}, "body": {"month": "7", "num": 614, "link": "", "year": "2009", "news": "", "safe_title": "Woodpecker", "transcript": "[[A man with a beret and a woman are standing on a boardwalk, leaning on a handrail.]]\nMan: A woodpecker!\n<<Pop pop pop>>\nWoman: Yup.\n\n[[The woodpecker is banging its head against a tree.]]\nWoman: He hatched about this time last year.\n<<Pop pop pop pop>>\n\n[[The woman walks away. The man is still standing at the handrail.]]\n\nMan: ... woodpecker?\nMan: It's your birthday!\n\nMan: Did you know?\n\nMan: Did... did nobody tell you?\n\n[[The man stands, looking.]]\n\n[[The man walks away.]]\n\n[[There is a tree.]]\n\n[[The man approaches the tree with a present in a box, tied up with ribbon.]]\n\n[[The man sets the present down at the base of the tree and looks up.]]\n\n[[The man walks away.]]\n\n[[The present is sitting at the bottom of the tree.]]\n\n[[The woodpecker looks down at the present.]]\n\n[[The woodpecker sits on the present.]]\n\n[[The woodpecker pulls on the ribbon tying the present closed.]]\n\n((full width panel))\n[[The woodpecker is flying, with an electric drill dangling from its feet, held by the cord.]]\n\n{{Title text: If you don't have an extension cord I can get that too. Because we're friends! Right?}}", "alt": "If you don't have an extension cord I can get that too. Because we're friends! Right?", "img": "https://imgs.xkcd.com/comics/woodpecker.png", "title": "Woodpecker", "day": "24"}}]
105105
```
106106
---
107+
This way you can use `chk http` as your script-able http client.
107108

108-
This way you can use `chk http` as your script-able http client. Let's write a feature test or [testcase](/examples/testcase-examples) for this request.
109+
Next, let's write a feature test or [testcase](/tutorials/feature-test) for this request.
109110

110111
:wink::tada::confetti_ball:

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const sidebars = {
4343
label: "Tutorial",
4444
items: [
4545
"tutorials/http-client",
46+
"tutorials/feature-test",
4647
],
4748
},
4849
"examples/http-examples",

0 commit comments

Comments
 (0)