API Testing With Anna Framework
Tags: testing, python, tooling, api
Published: 2022-04-06 10:14:29

##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k0geqw08rrnega844od.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn9vf2h9vdhgwjnmsbzn.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0089xrbto7x3455jfekc.png)
 
The [anna ](https://github.com/EvgeniiGerasin/anna-api-framework) library was developed for quickly development and report generation. 
![anna label](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6avmpb9h9733hzf9x2gq.png)
 
##Installation

```bush
pip install anna-api-test-framework
```

##How to use
Import the library first:

```python
from anna import Actions, Report, Asserts
```

- **Actions** contains methods for executing http requests. All request and response data is automatically added to the report. 

```python
action = Action(url=url)
response = action.request(method=method)
```
![Request and response data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwe86dpwribibedt55bn.png)

It's very convenient.

- **Report** contains methods for added important information to the report. Like steps, title, epic, links, another data and etc.

```python
@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')
```

- **Assert** contains methods for checking
```python
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'
    )
```
## How to run tests
Use the following command:

```
pytest alluredir="./results"
```
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](https://docs.qameta.io/allure-report/#_installing_a_commandline). 
Use the following command to generate report:
```
allure generate "./results" -c -o "./report"
```
The report will be generated in the `report` directory
## Ho to open report
Use the following command:
```
allure open "./report" 
```
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
```
If you follow the link, the generated report will open
![Report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or7re8zvb75a2xiqsccz.png)
## Conclusion
This library helps you quickly develop test scripts and data for reports. 

[Anna's repository on github](https://github.com/EvgeniiGerasin/anna-api-framework)

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