This is the 26th day of my participation in the August Text Challenge.More challenges in August
What is a hook?
A hook function is an Api that is triggered only under certain circumstances.
Take a hook chestnut
Just like when you go to ATM to withdraw money with your bank card, when you withdraw money, a MSG hook function will be triggered, and this MSG hook function will perform the action of sending SMS messages to your mobile phone.
That is, the hook function for sending SMS will be triggered when the cash withdrawal action is successful.
The hook hrun
As with other testing frameworks, Httprunner has its own setup and teardown, which are implemented as hooks.
Hook classification
There are two types of Hooks in Httprunner: Case and Step.
Case level hook
Implement setup and teardown before and after a case.
config:
name: "demo testcase"
variables:
device_sn: "ABC"
username: ${ENV(USERNAME)}
password: ${ENV(PASSWORD)}
base_url: "https://getman.cn/mock"
setup_hooks:
- ${hook_print(setup)}
teardown_hooks:
- ${hook_print(teardown)}
teststeps:
-
name: demo step 1
api: api/demo_api.yml
variables:
user_agent: 'the Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
device_sn: $device_sn
extract:
- token: content.name
validate:
- eq: ["status_code".200]
-
name: demo step 2
api: api/demo_api2.yml
variables:
token: $token
validate:
- eq: [reason.OK]
Copy the code
Debugtalk defines the hook_print function
def hook_print(msg) :
print('This is:',msg)
Copy the code
Test execution console print
Step level hook
Implement setup and teardown before and after step.
config:
name: "demo testcase"
variables:
device_sn: "ABC"
username: ${ENV(USERNAME)}
password: ${ENV(PASSWORD)}
base_url: "https://getman.cn/mock"
teststeps:
-
name: demo step 1
api: api/demo_api.yml
variables:
user_agent: 'the Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
device_sn: $device_sn
extract:
- token: content.name
validate:
- eq: ["status_code".200]
setup_hooks:
- ${hook_print(setup)}
teardown_hooks:
- ${hook_print(teardown)}
-
name: demo step 2
api: api/demo_api2.yml
variables:
token: $token
validate:
- eq: [reason.OK]
Copy the code
Test execution console print
Usage scenarios
Environment login
We can get the token of the environment through hook before the test, so that we can use it directly in the later test.
pretreatment
If we need to carry out some pre-processing actions before the test, such as configuration initialization, test project creation and user creation, we can use Hook to achieve.
post-processing
We can uniformly deal with test resources and garbage data after the test through hook.
With debugTalk, we can implement many complex functions to support our tests, and even get external data or call other systems to achieve richer functions.
Thank you for reading, don’t forget to follow, like, comment, forward four consecutive yo!