DEV Community

Evgenii Gerasin
Evgenii Gerasin

Posted on

API Testing With Anna Framework

Introduction

Hi everyone. I work as QA engineer in an IT company. I am a team leader of autotesters. We automatize UI and API tests. We run thousands of test scripts every day. Our scripts are written on python.

Python label

Pythons standard libraries are very good, but developing and maintaining scripts take a lot of time. We need to quickly develop, launch and provide test result to the customer.

In the work of a tester, it is very important to provide a report with test result to the customer. We use Allure for this.

Allure label

To run the test scripts, we use CI/CD (Jenkins). After passing the test scripts, a report with the test results is automatically generated.

It looks like this:
Example of report

The anna library was developed for quickly development and report generation.
anna label

Installation

pip install anna-api-test-framework
Enter fullscreen mode Exit fullscreen mode

How to use

Import the library first:

from anna import Actions, Report, Asserts
Enter fullscreen mode Exit fullscreen mode
  • Actions contains methods for executing http requests. All request and response data is automatically added to the report.
action = Action(url=url)
response = action.request(method=method)
Enter fullscreen mode Exit fullscreen mode

Request and response data

It's very convenient.

  • Report contains methods for added important information to the report. Like steps, title, epic, links, another data and etc.
@Report.epic('Simple tests')
@Report.story('Tests google')
@Report.testcase('https://www.google.com', 'Google')
@Report.link('https://www.google.com', 'Just another link')
class TestExample:

    @Report.title('Simple test google')
    @Report.severity('CRITICAL')
    def test_simple_request(self):
        url = 'https://google.com'
        method = 'GET'
        want = 200 
        Report.description(url=url, method=method, other='other information')
Enter fullscreen mode Exit fullscreen mode
  • Assert contains methods for checking
with Report.step('Checking response'):
    Assert.compare(
        variable_first=want,
        comparison_sign='==',
        variable_second=got,
        text_error='Response status code is not equal to expected'
    )
Enter fullscreen mode Exit fullscreen mode

How to run tests

Use the following command:

pytest alluredir="./results"
Enter fullscreen mode Exit fullscreen mode

This command runs tests from the current directory and saves the test results to the results directory

How to generate report

To do this, you need an installed Allure.
Use the following command to generate report:

allure generate "./results" -c -o "./report"
Enter fullscreen mode Exit fullscreen mode

The report will be generated in the report directory

Ho to open report

Use the following command:

allure open "./report" 
Enter fullscreen mode Exit fullscreen mode

You can see the following output:

Starting web server...
2022-04-06 12:58:39.896:INFO::main: Logging initialized @1655ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://172.31.22.186:61080/>. Press <Ctrl+C> to exit
Enter fullscreen mode Exit fullscreen mode

If you follow the link, the generated report will open
Report

Conclusion

This library helps you quickly develop test scripts and data for reports.

Anna's repository on github

Thank you for your attention!👍👍👍
**this is my first article, do not judge strictly

Top comments (0)