The most detailed in the entire network, Python interface automated test parameter correlation (application scenario example)


Preface

What is parameter association?

Parameter association, also called interface association, means that there is parameter connection or dependence between interfaces. When completing a certain functional business, it is sometimes necessary to request multiple interfaces in sequence. In this case, there may be an association between some interfaces.

For example: one or some request parameters of interface B are obtained by calling interface A, that is, you need to request interface A first, get the required field values ​​from the return data of interface A, and pass them as request parameters when requesting interface B. enter.

What are the scenarios for parameter association?

The most common scenario: After requesting the login interface, the token value is obtained. When making subsequent requests to other interfaces, the token needs to be passed in as a request parameter.

Another example is the order->payment scenario. After calling the order interface to generate an order, the order number will be returned, and the order number will be passed to the payment interface for payment.

1. Parameter association scenario

Taking the most common online shopping as an example, we can roughly simplify the corresponding scenarios and requests as follows (you can think of the shopping process of a certain product):

The user selects the product in the shopping cart and clicks [Go to Checkout] to enter the order confirmation page. On the order confirmation page, click [Submit Order]. This will first request the order interface to create an order.

Then the created order will be used to request the payment voucher interface. This interface will call up the payment page, which is the payment interface for entering the password.

After entering the payment password, the payment interface of the payment service will be requested for actual payment. The payment result will be returned to the requesting party to inform whether the payment was successful.

The interfaces involved in this process are actually related. If we want to test the entire process, we need to call all the interfaces involved in order.

But here we only need to understand the parameter association, so taking the ordering interface and the payment voucher obtaining interface as an example is enough. That is, first request the ordering interface to generate an order number, and then use this order number to request the payment voucher interface. Only then can you bring up the payment interface and make the payment.

The ordering interface is as follows:

Interface address: <server>/trade/order/purchase
Request type: post
Request parameters:

{
    
    
	"goodsId": 10,  //商品id
	"goodsSkuId": 33,   //sku id
	"num": 2,   //购买数量
	"tradePromotion": {
    
     //选择的优惠项
		"type": 1,  //类型<1:优惠券>
		"promotionId": 1    //优惠id
	}
}

Return value data:

{
    
    
    "code": 0,
    "msg": "成功",
    "data": {
    
    
        "tradeNo": "0020220116204344962706666"  //交易订单号
    },
    "t": 1639658625474
}

The interface for obtaining payment vouchers is as follows:

Interface address: <server>/pay/pre/consum
Request type: post
Request parameters:

{
    
    
	"orderNo":"0020220116204344962706666",    //交易订单号
	"product":"alipayWapClient"    //支付渠道<alipayWapClient:支付宝手机网页支付>
}

Return value data:

{
    
    
    "code": 0,
    "msg": "成功",
    "data": {
    
    
        "payNo":"123213213219379213",
        "certificate": "<form name=\"punchout_form\xxxxxxx >\n</form>\n<script>document.forms[0].submit();</script>"
    },
    "t": 1639659171031
}

The orderNo field associates the two interfaces. Because the order number generated is different every time, when testing this scenario, you need to associate the parameters of the two interfaces to make it work.

2. Script writing

So how can parameter association be handled in the automated testing of the pytest framework? Two ideas are provided here, as follows:

According to the calling sequence of the business scenario, call the interfaces in order in the use case.
Write the dependent interfaces as fixture functions, and use yield to return the parameters required by the next interface.

1) Call in order in the use case.
The code example is as follows:

import requests
import json
import pytest

def test_order_pay():
    '''
    创建订单->获取支付凭证,调起支付界面
    :return:
    '''
    # 先调用下单接口生成订单
    url_order = "https://gouwu.com/trade/order/purchase"
    data_order = {
    
    
        "goodsId": 10,
        "goodsSkuId": 33,
        "num": 2,
        "tradePromotion": {
    
    
            "type": 1,
            "promotionId": 1
        },
        "tradeDirectionArticle": {
    
    
            "articleId": 1
        }
    }
    res_order = requests.post(url=url_order, json=data_order).text
    tradeNo = json.loads(res_order)["tradeNo"]

    # 再请求获取支付凭证接口
    url_pay = "https://gouwu.com/pay/pre/consum"
    data_pay = {
    
    
        "orderNo": tradeNo, # tradeNo通过下单接口获取
        "product": "alipayWapClient"
    }
    res_pay = requests.post(url=url_pay, json=data_pay).text
    res_pay = json.loads(res_pay)
    # 断言
    assert res_pay["code"]==0
    assert res_pay["data"]["payNo"]
    assert res_pay["data"]["certificate"]
    
    
if __name__ == '__main__':
    pytest.main()

The above code is just a streamlined call. We can also encapsulate each interface request into a separate function. In the test case, we only need to call these functions in order. We will explain this in a subsequent article.

2) To use the Fixture function
in pytest, please refer to my previous article pytest-Fixture (firmware)

Define the Fixture function. The code example is as follows:

@pytest.fixture()
def get_order():
    '''
    请求下单接口,创建订单
    :return:
    '''
    url_order = "https://gouwu.com/trade/order/purchase"
    data_order = {
    
    
        "goodsId": 10,
        "goodsSkuId": 33,
        "num": 2,
        "tradePromotion": {
    
    
            "type": 1,
            "promotionId": 1
        },
        "tradeDirectionArticle": {
    
    
            "articleId": 1
        }
    }
    res_order = requests.post(url=url_order, json=data_order).text
    tradeNo = json.loads(res_order)["tradeNo"]
    yield tradeNo

Call the fixture function defined above in the test function. The code example is as follows:

def test_pay(get_order):
    '''
    下单->支付场景校验
    :param get_order: 调用上面的Fixture函数,函数名get_order即返回的tradeNo
    :return:
    '''
    url_pay = "https://gouwu.com/pay/pre/consum"
    data_pay = {
    
    
        "orderNo": get_order,  # get_order即为上面定义的fixture函数返回值
        "product": "alipayWapClient"
    }
    res_pay = requests.post(url=url_pay, json=data_pay).text
    res_pay = json.loads(res_pay)
    # 断言
    assert res_pay["code"] == 0
    assert res_pay["data"]["payNo"]
    assert res_pay["data"]["certificate"]

The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Time flies and opportunities are fleeting. Seize every precious moment and water the flowers of your dreams with your sweat. Keep struggling and pursue excellence. Only by working hard can you start a glorious journey in life! Believe in yourself and move forward!

Although life is full of challenges and uncertainties, firm belief and unremitting efforts will win success. Believe in yourself, move forward bravely, and pursue the infinite possibilities that belong to you!

Struggle is the melody of life, and persistence is the password of success. No matter how many difficulties you encounter, keep the flame of your dream burning and move forward courageously, and you will create a glorious chapter of your own! Believe in yourself and you can do anything!

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/133383575