Unit tests have to include REST API calls between components. In the case of end to end tests the components must be deployed, but for unit testing we care for contract testing the connection, i.e. the call was done with the correct arguments.
One option to do this would be to mock the part where the actual request is sent from the SwaggerClient, but do the full validation based on the openapi specification. An example of this case could be the following:
def test_mock_rwc_client(app, default_user, mock_rwc_client):
client = SwaggerClient.from_spec(
rwc_json_spec,
http_client=Mock(), # replacing the actual http client
config={'also_return_response': True})
with pytest.raises(ValidationError):
res = mock_rwc_client.api.create_workflow(
workflow={'specification': {},
# 'type': 'serial', leaving out a required parameter should raise a ValidationError
'name': 'test'},
user=str(default_user.id_)).result()
res = mock_rwc_client.api.create_workflow(
workflow={'specification': {},
'type': 'serial',
'name': 'test'},
user=str(default_user.id_)).result() # the result is a Mock but there was no exception
A second option would be to have a mock server running, using one of the designated openapi.tools, like (Prism)[https://stoplight.io/platform/prism/]. If it is deployed with Travis or our CI tool of choice, it should respond with preset sample responses.
Prism
Deploying it in travis is simple, it can be installed in the before_script
section and launched with the path to the json openapi spec:
# in .travis.yaml
before_script:
- curl https://raw.githubusercontent.com/stoplightio/prism/master/install.sh | sudo sh
- prism run --mock --list --spec /home/travis/build/dinosk/reana-server/reana_server/openapi_connections/reana_workflow_controller.json &
# a test case calling the prism server
def test_prism_server(app, default_user):
rwc_api_client = create_openapi_client('reana-workflow-controller')
with pytest.raises(ValidationError):
res = rwc_api_client.api.create_workflow(
workflow={'specification': {},
# 'type': 'serial',
'name': 'test'},
user=str(default_user.id_)).result()
_, res = rwc_api_client.api.create_workflow(
workflow={'specification': {},
'type': 'serial',
'name': 'test'},
user=str(default_user.id_)).result()
assert res.status_code == 201
Sandbox
https://github.com/getsandbox/sandbox MIT
This would require creating the mock endpoints and their responses in Javascript. The main website takes the openapi specification and spawns a test server available for 1 hour, but it doesn't seem to allow this auto generation from the command line, if we want to deploy it on travis
Microrocks
Installation from binaries not there yet, other installation options are Kubernetes, Openshift, unclear how to use with Travis CI. http://microcks.github.io/using/index.html#installation
Apisprout
No Swagger version 2 support. Doesn't generate sample responses based on schema.