How to create a simple API Test¶
API-Tests are usually something, that the classic business people wouldn’t know about. With
baangt and a bit
of effort it should be possible even for business people to do some simple API-Tests.
As with all tests in
baangt also API-Tests are defined in a simple format in MS Excel. See
as working example.
Prerequisit for simple API definition format:
- Filename must contain the word “api”, otherwise simple format will try to create a browser test run.
Steps to test the simple API Format:¶
baangt, chose file
MovieSimpleApi.XLSX as your run definition. Start the testrun by clicking on the
Execute. After a few seconds you should see the popup “Testrun finished”. Now open the result file
baangt_MovieSimpleAPI_<date>.xlsx and see overview and details of the API Test run.
To extend this very simple example you could want to add the field “Actors” to your result sheet. To do so, add one line
in the Tab
Save the Excel-Sheet. Re-run the test case and you should see the new column “Result_Actors” with the values retrieved from the API.
Activities for API-Tests:¶
(Even though we write all activtities in UPPER CASE, you can write them in any way you like)
||Set’s the main URI/URL for your API-Tests. Could be omitted, if you want to always specify full path in ENDPOINT.|
||Set’s the Endpoint-Name for the following API-Call. E.g. if your Endpoint is located at
https://app-eu.earthsquad.global/api/rest-auth/login and during this test case execution you’d call a lot of APIs
on this server, then you’d set
||Send a “POST”-Request to the API. Place the content, that you want to send to this endpoint in the column
||Send a “GET”-Request to the API. URL-Parameters are taken from
||Set additional parameters for the next API-Calls into the Header. In combination with the special fields (see below) it’s easy to take a result from one API-Request and use it (or parts of it) as input for the next call.|
||Save a value from the header or from result to output file (XLSX).
This will retrieve value of element specified by locator And compare with expected_value specified in value
if expected_value not matches with output_value it will raise TestStepExecution and result in FAILED.
Create Address Data for various test cases and save in testDataDict The following field variable can be used via $(field_name).
[‘HouseNumber’, ‘AdditionalData1’, ‘AdditionalData2’, ‘StreetName’, ‘CityName’, ‘PostalCode’, ‘CountryCode’]
Special data fields in API-Tests:¶
In WEB-Testing you check results either via
Assert-Statement or via mapping the text or attribute of an element to a
field in the TestDataDictionary. In API-Tests you have some automatic internal variables, that you can use without
manually declaring them:
|RESULT_CODE||Result code of the last call to an API. Ideally you’d be able to match result codes as described in here https://restfulapi.net/http-status-codes/, but in the end setting the status code is the job of the developer of the API you’re using - they might follow a different path or simply have bugs.|
|ANSWER_HEADER||Last Header. You can access a certain part of the header by using $(ANSWER_HEADER.<partName>), so if you want to
use the part
|ANSWER_CONTENT||Last content of an API-Call (Post, Get, etc.). Again you can access/extract/replace parts of this content using
the “.” like described in the line above (e.g.
Sometimes we need random values like string, name, integer, float, date, time now in such case we have
functionality. It is used inside value column of and its structure is
type field is compulsory and
every other fields are optional, also each fields are not useful in every type, e.g.-
name type doesn’t need any
other optional fields as they are use less for it. You can see fields and types supporting them.
|type||This field is compulsory and base of
|min||string, int, float, date, time are the types supporting this field. Value of min will be with respect to its type like value for string will be an integer containing minimum number of characters in string and for all other it will be lower limit, for int it will be an integer & float for float, for date value will be a date e.g. - “31/01/2020” and for time it would look like “20:30:59”|
|max||string, int, float, date, time are the types supporting this field. Value of max will be same like in min, value for string will be an integer containing maximum number of characters in string and for all other it will be upper limit, for int it will be an integer & float for float, for date value will be a date e.g. - “01/06/2020” and for time it would look like “13:10:30”|
date, time are the only types supporting format field. In above date examples date is in %d/%m/%Y format and time is in %H:%M:%S format. Here “%d” stands for the day, “%m” stands for month, “%Y” stands for year including century e.g.- 2020, if you want only year you can use “%y” e.g. 20. If you use min and max fields in date, time then you must input its written format in format field, default will be “”%d/%m/%Y” for date. Now if you want date with “-” as seperator you can write format as “%d-%m-%Y” so the output would be like “31-01-2020”.