In this lab we are going to look at testing our application using test cases that are generated using the OAS(OpenAPI Spec). To accomplish this task we will be using a tool called Schemathesis.
Schemathesis is a tool for testing your web applications built with an Open API specifications. It reads the application schema and generates test cases which will ensure that your application is compliant with its schema. The nice thing about Schemathesis is that The application being test could be written in any language, the only thing you need is a valid API schema in a supported format. For more information on Schemathesis, you can check out the project page here.
We'll be using the Todo project that we created at the beginning of the series to test with. If you need a new copy of the project follow these steps:
https://github.com/redhat-appdev-practice/schemathesis-lab.git
mvn spring-boot:run
pip install --user virtualenv
python -m venv myvenv
source myvenv/bin/activate
source myvenv/Scripts/activate
for Windowspip install schemathesis
mvn spring-boot:run
In our project we are using an OAS that is in a remote repo, Scemathesis allows you to run tests against both local remote schemas.
schemathesis run todo.yaml --base-url http://localhost:8080
For remote: schemathesis run https://raw.githubusercontent.com/redhat-appdev-practice/schemathesis-lab/master/todo.yaml --base-url http://localhost:8080
Note: there should be failures for both of these runs
Add the following to the TodosApiController:
@Override
public ResponseEntity<Void> createTodo( Todo todo, Boolean completed) {
return new ResponseEntity<>(HttpStatus.valueOf(200);
}
@Override
public ResponseEntity<Void> deleteTodo(String todoId) {
return new ResponseEntity<>(HttpStatus.valueOf(200);
}
@Override
public ResponseEntity<List<Todo>> getTodos( Boolean completed) {
return new ResponseEntity<>(HttpStatus.valueOf(200);
}
@Override
public ResponseEntity<Void> updateTodo(String todoId, Todo todo) {
return new ResponseEntity<>(HttpStatus.valueOf(200);
}
schemathesis run todo.yaml --base-url http://localhost:8080
schemthesis run --help
to see more testing options, specifically the --checks option.schemathesis run todo.yaml --checks all --base-url http://localhost:8080
Update the TodosApiController methods to conform to the OAS:
@Override
public ResponseEntity<Void> createTodo(@Valid Todo todo, @Valid Boolean completed) {
return new ResponseEntity<>(HttpStatus.valueOf(201));
}
@Override
public ResponseEntity<Void> deleteTodo(String todoId) {
return new ResponseEntity<>(HttpStatus.valueOf(204));
}
@Override
public ResponseEntity<List<Todo>> getTodos(@Valid Boolean completed) {
return ResponseEntity.status(200).body(new ArrayList<Todo>() );
}
@Override
public ResponseEntity<Void> updateTodo(String todoId, Todo todo) {
return new ResponseEntity<>(HttpStatus.valueOf(202));
}
schemathesis run todo.yaml --checks all --base-url http://localhost:8080
Create python file test.py:
# test.py
import schemathesis
schema = schemathesis.from_path('./todo.yaml')
schema.base_url = 'http://localhost:8080'
@schema.parametrize()
def test_time(case):
response = case.call()
assert response.elapsed.total_seconds() < 1