⬆️ Pay attention to the Hogwarts Testing Institute's official account, reply to the "gift package", and collect the test dry cargo data collection.
Recently, a small practical problem in interface test has attracted the attention of many test students. This paper sorts out the doubts of students and the answers of college teachers for your reference.
Question by @Vantas
During the interface test, I encountered a problem:
How to use the value of a key of the return value of the previous use case as an input parameter of the next use case?
For example, the first use case is login, and each subsequent interface needs to use the token returned by the login interface. How to transfer this token? What if the following interfaces need multiple input parameters (i.e. those that need to be logged in)
token, which also requires the id value of another interface)?
Answer by @Wayyt
It depends on what tool is used. If you use Java/Python code, you can convert the return value into JSON and extract it.
Question by @Vantas
I use Pytest, which assigns the return value to a dictionary of global variables. The following use cases call the dictionary directly. Is this OK?
Return with JSON or dictionary, and then how to pass in the following use cases. I haven't been clear. Is it to call the method name with fixrule, and then write the returned variable name directly in the use case?
Like the third payment use case, which requires a token and "orderids" after the URL. This requires the return value of the first two use cases. How to write it? Two cannot be passed in a method name
fixtrue? What if you need multiple parameters? If there are many use cases and there are many parameter call associations between use cases, how to maintain them better?
Is there an optimal way? The following is sample code:
def setup(): self.t=globals() #land def test_fxLogin(self): url = "http://www.xxx.com/token" data = {"username":"hffx","password":"123456","platformType":"NORMAL"} headers = {"Content-Type": "application/json"} r = requests.post(url, data=json.dumps(data), headers=headers) result = r.text print(result) jsr = json.loads(result) fxtoken = jsr['data'] self.t['fx']=fxtoken assert r.status_code==200 #place an order def test_buyprocedure(self): url = "http://www.xxx.com/" data = {"data"}]} headers = {"Authorization":self.t['fx'],"Content-Type": "application/json"} r=requests.post(url,data=json.dumps(data),headers=headers) result = r.text print(result) jsr=json.loads(result) orderid=jsr[data][id] self.t['orderids']=orderid assert r.status_code == 200 #payment def test_payprocedure(self): url = "http://www.xxx.com/"+self.t['orderids'] data = {"data}]} headers = {"Authorization":self.t['fx'],"Content-Type": "application/json"} r=requests.post(url,data,headers) print(r.text) assert r.status_code==200
Answer by @Wayyt
In fact, this logic is very simple. Don't think of it as complicated. Nothing more than doing three things: getting the token, storing the token and calling the token.
It can be extracted from the return value and stored with a list, a dictionary or even a file. Just read the data structure or file where you put the token.
// You can use setup_class @classmethod def setup_class(cls): cls.pass_dic = {} // Then use self. In the test case pass_ DIC to access the results
Answer by @ Sihan
- Global variables are OK, but not recommended
- Class variables can be CLS XX, multithreading is prone to problems
- Instance variable self XX, this is the best way
The following is an introductory use case without using the Page/API Object mode:
class TestWework: def setup_class(self): r = requests.get( 'https://qyapi.weixin.qq.com/cgi-bin/gettoken', params={ 'corpid': 'xxxxx', 'corpsecret': 'xxxxxxxx' } ) self.token = r.json()['access_token'] def test_tags_list(self): r = requests.post( 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list', params={'access_token': self.token} ) print(json.dumps(r.json(), indent=2, ensure_ascii=False)) assert r.status_code == 200 assert r.json()['errcode'] == 0
Above, welcome to leave a message to discuss. More advanced practice of automated testing,
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information