I am trying to find the effective way to automate the API contract validation. Validations should cover
- Field Mandatory or Optional
- Field length
- Field Type
- Structure
- Values not allowed
We are using Java with RESTAssured. For few APIs, in the past I have developed individual tests for each field & each validation (like below). Ex: Test Employee Name is Mandatory (Submit an API request with empty Employee Name) Test Employee Name is String (Submit an API request with number in Employee Name) Test Employee name field length (Submit an API request with empty Employee Name more than allowed length)
This approach works but I guess there might be effective ways. Looking for suggestions.
CodePudding user response:
I think using parameterized test will solve a part of your problem. The main idea is:
Provide a csv file (or any kinds), each line can contain data test and corresponding error message.
One line = one test.
Example:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class DemoTest {
@ParameterizedTest
@CsvSource({
",name cannot be blank",
"lucas,name cannot less than 6 chars"
})
void test1(String name, String errorMsg){
System.out.printf("name=%s, errorMsg=%s", name, errorMsg);
}
}
The example goes with Junit5, you can do the same with TestNG @DataProvider
CodePudding user response:
What you are referring to is JSON schema , you can create a schema for your api response mentioning which all fields are mandatory and then use it with postman or restassured .
You can generate schema using :