The strongest in the whole network, Python interface automation test actual combat - interface parameter association (shopping example)


foreword

What is parameter association?

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

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

What are the scenarios?

One of the most common scenarios is: the token value is obtained after requesting to log in to the interface, and the token needs to be passed in as a request parameter in subsequent requests to other interfaces.

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.

Parameter association scene

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

The user selects the product in the shopping cart and clicks [Go to Settlement] to enter the order confirmation page, and clicks [Submit Order] on the order confirmation page. At this time, the user will first request the order interface to create an order

Next, the created order will be used to request the payment voucher interface, which 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 to make the actual payment, and the payment result will be returned to the requesting party, informing whether the payment is successful

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

But here we only need to understand the parameter association, so take the order interface and the payment voucher interface as examples, and examples are enough, that is, first request the order interface to generate an order number, and then use the order number to request the payment voucher interface. Only then can the payment interface be called up and the payment can be made.

The interface for placing an order 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 the payment certificate 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\" method=\"post\" action=\"xxxxxx"
    },
    "t": 1639659171031
}

Among them, the orderNo field associates these two interfaces. Because the order numbers generated each time are different, when testing this scenario, it is necessary to associate the parameters of these two interfaces in order to get through.

script writing

So in the automated testing of the pytest framework, how can parameter association be handled?

Two ideas are provided here, as follows:
According to the calling sequence of the business scenario, call the interface in order in the use case
Write the dependent interface as a fixture function, 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 only called in a pipelined manner. We can also encapsulate each interface request into a separate function first, and only need to call these functions in order in the test case.

2. Use the Fixture function
The Fixture function in pytest can refer to my previous article pytest-Fixture (firmware)
to 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"]

Parameter association is an inevitable scenario in interface automation testing. Designing use cases for associating parameters will directly affect the maintenance of use cases. Of course, this is also a problem that needs to be considered when designing the architecture of interface automation projects.

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Free your mind, pursue your dreams, and struggle is the compass that illuminates your progress. Bravely venture out, keep exploring, and only by working hard can you write your own legend. Believe in yourself, persevere, and you will eventually create your own brilliant life! Forge ahead with passion, do your best, and bloom a brilliant life in the struggle!

Bravely climb the peak, surpass yourself, and struggle is the power of transformation. Stick to the original intention and chase the dream, only hard work can create extraordinary. Believe in yourself, forge ahead, and eventually write your own glorious chapter!

Believe in your heart, ignite the flame of success, and struggle is the journey of self-achievement. Overcoming difficulties and creating the future, only hard work can bloom our own brilliance. Believe in yourself, persevere, and move towards your own glorious peak! Go forward bravely, overcome difficulties, and create a splendid chapter in life in the struggle!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/132104472