A, Hook introduced background
If the interface requires special treatment before and after the test, as the waiting operation after the initialization operation before execution, execution is completed. setUp (similar in unittest) and tearDown () method, Hook function HttpRunner such operations are also supported.
setup_hooks: hook function performed prior to sending the HTTP request, mainly for the preparation; may also be implemented on request of the content request preprocess
teardown_hooks: HTTP request is executed after the hook function, mainly for cleaning post-test; may also be achieved in response to the response be modified, for example, processing such as encryption and decryption
Second, practice cases
Case Description: After setting interface request If the response status code 200, waits 100ms; otherwise, wait time is set according to customize
Test Interface: http: //httpbin.org/get
Request Type: GET
1. Edit debugtalk.py code, create an auxiliary function sleep ()
debugtalk.py Import hashlib Import Time # method of obtaining sign the definition, transmission user name and password parameters DEF getSign (User, the passwd): STR = + the passwd User # string concatenation username + password MD5 = hashlib.md5 () # call md5 hashlib module () method md5.update (str.encode (encoding = ' utf-8 ' )) # of str, utf-8 format encoding, then md5 encrypted Sign md5.hexdigest = () # md5 encryption, and hexadecimal string stored in the sign of the variable return sign # accordance with a given [username + password] md5 generated encrypted digest information, sign and return the results Print (getSign ( ' ADMIN ' , '123456 ' )) DEF SLEEP (Response, t): IF response.status_code == 200 is : the time.sleep ( 0.1 ) the else : the time.sleep (t) # The value of t, the delay time set
(PS: getSign () function md5 encryption method used above herein can temporarily calling the function)
2. Create use cases, references helper
New use cases test_get_request_hook, since at this time the non-200 status code defines 2s response time delay, which is provided in the auxiliary reference function $ {sleep ($ response, 2)}
hooks referenced helper:
Operation: variables / parameters -> add hooks -> teardown_hooks: Fill cited Helper
(Status_code) is provided in response to the assertion of the status code:
Operation: extract / validate -> add validate -> validate: field setting response to assertion checking, logic, character type, the expected value
The implementation of use cases:
State 200, the response time delay 0.1s
A non-responsive state structure 200, such as 404 state. Modify the interface address is: http: //httpbin.org/getaaa
State 404, the response time delay 2s