Welcome back to my blog series on using k6 to test the performance of PokeAPI! In this fourth instalment, I'll introduce the concept of assertions and their importance in validating API responses. I will demonstrate how to use assertions for response status codes, response times, and JSON data. Finally, I will discuss how to deal with failed assertions and how to handle them. So, let's begin.
Understanding assertions and their importance in validating API responses
A critical component of performance testing are assertions, as they help ensure that the API is handling the load and returning the expected results. The assertions validate that the API is functioning properly under various conditions. Assertions can be used to check for specific conditions in API responses, such as status codes, response times, and data correctness.
Implementing assertions for response status codes, response times, and JSON data
For API responses, k6 allows us to add assertions to our test scripts to validate various aspects. Here is an example of how to implement assertions for response status codes, response times, and JSON data.
import http from 'k6/http';
import { check } from 'k6';
export let options = {
vus: 10,
duration: '30s',
};
export default function () {
const response = http.get('https://pokeapi.co/api/v2/pokemon/1');
check(response, {
'status is 200': (r) => r.status === 200,
'response time is less than 200ms': (r) => r.timings.duration < 200,
});
const jsonData = JSON.parse(response.body);
check(jsonData, {
'pokemon has the correct name': (data) => data.name === 'bulbasaur',
'pokemon has the correct height': (data) => data.height === 7,
});
}
In this example, I've added assertions to check the following conditions:
- The response status code is 200 (OK).
- The response time is less than 200 milliseconds.
- The Pokémon's name is 'bulbasaur'.
- The Pokémon's height is 7.
Here are my results, you can see that for 10 iterations, I had a response time of over 200ms:
✓ status is 200
✗ response time is less than 200ms
↳ 99% — ✓ 4520 / ✗ 10
✓ pokemon has the correct name
✓ pokemon has the correct height
checks.........................: 99.94% ✓ 18110 ✗ 10
data_received..................: 1.0 GB 33 MB/s
data_sent......................: 1.8 MB 61 kB/s
http_req_blocked...............: avg=200.15µs min=0s med=0s max=101.94ms p(90)=1µs p(95)=1µs
http_req_connecting............: avg=30.77µs min=0s med=0s max=18.91ms p(90)=0s p(95)=0s
http_req_duration..............: avg=53.93ms min=24.28ms med=50.37ms max=984.96ms p(90)=74.17ms p(95)=81.83ms
{ expected_response:true }...: avg=53.93ms min=24.28ms med=50.37ms max=984.96ms p(90)=74.17ms p(95)=81.83ms
http_req_failed................: 0.00% ✓ 0 ✗ 4530
http_req_receiving.............: avg=24.37ms min=221µs med=20.76ms max=301.92ms p(90)=43.06ms p(95)=50.09ms
http_req_sending...............: avg=80.93µs min=26µs med=48µs max=9.06ms p(90)=86µs p(95)=120µs
http_req_tls_handshaking.......: avg=103.29µs min=0s med=0s max=56.23ms p(90)=0s p(95)=0s
http_req_waiting...............: avg=29.48ms min=13.68ms med=27.7ms max=956.39ms p(90)=38.27ms p(95)=43.11ms
http_reqs......................: 4530 150.676422/s
iteration_duration.............: avg=66.28ms min=33.03ms med=62.83ms max=993.55ms p(90)=88.02ms p(95)=96.67ms
iterations.....................: 4530 150.676422/s
vus............................: 10 min=10 max=10
vus_max........................: 10 min=10 max=10
Error handling and best practices for dealing with failed assertions
When an assertion fails, it's essential to handle the failure gracefully and log relevant information to help diagnose the issue. k6 automatically logs failed assertions, making it easier to identify and address issues in the API.
Here are some best practices for dealing with failed assertions:
- Be specific with your assertions: Clearly define what you expect from the API response to ensure that you're accurately testing its functionality.
- Keep assertions simple: Complex assertions can be hard to understand and maintain. Keep them simple and focused on specific aspects of the response.
- Group related assertions: Grouping related assertions makes it easier to see the overall status of a test and identify specific areas that need attention.
- Log relevant information: The request URL, response status code, and response body can be used to help diagnose the issue when an assertion fails.
To recap, in this blog post, I've introduced the concept of assertions and their importance in validating API responses. I've also shown how to implement assertions for response status codes, response times, and JSON data. Finally, I've discussed error handling and best practices for dealing with failed assertions.
Stay tuned for the next instalment in my blog series, where I'll explore advanced k6 features and techniques for optimizing PokeAPI performance testing further.