Support Center
steps.py
that is inside your apitest project.@given('I save the exchange rate of EUR to "{currency}" under "{name}"')
@obtain_values
def i_save_the_euro_exchange_rate(ctx:'Context', currency:'str', name:'str') -> 'None':
# The endpoint that gives us exchange rates ..
address = 'https://api.frankfurter.app/latest'
# .. invoke it ..
response = requests.get(address)
# .. extract the JSON response ..
data = response.json()
# .. extract rates ..
rates = data['rates']
# .. get the rate for the input currency ..
rate = rates[currency]
# .. and store it for later use.
ctx.zato.user_ctx[name] = rate
This is a regular Python function whose main purpose, in lines 5-20, is to connect to an API endpoint, get a JSON response and extract some data from it.
This principle will be almost always true, the main part of a custom step won't be about apitest itself but about a piece of functionality, perhaps a part of a business process, that you want to wrap in a test step.
Let's discuss the apitest-specific, highlighted lines then.
@given
decorator with a string pattern of your choice. Parameters that are variables, such currency and name, are always in quotation marks ""
and between curly brackets {}
.@obtain_values
decorator is always needed for the test to be able to extract values from its environment and contextctx.zato.user_ctx
dictionary. Anything you assign to it will be later available in other test steps.Taken together, this lets us have this:
Given I save the exchange rate of EUR to "USD" under "exchange_rate"
Given request is "{}"
Given path "/rate" in request is "#exchange_rate"
You can now observe how the value of the exchange rate was saved in the "exchange_rate" test variable and how it can be used by a different step by prepending the pound sign #
to it.
A couple more points:
All the built-in steps in apitest are implemented in the same manner. It means that you can open their source code in your apitest installation, check how they work and base your own steps on them.
Don't delete anything from the "steps.py" file. Even if you add your own steps, don't delete the code that you can find there by default.