#
Adding/editing assertions
"Generate using AI" is good to get started with assertions, but at some point, you will need to add your own assertions as per the requirement. KushoAI assertions can be written in JS in the Chai.js expect syntax. Each line should be an expect statement and is considered an independent assertion that is checked against the API response. Each assertion runs independently, so the failure of one assertion will not stop the execution of the remaining assertions.
Here are some examples of commonly used assertion functions:
- Check if a field is present
expect(beverages).to.have.property('tea')
- Check if a field is of a particular datatype
expect(foo).to.be.a('string');
- Check if a field has a particular value
expect(foo).to.equal('bar');
- Check if a string field is of a certain length
expect(foo).to.have.lengthOf(3);
- Check if a number is between a range
expect(response.statusCode).to.be.within(200, 299); // inclusive of boundries
expect(response.statusCode).to.be.above(199).and.below(300); // exclusive of boundries
To add a custom error message that is shown when the assertion fails, do this:
expect(response.statusCode, "Custom error for wrong status code").to.be.within(200, 299);
Although Chai.js functions are fluent and you can chain them on a single line, we recommend keeping each check separate for better readability and understanding of what exactly resulted in a test failure.