The design concept of TEP is that everyone can use Python to write automation. This article will introduce how to use TEP to complete the automation of adding, deleting, modifying and checking interfaces.

The environment variable

Edit the fixtures/fixture_admin. Py:

"qa": {
    "domain": "https://qa.com",},Copy the code

Modify the domain of the QA environment.

The login

Fixtures /fixture_admin.py because the non-logging interface needs to take the token from the logging interface and place it in the request parameter, we implemented the logging first in fixtures/fixture_admin.py, modifying the URL and json:

response = request(
    "post",
    url=url("/api/users/login"),
    headers={"Content-Type": "application/json"},
    json={
        "username": "admin"."password": "123456",})Copy the code

Alter response_token assignment based on the actual response data structure:

response_token = jmespath.search("token", response.json())

class Clazz:
        token = response_token
        jwt_headers = _jwt_headers(response_token)

return Clazz
Copy the code

See _jwt_headers() to customize headers:

def _jwt_headers(token) :
    return {"Content-Type": "application/json"."authorization": f"Bearer {token}"}
Copy the code

_ indicates internal functions that cannot be accessed externally. Conftest. py only provides fixtures for external functions.

The complete code

def _jwt_headers(token) :
    return {"Content-Type": "application/json"."authorization": f"Bearer {token}"}


@pytest.fixture(scope="session")
def login() :
    # Code your login
    logger.info("Administrator login")
    response = request(
        "post",
        url=url("/api/users/login"),
        headers={"Content-Type": "application/json"},
        json={
            "username": "admin"."password": "123456",})assert response.status_code < 400
    response_token = jmespath.search("token", response.json())

    class Clazz:
        token = response_token
        jwt_headers = _jwt_headers(response_token)

    return Clazz
Copy the code

Description:

  1. You can copyloginFor multiple fixtures, such aslogin_admin,login_some_user, flexible use.
  2. scope="session"Is used to log in only once. All test cases use the same login modetoken. Can be changed tofunctionMake each use case use differentlytoken.

Write use cases

The new test

New tests \ crud_test py:

from loguru import logger
from tep.client import request


def test(faker_ch, login, url) :
Copy the code

Pytest’s fixtures are used as arguments to the test() function. Faker_ch, login, and URL are fixtures defined in fixture_admin.py.

  • from loguru import loggerUsed to print logs in test cases
  • from tep.client import requestTep encapsulates the request logging function and can also be used nativefrom requests import request

new

Call parameters are nickname and phone, and use faker_ch to create 1 test data:

fake = faker_ch
nickname = fake.name()
phone = fake.phone_number()
Copy the code

The request method is POST, headers takes the return value login.jwt_headers:

response = request(
    "post",
    url=url("/api/users"),
    headers=login.jwt_headers,
    json={
        "nickname": nickname, "phone": phone
    }
)
Copy the code

Add an assertion to simply judge the response status code <400:

assert response.status_code < 400
Copy the code

You can also look it up in the database.

Extract the data needed to modify the interface:

user_id = jmespath.search("id", response.json())
created_at = jmespath.search("createdAt", response.json())
updated_at = jmespath.search("updatedAt", response.json())
Copy the code

Jmespath is recommended to extract JSON.

The query

The request parameter is passed in the local variable nickname, using get and asserting that headers takes the login return value login.jwt_headers:

response = request(
    "get",
    url=url("/api/users"),
    headers=login.jwt_headers,
    params={
        "page": 1."perPage": 10."keyword": nickname
    }
)
assert response.status_code < 400
Copy the code

The GET request requires the JSON keyword to be changed to params.

Modify the

Create 1 new data using Faker:

nickname_new = fake.name()
phone_new = fake.phone_number()
Copy the code

The request method is PUT, headers The return value of login. Jwt_headers:

response = request(
        "put",
        url=url(f"/api/users/{user_id}"),
        headers=login.jwt_headers,
        json={
            "id": user_id, "createdAt": created_at, "updatedAt": updated_at,
            "phone": phone_new, "nickname": nickname_new
        }
    )
assert response.status_code < 400
Copy the code

The request parameters use the data user_id, creATED_AT, and updated_AT extracted by the new interface.

delete

The request method is delete, the url is user_id, headers is login. Jwt_headers:

response = request(
    "delete",
    url=url(f"/api/users/{user_id}"),
    headers=login.jwt_headers
)
assert response.status_code < 400
Copy the code

Delete interface without JSON and Params.

Complete the use case


"" @author: Don @date: 12/25/2020 1:02pm @desc: Add, delete, modify, update ""
import jmespath
from loguru import logger
from tep.client import request


def test(faker_ch, login, url) :
    fake = faker_ch
    logger.info("New")
    nickname = fake.name()
    phone = fake.phone_number()
    response = request(
        "post",
        url=url("/api/users"),
        headers=login.jwt_headers,
        json={
            "nickname": nickname, "phone": phone
        }
    )
    assert response.status_code < 400
    user_id = jmespath.search("id", response.json())
    created_at = jmespath.search("createdAt", response.json())
    updated_at = jmespath.search("updatedAt", response.json())

    logger.info("Query")
    response = request(
        "get",
        url=url("/api/users"),
        headers=login.jwt_headers,
        params={
            "page": 1."perPage": 10."keyword": nickname
        }
    )
    assert response.status_code < 400

    logger.info("Change")
    nickname_new = fake.name()
    phone_new = fake.phone_number()
    response = request(
        "put",
        url=url(f"/api/users/{user_id}"),
        headers=login.jwt_headers,
        json={
            "id": user_id, "createdAt": created_at, "updatedAt": updated_at,
            "phone": phone_new, "nickname": nickname_new
        }
    )
    assert response.status_code < 400
    logger.info(F "user name mobile phone{nickname} {phone}The modified{nickname_new} {phone_new}")

    logger.info("Delete")
    response = request(
        "delete",
        url=url(f"/api/users/{user_id}"),
        headers=login.jwt_headers
    )
    assert response.status_code < 400

Copy the code

summary

This article introduces the basic use of TEP. First configure environment variables, then modify the login code, then add tests, and finally write the interface request of add, delete, change and check. Each use case is placed in a function that uses global environment variables through function arguments referencing fixtures, and you can define the local variables you need to test within the function. Each interface is a code block consisting of five parts: interface description, test data, request, assertion, and data extraction. Interfaces are parameterized and associated by variables.