A must for automated testing, how to use the interface automated testing framework to build an API testing project, and teach you how to get started

 Table of contents

introduction

GET method: Get the information of a single breed of dog.

POST method: Add the breed information of the new dog.

PUT method: modify the breed information of an existing dog.

DELETE method: delete the breed information of an existing dog.

Python 3.x。

PyCharm or another Python IDE.

Code Shang interface automation testing framework.

Step 1: Install the interface automation testing framework

Step 2: Obtain an API key

Step 3: Set up the test environment

Step 4: Write Test Cases

Step 5: Run the tests

in conclusion


introduction

Efficient interface testing is an important aspect of promoting interface automation. This article introduces the actual method of the interface automation testing framework project, which provides help for interface testing. It should be noted that the actual combat of the test framework project requires not only the ability to understand the details of the interface, but also a theoretical basis for testing.

In this article, we will use Python and an interface automation testing framework to build a simple API automation testing project. We will use a public API for testing, which is a RESTful service that provides pictures of dogs and other dog-related information. We will use various methods provided in the framework to perform requests and assert responses. Our testing will cover the following scenarios:

  1. GET method: Get the information of a single breed of dog.

  2. POST method: Add the breed information of the new dog.

  3. PUT method: modify the breed information of an existing dog.

  4. DELETE method: delete the breed information of an existing dog.

In order to start this project, you will need the following software installed:

  1. Python 3.x。

  2. PyCharm or another Python IDE.

  3. Code Shang interface automation testing framework.

Step 1: Install the interface automation testing framework

The interface automation testing framework can be easily installed via pip. Open a terminal and enter the following command:

pip install requests
pip install flaky
pip install loguru
pip install pytest
pip install pytest-html
pip install pytest-xdist
pip install tqdm
pip install xmltodict
pip install marshmallow
pip install gallow

Step 2: Obtain an API key

We will use Dog API to test our automated test project. This is a very handy RESTful service that provides pictures of dogs and other dog-related information. We will need to obtain the API key provided by Dog API. Log in to https://dog.ceo/dog-api/, click the "Get  Your API Key Now" button. Enter your email address and Dog API will send you an API key.

Step 3: Set up the test environment

We need to setup a test environment. In PyCharm or other Python IDE, create a new Python project called "test_dog_api". Create a new file called "config.py" in the project folder. In this file add the following code, replacing 'YourDogApiKey' with your API key:

import os

class Config:
    BASE_URL = 'https://dog.ceo/api'
    DOG_API_KEY = os.getenv('DOG_API_KEY', 'YourDogApiKey')

Step 4: Write Test Cases

We will write 4 test cases to test the GET, POST, PUT and DELETE methods of the API respectively.

Create a new file called "test_api.py" in the project folder. Add the following code to this file:

from framework import constants as const

class TestDogApi:
    def test_get_random_dog(self, client):
        response = client.get(f'{const.BASE_URL}/breeds/image/random')
        assert response.status_code == 200
        assert response.json().get('status') == 'success'
        assert response.json().get('message') is not None

    def test_create_new_breed(self, client):
        data = {'name': 'BullDog', 'subBreeds': ['French', 'English'], 'location': 'UK'}
        response = client.post(f'{const.BASE_URL}/breeds', data=data)
        assert response.status_code == 200
        assert response.json().get('status') == 'success'
        assert response.json().get('message') is not None

    def test_update_breed(self, client):
        data = {'name': 'BullDog'}
        response = client.put(f'{const.BASE_URL}/breeds', data=data)
        assert response.status_code == 200
        assert response.json().get('status') == 'success'
        assert response.json().get('message') is not None

    def test_delete_breed(self, client):
        response = client.delete(f'{const.BASE_URL}/breeds')
        assert response.status_code == 200
        assert response.json().get('status') == 'success'
        assert response.json().get('message') is not None

In the code, we use the auxiliary methods provided in the framework, such as client.get(), client.post(), client.put() and client.delete(). These methods are very convenient because they automatically set the request header and process Responses and status codes, and parsing JSON responses. We use the assert statement to assert our response. These assertions include specific properties of the HTTP status codes and JSON responses we test.

Step 5: Run the tests

We use PyCharm or other Python IDEs to run tests. We can also run tests using command line tools. From the command line, go into our project folder and run the following command:

python -m pytest -n=2 --html=report.html

This will run the tests in parallel and generate a test report called report.html.

in conclusion

In this article, we use Python and an interface automation testing framework to create a simple API automation testing project. We mainly use the convenience methods provided in the framework to perform requests and assert responses. You can try to add other test scenarios in your test case to get a better understanding of how to use the interface automation testing framework.

Framework structure diagram of automated test learning steps:

The editor also prepared some benefits:

 

 

Guess you like

Origin blog.csdn.net/Free355/article/details/130242019