preamble
Today, I would like to talk to you about python+pytest interface automation test parameter correlation, the author's side will not say more nonsense, let's go directly to the topic.
I. What is parameter correlation?
Parameter association, also called interface association, means that there is a parameter connection or dependency between interfaces. In the completion of a functional business, sometimes you need to request multiple interfaces in sequence, at this time there may be a relationship between some interfaces. For example: a certain request parameter of the B interface is 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 in as a request parameter when requesting the B interface.
II. What are the scenarios?
One of the most common scenarios is:After requesting the login interface to obtain the token value, subsequent requests for other interfaces need to pass the token as a request parameter.
Another example is the order --> payment scenario, call the order interface to generate orders will return the order number, the order number is passed to the payment interface for payment.
III. Parameter correlation scenarios
Take the most common online shopping as an example, the corresponding scene and request we can roughly simplify the following (can be associated with a certain treasure shopping process):
- Users in the shopping cart to select goods click on [go to the checkout] to enter the order confirmation page, order confirmation page click on [submit order] this time the first request for the order interface to create an order
- Immediately after that, it will take the created order and request the Get Payment Credentials interface, which will bring up the payment page, i.e., the payment interface where you enter your password.
- After entering the payment password, the payment interface of the payment service will be requested to make the actual payment, and the result of the payment will be returned to the requesting party, informing whether the payment is successful or not.
The interfaces involved in this process are actually related, and we need to test the entire process so we need to call all these involved interfaces in order.
But here we only need to understand the parameter association, then the order interface and get payment credentials interface as an example, an example is enough, that is, first request the order interface to generate the order number, and then take this order number to request to get payment credentials interface, in order to call up the payment interface and make payments.
The interface for placing orders is as follows:
- Interface address: <server>/trade/order/purchase
- Request type: post
Request Parameters:
{ "goodsId": 10, //commodity id "goodsSkuId": 33, //sku id "num": 2, // Number of purchases "tradePromotion": { //Selected offers "type": 1, //Type<1: Coupon>. "promotionId": 1 //preferential id } }
Returns the value data:
{ "code": 0, "msg": "Success.", "data": { "tradeNo": "0020220116204344962706666" //Trade Order Number }, "t": 1639658625474 }
The Get Payment Credentials interface is as follows:
- Interface Address:<server>/pay/pre/consum
- Request Type:post
Request Parameters:
{ "orderNo":"0020220116204344962706666", //Trade Order Number "product":"alipayWapClient" //Payment Channels<alipayWapClient: Alipay Mobile Web Payment> }
Return Value data.
{ "code": 0, "msg": "Success.", "data": { "payNo":"123213213219379213", "certificate": "<form name=\"punchout_form\" method=\"post\" action=\"/?charset=UTF-8&method=&sign=aTKlfEnYgR7x9xs1Eyjipo0S%2BFtQ6AKu9d%2Brb7iieMThz2Dq7zp4h8QH4lelTKovOloT%2FPiNXR5miwKgOWW3K6pl0TFO5XX5NSZNBmU%2BPd5ogeo0YT0vCqWUM60rqbYLNtZmvyx%2BAR4Z2SOnqs0CYqVIbZAhpn1Bd5HsdcCCYVgsgOdbEE60Cfu3LG3Z%2FQ0GQIdW24uTyr%2BojRc25ipOC9NIYwtba8UjRw18Z3e7sj75qoIg%2FipICH7FCJBJEdlgBGlNxIjKzhYj4OBg93D&return_url=https%3A%2F%%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.&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":"Tested Products","product_code":"QUICK_WAP_WAY"}\">\n<input type=\"submit\" value=\"Pay Now\" style=\"display:none\" >\n</form>\n<script>[0].submit();</script>" }, "t": 1639659171031 }
Where the orderNo field makes these two interfaces correlate. Because the order number is different each time it is generated, when testing this scenario, it is necessary to make the parameters of these two interfaces correlate in order to go through.
IV. Scripting
So how can parameter associations be handled in automated testing with the pytest framework? Two ideas are provided here, theBelow:
- Call the interfaces sequentially in the use case according to the invocation timing of the business scenario
- Write the dependent interface as a fixture function and use yield to return the parameters needed for 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(): ''' Create order - > get payment credentials, bring up payment screen :return. ''' # Call the ordering interface first to generate an order url_order = "/trade/order/purchase" data_order = { "goodsId": 10, "goodsSkuId": 33, "num": 2, "tradePromotion": { "type": 1, "promotionId": 1 }, "tradeDirectionArticle": { "articleId": 1 } } res_order = (url=url_order, json=data_order).text tradeNo = (res_order)["tradeNo"] # Re-request for payment credentials interface url_pay = "/pay/pre/consum" data_pay = { "orderNo": tradeNo, # tradeNo is obtained through the ordering interface "product": "alipayWapClient" } res_pay = (url=url_pay, json=data_pay).text res_pay = (res_pay) # Assertion assert res_pay["code"]==0 assert res_pay["data"]["payNo"] assert res_pay["data"]["certificate"] if __name__ == '__main__': ()
The above code is just a streaming call, we can also encapsulate each interface request into a separate function, in the test case just call these functions in order, which we will explain in a subsequent article.
2, the use of Fixture function
Define the Fixture function with the following code example:
@() def get_order(): ''' Request the order interface to create an order :return. ''' url_order = "/trade/order/purchase" data_order = { "goodsId": 10, "goodsSkuId": 33, "num": 2, "tradePromotion": { "type": 1, "promotionId": 1 }, "tradeDirectionArticle": { "articleId": 1 } } res_order = (url=url_order, json=data_order).text tradeNo = (res_order)["tradeNo"] yield tradeNo
Call the fixture function defined above in the test function with the following code example:
def test_pay(get_order): ''' Order ->Payment Scenario Verification :param get_order: call the Fixture function above, function name get_order is the returned tradeNo :return. ''' url_pay = "/pay/pre/consum" data_pay = { "orderNo": get_order, # get_order is the return value of the fixture function defined above. "product": "alipayWapClient" } res_pay = (url=url_pay, json=data_pay).text res_pay = (res_pay) # Assertion assert res_pay["code"] == 0 assert res_pay["data"]["payNo"] assert res_pay["data"]["certificate"]
V. Summary
Parameter association is an inevitable scenario in interface automation testing, and the design of use cases for associating parameters will have a direct impact on the maintenance of the use cases, which is of course an issue that needs to be considered in the architectural design of the interface automation project.
For those just starting out, what we need to understand is what a parameter association is and what can be done with it
to this article on the python+pytest interface automation parameter correlation is introduced to this article, more related python parameter correlation content please search my previous articles or continue to browse the following related articles I hope you will support me in the future more!