当前位置:网站首页>pytest-fixture
pytest-fixture
2022-07-01 03:06:00 【Xiaojing, who likes dumplings】
fixture The role of
fixture The function of is to put some non core test logic ( Such as the retrieval and generation of test data ) Separate from the test function , To facilitate the reuse of other test functions , While maintaining the consistency of these edge logics
fixture The code in can be customized , Meet the changing needs of testing , This includes defining the data set in the incoming test , Configure the initial state of the system before testing , Provide data for batch test, etc
fixture The definition of
How to define a fixture?
pytest Use decorators @pytest.fixture(scope,params,autouse,ids,name) To declare that this function is a fixture
scope:
Optional parameters , Specify the scope of action , Parameters have four optional values :function,class,module,sesson, The default is function
When scope by function when , Each test function only needs to be run once
When it comes to class when , Each test class only needs to be run once
by module when , Each module only needs to be run once
by session when , You only need to run once per session , Run before and after each session
params
to fixture Pass in the parameter ,fixture The essence is a function , But it is impossible to transfer parameters in the normal way
Use request.param In the fixture The function gets this parameter internally
@pytest.fixture(params=[{
"name":"test1"},{
"name":"test2"},{
"name":"test3"}])
def my_fixture_one(request):
return request.param["name"]
def test_my_fixture(my_fixture_one):
print(my_fixture_one)
params It's a List,request.param You can get the elements of the list
request It's a pytest Built in fixture
The above operation results :
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 3 items
test_my_add.py::test_my_fixture[my_fixture_one0] test1
PASSED
test_my_add.py::test_my_fixture[my_fixture_one1] test2
PASSED
test_my_add.py::test_my_fixture[my_fixture_one2] test3
PASSED
ids
If not specified id,pytest Use fixture Name plus a string of numbers as the identification of the case
id It could be a list , It can also be a function
@pytest.fixture(params=[{
"name":"test1"},{
"name":"test2"},{
"name":"test3"}],ids=["first","second","third"])
def my_fixture_one(request):
return request.param["name"]
def test_my_fixture(my_fixture_one):
print(my_fixture_one)
Running results :
C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py"
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 3 items
test_my_add.py::test_my_fixture[first] test1
PASSED
test_my_add.py::test_my_fixture[second] test2
PASSED
test_my_add.py::test_my_fixture[third] test3
PASSED
name
to fixture rename
autouse
autouse=True, All functions within this scope run this fixture
fixture Usage example
To transfer data
@pytest.fixture()
def my_fixture_two():
return "a"
def test_my_fixture_two(my_fixture_two):
lst1=["b"]
lst1.append(my_fixture_two)
print(lst1)
Running results :
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item
test_my_add.py::test_my_fixture_two ['b', 'a']
PASSED
Execute before and after the case runs setup and teardown Work
such as , Insert data into the database before the test run , Delete the record after the test case is executed
stay fixture Use in yield keyword ,yield You can also return a numeric value
@pytest.fixture()
def my_fixture_three():
print(" Actions before test case execution ")
yield
print(" Actions after test case execution ")
def test_my_fixture_three(my_fixture_three):
print(" The test case is executing ....")
Running results :
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: E:\JenkinsLearn\My-pytest\test, configfile: pytest.ini
plugins: cov-3.0.0
collecting ... collected 1 item
test_my_add.py::test_my_fixture_three Actions before test case execution
The test case is executing ....
PASSED Actions after test case execution
thus it can be seen ,fixture It has been implemented before the test case is implemented , If you encounter yield, Start executing test cases , If the test case has a return value , have access to yield return , When the test case is executed , Re execution yield After the code , No matter what happens during the test ,yield The following code will execute
It can be used to create and destroy cases before execution
fixture You can use other fixture
adopt conftest.py Files can be shared fixture
Put it in conftest.py Inside fixture Can be Shared
The use of multiple fixture
A test case can use multiple fixture
Class uses fixture
If a class needs to use fixture, Decorators are required @pytest.mark.usefixture(‘fixture1’,‘fixture2’)
边栏推荐
- Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
- 实战 ELK 优雅管理服务器日志
- Lavaweb [first understanding the solution of subsequent problems]
- Mouse over effect VI
- [applet project development -- JD mall] uni app commodity classification page (first)
- Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
- Golang多图生成gif
- C#实现图的深度优先遍历--非递归代码
- So easy deploy program to server
- Od modify DLL and exe pop-up contents [OllyDbg]
猜你喜欢

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

Network address translation (NAT) technology

Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example

第03章_用戶與權限管理

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

Redis efficient like and cancel function

Record a service deployment failure troubleshooting

Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
![[exsi] transfer files between hosts](/img/c3/128b72aca6e030b2d4be2b6bddbc43.png)
[exsi] transfer files between hosts

HTB-Lame
随机推荐
Servlet [first introduction]
So easy deploy program to server
基于Pytorch完整的训练一个神经网络并进行验证
robots. Txt restrict search engine inclusion
Mybati SQL statement printing
Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
XXL job User Guide
[exsi] transfer files between hosts
Magnetic manometer and measurement of foreign coins
xxl-job使用指南
Record a service deployment failure troubleshooting
Mouse over effect II
Chapter 03_ User and authority management
php批量excel转word
[PR # 5 A] two way running (state pressure DP)
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Huawei operator level router configuration example | configuration static VPLS example
Design practice of current limiting components
Lavaweb [first understanding the solution of subsequent problems]
世界上最好的学习法:费曼学习法