Contents: Guide
-
- foreword
- 1. From entry to mastery of Python programming
- 2. Interface automation project actual combat
- 3. Actual Combat of Web Automation Project
- 4. Actual Combat of App Automation Project
- 5. Resume of first-tier manufacturers
- 6. Test and develop DevOps system
- 7. Commonly used automated testing tools
- Eight, JMeter performance test
- 9. Summary (little surprise at the end)
foreword
Globally set request headers
The pytest + yaml framework encapsulates a built-in fixture called requests_session, and its scope of action is scope="session", that is, all session use cases are only instantiated once in a session.
Now just write a login fixture function in conftest, get the token and add it to the requests_session header
import pytest
import uuid
"""
全局仅登录一次,获取token,
在请求头部添加Authentication Bearer 认证
内置fixture requests_session
"""
def login():
"""登录方法"""
# 调用登录方法,返回token
return str(uuid.uuid4()) # noqa
@pytest.fixture(scope="session", autouse=True)
def login_first(requests_session):
"""全局仅一次登录, 更新session请求头部"""
# 调用登录方法,获得token
token = login()
headers = {
"Authentication": f"Bearer {
token}"
}
requests_session.headers.update(headers)
Then write 2 yaml files (note that there is no need to add the request header repeatedly in the yaml file)
test_get_demo.yml
config:
name: get
teststeps:
-
name: get
request:
method: GET
url: http://httpbin.org/get
validate:
- eq: [status_code, 200]
test_post_demo.yml
config:
name: post示例
variables:
username: test
password: "123456"
teststeps:
-
name: post
request:
method: POST
url: http://httpbin.org/post
json:
username: ${
username}
password: ${
password}
validate:
- eq: [status_code, 200]
Enter pytest to run on the command line, so you can see that the request header parameters are automatically included in the two use cases.
What if other fixed parameters need to be added to the request header?
Such as versionId and configId, the method is the same
headers = {
"Authentication": f"Bearer {
token}",
"versionId": "v1.0",
"configId": "10086"
}
requests_session.headers.update(headers)
When the global requests_session request session is set, all requests will carry the added header parameters by default
headers = {
"Authentication": f"Bearer {
token}",
"versionId": "v1.0",
"configId": "10086"
}
requests_module 和 requests_function
What about some interfaces that do not require login?
For example, the login and registration interfaces do not need to bring a login token.
In addition to using a requests_session global built-in fixture by default, two are reserved
requests_module: use a request session in each yaml file (cookies will be kept)
requests_function: use it once in each use case, each use case runs independently, and does not keep cookies.
Next, let's see how to use test_register.yml in the use case
config:
name: post示例
fixtures: requests_module
注册1:
request:
method: POST
url: http://httpbin.org/post
json:
username: test123
password: "123456"
validate:
- eq: [status_code, 200]
注册2:
request:
method: POST
url: http://httpbin.org/post
json:
username: test444
password: "123456"
validate:
- eq: [status_code, 200]
Pass in the fixtures parameter in config, requests_module is a request session in each yaml file (cookies will be kept)
requests_function is used once in each use case, and each use case runs independently without keeping cookies.
Custom fixtures
The core function of pytest is to learn to use fixtures flexibly, so our plug-in can also support the function of calling fixtures in use cases.
Write the fixture function you need to implement in the conftest.py file, and set the scope of use to scope="function" function level
import pytest
@pytest.fixture(scope="function")
def demo_fixture():
print("用例前置操作->do something .....")
yield
print("用例后置操作,do something .....")
Reference fixtures in yaml files
config:
name: post示例
fixtures: demo_fixture
注册1:
request:
method: POST
url: http://httpbin.org/post
json:
username: test123
password: "123456"
validate:
- eq: [status_code, 200]
注册2:
request:
method: POST
url: http://httpbin.org/post
json:
username: test444
password: "123456"
validate:
- eq: [status_code, 200]
So the running results can be seen, each use case will be executed before and after
collected 2 items
test_f2.yml 用例前置操作->do something .....
.用例后置操作,do something .....
用例前置操作->do something .....
用例后置操作,do something .....
If you want to run it only once in the entire yaml file, then write the fixture function you need to implement in the conftest.py file, and set the usage scope to scope="module" module level
import pytest
@pytest.fixture(scope="module")
def demo_fixture():
print("用例前置操作->do something .....")
yield
print("用例后置操作,do something .....")
So when you see that it is running, it is only executed once in all use cases of the yaml file
collected 2 items
test_f2.yml 用例前置操作->do something .....
..用例后置操作,do something .....
Use of multiple fixtures
When the use case in yaml needs to use multiple fixtures, 2 formats are supported
Format 1: separated by commas
config:
fixtures: fixture_name1, fixture_name2
Format 2: use list
config:
fixtures: [fixture_name1, fixture_name2]
The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled |
1. From entry to mastery of Python programming
2. Interface automation project actual combat
3. Actual Combat of Web Automation Project
4. Actual Combat of App Automation Project
5. Resume of first-tier manufacturers
6. Test and develop DevOps system
7. Commonly used automated testing tools
Eight, JMeter performance test
9. Summary (little surprise at the end)
Stick to your dreams and never give up. Every effort will make you stronger. Difficulties are not scary, face them bravely. The power of struggle will light the way forward. Confidence and courage are the guarantee of victory. Struggle with passion and create brilliance.
Effort is the best investment, and it will pay off. Behind success is continuous hard work and persistence. Not afraid of failure, dare to challenge. Believe in yourself and sprint forward, only by persisting to the end can you see a brilliant tomorrow.
Break through the thorns and thorns, climb the peak. No difficulty will trouble you, as long as you have a dream and persevere. The sweat of struggle will create a brilliant life. Believe in yourself and surpass yourself. The scenery ahead belongs to you who are struggling.