Write feature test for API
- Prerequisite: First, setup CHKware to continue
- Find more testcaseexamples here
Let's continue to test our XKCD.com JSON API http client that we covered earlier. Please do as follows:
- Create a file called - xkcd-joke-test.chkon the same directory where your created- xkcd-joke.chkearly.
- Open - xkcd-joke-test.chkfile, and add following spec.- As you can see, it's possible to combine both - httprequest and- testcasespec on same file. See Testcase example for other asserts you can write.- ---
 version: default:testcase:0.7.2
 request:
 url: https://xkcd.com/614/info.0.json
 method: GET
 spec:
 asserts:
 - { type: AssertEqual, actual: "{$_response.code}", expected: 200 }
 - { type: AssertIsInt, actual: "{$_response.body.num}" }
 - { type: AssertEqual, actual: "{$_response.body.year}", expected: 2009 }- Here notice the - verisonstring- version: default:testcase:0.7.2, this is important for this specification to be a testcase specification.
- Open a terminal. Hit enter after writing following command on terminal. - chk testcase xkcd-joke-test.chk- You'll get output like following. Data will vary depending on the day you are doing it. - File: xkcd-joke-test.chk
 - Making request [Success]
 - Process data for assertion [Success]
 - Prepare exposable [Success]
 ---
 -> Running `AssertEqual` on `{$_response.code}` [Success]
 -> Running `AssertIsInt` on `{$_response.body.num}` [Success]
 -> Running `AssertEqual` on `{$_response.body.year}` [Success]- You just tested a live API 🚀🌟. - Let us go through the testcase spec. we wrote. If you call the API - https://xkcd.com/614/info.0.jsonin any Firefox, the response will be shown like this: - Please notice that the response contains node - numand- yearon it. Thus on the spec. in- assertswe are doing three assertions.- If response we got, has code 200; otherwise whether the response was successful.
- If response body contains a node num, and does it hold an integer value.
- If response body contains a node year, and does it hold value2009.
 
- Now If you add - --resultor- -rflag to the command then it should show you the result in formatted output.- chk testcase xkcd-joke-test.chk --result- You should be able to see following if no exception occurs. - -> Running `AssertEqual` on `{$_response.code}` [Success]
 -> Running `AssertIsInt` on `{$_response.body.num}` [Success]
 -> Running `AssertEqual` on `{$_response.body.year}` [Success]
- These above response are coming as formatted output. If you want to see all JSON just pass the - --no-fomator- -nfoption flag.- chk testcase xkcd-joke-test.chk --result --no-format- and response looks like this. - [[{"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": ""}]]
This way you can write API feature test. More testcase examples here.
😉🎉🎊