Preface
One 、 What is parameter association ?
Two 、 What are the scenes ?
3、 ... and 、 Parameter association scene
Four 、 scripting
1、 Call... In order in the use case
2、 Use Fixture function
5、 ... and 、 summary
PrefaceWhat about today , I want to talk with you python+pytest Parameter correlation of interface automation test , I won't say much nonsense here , Let's go straight to the point .
One 、 What is parameter association ?Parameter association , Also called Interface Association , That is, there are parameter connections or dependencies between interfaces . When completing a functional business , Sometimes you need to request multiple interfaces in order , At this time, there may be an association relationship between some interfaces . such as :B One or some request parameters of the interface are through the call A Interface obtained , That is, you need to request A Interface , from A Get the required field value from the returned data of the interface , In the request B Interface is passed in as a request parameter .
Two 、 What are the scenes ?One of the most common scenarios is : After requesting to log in to the interface, get token value , In subsequent requests for other interfaces, you need to token Passed in as a request parameter .
Another example is placing an order --> Payment scenario , After calling the order interface to generate an order, the order number will be returned , The order number is transferred to the payment interface for payment .
3、 ... and 、 Parameter association sceneTake the most common online shopping as an example , The corresponding scenarios and requests can be roughly simplified as follows ( Can be associated with a treasure shopping process ):
The user clicks on the shopping cart to select the item 【 To settle accounts 】 Enter the order confirmation page , Click... On the order confirmation page 【 place order 】 At this time, you will first request the order interface to create an order
Then, it will take the created order to request the payment voucher interface , This interface will call up the payment page , That is, the payment interface for entering password
After entering the payment password, the payment interface of the payment service will be requested to make the actual payment , The result of the payment will be returned to the requestor , Inform whether the payment is successful
The interfaces involved in this process are actually related , If we want to test the whole process, we need to call all these involved interfaces in order .
But here we only need to understand the parameter correlation , Take the following single interface and the interface for obtaining payment vouchers as examples , Examples are enough , That is, first request the order interface to generate the order number , Then take this order number to request the payment voucher interface , To activate the payment interface and make payment .
The order interface is as follows :
Address of the interface :< The server >/trade/order/purchase
Request type :post
Request parameters :
{ "goodsId": 10, // goods id "goodsSkuId": 33, //sku id "num": 2, // Purchase quantity "tradePromotion": { // Selected offers "type": 1, // type <1: Coupon > "promotionId": 1 // Discount id }}
Return value data:
{ "code": 0, "msg": " success ", "data": { "tradeNo": "0020220116204344962706666" // Transaction order number }, "t": 1639658625474}
The interface for obtaining payment vouchers is as follows :
Address of the interface :< The server >/pay/pre/consum
Request type :post
Request parameters :
{ "orderNo":"0020220116204344962706666", // Transaction order number "product":"alipayWapClient" // Payment channel <alipayWapClient: Alipay mobile phone web payment >}
Return value data:
{ "code": 0, "msg": " success ", "data": { "payNo":"123213213219379213", "certificate": "<form name=\"punchout_form\" method=\"post\" action=\"https://openapi.alipay.com/gateway.do?charset=UTF-8&method=alipay.trade.wap.pay&sign=aTKlfEnYgR7x9xs1Eyjipo0S%2BFtQ6AKu9d%2Brb7iieMThz2Dq7zp4h8QH4lelTKovOloT%2FPiNXR5miwKgOWW3K6pl0TFO5XX5NSZNBmU%2BPd5ogeo0YT0vCqWUM60rqbYLNtZmvyx%2BAR4Z2SOnqs0CYqVIbZAhpn1Bd5HsdcCCYVgsgOdbEE60Cfu3LG3Z%2FQ0GQIdW24uTyr%2BojRc25ipOC9NIYwtba8UjRw18Z3e7sj75qoIg%2FipICH7FCJBJEdlgBGlNxIjKzhYj4OBg93D&return_url=https%3A%2F%2Fblog.csdn.net%2Fa032788aotify_url=http%3A%2F%2F82.157.145.132%3A8089%2Ftest%2Fnotify%2Fgateway&version=1.0&app_id=2021001105644746&sign_type=RSA2×tamp=2021-150&alipay_sdk=alipay-sdk-java-4.9.5.ALL&format=json\">\n<input type=\"hidden\" name=\"biz_content\" value=\"{"time_expire":"2022-12-31 22:00:00","out_trade_no":"123213213219379213","total_amount":0.01,"subject":" Test product ","product_code":"QUICK_WAP_WAY"}\">\n<input type=\"submit\" value=\" Pay now \" style=\"display:none\" >\n</form>\n<script>document.forms[0].submit();</script>" }, "t": 1639659171031}
among orderNo Field associates the two interfaces . Because the order number generated each time is different , So when testing this scenario , You need to associate the parameters of the two interfaces , To get through .
Four 、 scriptingSo in pytest Automated testing of the framework , How can parameter association be handled ? Here are two ideas , as follows :
Call timing according to business scenario , Call interfaces sequentially in use cases
Write the dependent interface as fixture function , And use yield Return the parameters required by the next interface
1、 Call... In order in the use caseThe code example is as follows :
import requestsimport jsonimport pytestdef test_order_pay(): ''' Create order -> Get payment voucher , Call up the payment interface :return: ''' # First call the order interface to generate an order 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"] # Re request to obtain payment voucher interface url_pay = "https://gouwu.com/pay/pre/consum" data_pay = { "orderNo": tradeNo, # tradeNo Obtain through the order interface "product": "alipayWapClient" } res_pay = requests.post(url=url_pay, json=data_pay).text res_pay = json.loads(res_pay) # Assertion 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 pipeline call , We can also encapsulate each interface request into a separate function first , In the test case, you only need to call these functions in order , This will be explained in the following articles .
2、 Use Fixture functionDefinition Fixture function , The code example is as follows :
@pytest.fixture()def get_order(): ''' Request order interface , Create 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
The middle note is defined in the test function. fixture function , The code example is as follows :
def test_pay(get_order): ''' Place an order -> Payment scenario verification :param get_order: Call the above Fixture function , Function name get_order That is, the returned tradeNo :return: ''' url_pay = "https://gouwu.com/pay/pre/consum" data_pay = { "orderNo": get_order, # get_order Is defined above fixture Function return value "product": "alipayWapClient" } res_pay = requests.post(url=url_pay, json=data_pay).text res_pay = json.loads(res_pay) # Assertion assert res_pay["code"] == 0 assert res_pay["data"]["payNo"] assert res_pay["data"]["certificate"]
5、 ... and 、 summary Parameter correlation is an inevitable scenario in interface automation testing , The use of design related parameters will directly affect the maintenance of use cases , Of course, this is also a problem that needs to be considered in the architecture design of interface automation project .
For new students , What we need to understand is , What is parameter association , And how to deal with it
This is about python+pytest This is the end of the article on interface automation parameter correlation , More about python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !