Package: seekret.apitest
Seekret API testing runtimeTheseekret.apitestpackage contains runtime functions and tools intended to ease API testing.Seekret uses this library in automatically generated tests to help the readability of the generated tests, and implement
common functionalities.QuickstartFirst, installseekret.apitest::pip install seekret.apitestNow, in order to run a test:Store one or more generated tavern tests from theSeekret websitein a directory.Copy the configuration file you received from Seekret to the same directory.Your test directory should look like this:/testdir
|-- api1_test.py
|-- api2_test.py
|-- ...
|-- run-profile.yamlAfter you finish setting up your test directory, simply runpytest.Enable live loggingUse the--log-cli-level INFOoption to show outgoing requests and incoming responses live as they occur during the
test.Run profileEach test run requires a run profile. By default, Seekret reads the run profile from therun-profile.yamlfile in the
root directory, but you can provide a different run profile by specifying the--run-profileflag.The run profile sets:The target server of the testsUsers and authentication configurationstarget_server:https://example.comusers:default:auth:type:bearerdata:token:<API key of the testing account>UsersTheuserskey contains the configuration of the user authentication. The data under theauthblock describes how to
authenticate as the user.The following methods are currently available:header- sends the value in thedatafield as headers.bearer- sends the value in thetokenfield ofdataas a bearer token in theAuthorizationheader.Example of a generated testSeekret generates the test to repeat an observed workflow. Values that have no importance to the workflow will be
randomizedduring test generationaccording to their inferred format.importseekret.apitestdeftest_post_channels_post_messages(seekret:seekret.apitest.Context):# Stage 1: POST /api/channelswithseekret.stage(method='POST',path='/api/channels')asrequest:response=request(json={'name':'<random name>'})assertresponse.status_code==201carry_0_responseBody_data_channel_id=response.search('json.data.channel.id')# Stage 2: POST /api/channels/{channel_id}/messageswithseekret.stage(method='POST',path='/api/channels/{channel_id}/messages')asrequest:response=request(json={'message':'<random message>'},path_params={'channel_id':carry_0_responseBody_data_channel_id})assertresponse.status_code==201In this generated tests, there are two stages:Create a channel:POST /api/channelsSend a message in the channel:POST /api/channels/{channel_id}/messagesSeekret will generate the value transfers between the test stages. For instance, in this case, the ID of the created
channel from the first request, is used as the requiredchannel_idpath parameter of the second request.Seekret will also assert that the response has the observed status.To aid debugging, when a test fails, Seekret will display the data of the requests and responses from the test.Deep DiveThe generated test uses theseekretfixture, which returns an instance of theseekret.apitest.Contextclass. The
test context allows separating test stages with thestagecontext manager, and sending requests to the target server.
The context uses the information from therun profilein order to determine the target server, and the
required authorization data.You can choose the user for a request with by specifying theuserparameter in therequest()call to another user
from therun profile.Setup and teardownYou can add setup and teardown logic to tests
usingpytest fixtures. Theseekretfixture is available for
use in function-scoped fixtures for declaring setup stages and logging.Consider the following example of setup and teardown logic:importpytestimportseekret.apitest@pytest.fixturedefchannel(seekret:seekret.apitest.Context):# Setup stage: happens before the test.withseekret.stage(method='POST',path='/api/channels')asrequest:response=request(json={'name':'Test Channel'})assertresponse.status_code==201channel_id=response.search('json.data.channel.id')# Yielding runs the actual test body.# Important! Yield outside of the stage context. Otherwise the test will start in the context of the setup stage.yieldchannel_id# Teardown stage: happens after the test.withseekret.stage(method='DELETE',path='/api/channels/{channel_id}'):response=request(path_params={'channel_id':channel_id})assertresponse.status_code==200deftest_post_messages_delete_messages(seekret:seekret.apitest.Context,channel):# The 'channel' parameter has the value yielded from the 'channel' fixture.# In this instance, 'channel' will be the ID of the created channel....Default user for testBy default,requestcalls will use thedefaultuser if another user wasn't provided. You can set the default user
using thedefault_userfield of theseekretvalue.Seekret provides thedefault_usertest mark in order to specify a different default user for a specific test.importpytestimportseekret.apitest@pytest.mark.default_user('non-admin')deftest_create_user(seekret:seekret.apitest.Context):withseekret.stage(method='POST',path='/api/users')asrequest:# Sends the request with the authorization data of the 'non-admin' user.response=request(json={'user_name':'<random name>','password':'<random password>'})assertresponse.status_code==403