当前位置:网站首页>Pytest interface automation test framework | @pytest Fixture () decorator
Pytest interface automation test framework | @pytest Fixture () decorator
2022-07-18 23:41:00 【COCOgsta】
Video source :B standing 《 Risking your life to upload !pytest Interface automation test framework ( From basic theory to project practice and secondary development ) Teaching video 【 software test 】》
Organize the teacher's course content and test notes while studying , And share it with you , Infringement is deleted , Thank you for your support !
The complete method is as follows :
fixture(scope='function', params=None, autouse=False, ids=None, name=None)
Parameter description :
- scope Parameters : Scope of marking method . Yes 4 Optional values :function( Default , function )、class( class )、module( modular )、package/session( package )
-function: Every function or method calls
import pytest
# fixture precondition function Corresponding setup Each use case will execute scope='function'
# Postcondition autouse The default is False Manual call Don't call it manually every time Automatically call
# fixture precondition Postcondition Shutdown system
@pytest.fixture(scope='function', autouse=True)
def login():
print(' Login system ')
yield
print(' Exit the system ')
class TestCase1:
def test_03(self):
print(' Test case three ')
def test_04(self):
print(' Test case four ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test1.py'])Running results :
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py
Testing started at In the morning 9:37 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 2 items
py_test1.py::TestCase1::test_03 Login system
PASSED [ 50%] Test case three
Exit the system
py_test1.py::TestCase1::test_04 Login system
PASSED [100%] Test case four
Exit the system
============================== 2 passed in 0.04s ===============================
Process finished with exit code 0
-class: Each class is called once , There can be multiple methods in a class
import pytest
# class There are multiple functions in a class Preconditions to be done before all functions are executed Postcondition setupclass
@pytest.fixture(scope='class', autouse=True)
def login():
print(' Login system ')
yield
print(' Exit the system ')
class TestCase1:
def test_03(self):
print(' Test case three ')
def test_04(self):
print(' Test case four ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test2.py'])Running results
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test2.py::TestCase1
Testing started at In the morning 9:38 ...
Launching pytest with arguments py_test2.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 2 items
py_test2.py::TestCase1::test_03 Login system
PASSED [ 50%] Test case three
py_test2.py::TestCase1::test_04 PASSED [100%] Test case four
Exit the system
============================== 2 passed in 0.03s ===============================
Process finished with exit code 0
-module: every last .py File call once , There are many more in this file function and class
import pytest
# module One py There are multiple classes in the file What should be done before the implementation of all class use cases What to do next
@pytest.fixture(scope='module', autouse=True)
def login():
print(' Login system ')
yield
print(' Exit the system ')
class TestCase1:
def test_03(self):
print(' Test case three ')
def test_04(self):
print(' Test case four ')
class TestCase2:
def test_05(self):
print(' Test case five ')
def test_06(self):
print(' Test case 6 ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test3.py'])Running results
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py
Testing started at In the morning 9:42 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 4 items
py_test3.py::TestCase1::test_03 Login system
PASSED [ 25%] Test case three
py_test3.py::TestCase1::test_04 PASSED [ 50%] Test case four
py_test3.py::TestCase2::test_05 PASSED [ 75%] Test case five
py_test3.py::TestCase2::test_06 PASSED [100%] Test case 6
Exit the system
============================== 4 passed in 0.06s ===============================
Process finished with exit code 0
-session: Multiple files are called once , Can span .py A file called , Every .py The document is module
- params: An optional parameter list , Realize parameterization
import pytest
# params Realize parameterization data Search for mobile phones Search package Search clothes Different data Come in
# Data is taken from the outside
# params Tuples list Tuples plus dictionaries List and dictionary
# params .param Fixed way of writing
@pytest.fixture(scope='function', autouse=True, params=[' clothes ', ' bag ', ' shoes '])
def login(request):
print(' Login system ')
yield request.param
print(' Exit the system ')
class TestCase1:
def test_03(self, login):
print(' Test case three ', login)
def test_04(self):
print(' Test case four ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test4.py'])Running results :
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test4.py::TestCase1
Testing started at In the morning 9:43 ...
Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 6 items
py_test4.py::TestCase1::test_03[\u8863\u670d] Login system
PASSED [ 16%] Test case three clothes
Exit the system
py_test4.py::TestCase1::test_03[\u5305\u5305] Login system
PASSED [ 33%] Test case three bag
Exit the system
py_test4.py::TestCase1::test_03[\u978b\u5b50] Login system
PASSED [ 50%] Test case three shoes
Exit the system
py_test4.py::TestCase1::test_04[\u8863\u670d] Login system
PASSED [ 66%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[\u5305\u5305] Login system
PASSED [ 83%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[\u978b\u5b50] Login system
PASSED [100%] Test case four
Exit the system
============================== 6 passed in 0.08s ===============================
Process finished with exit code 0
- autouse: Default False, It needs to be called to activate fixture; If True, Then all use cases automatically call fixture
- ids: Use case identification ID, Every ids Corresponding to params, without ids, They will come from params Automatic generation
import pytest
# params Realize parameterization data Search for mobile phones Search package Search clothes Different data Come in
# Data is taken from the outside
# params Tuples list Tuples plus dictionaries List and dictionary
# params .param Fixed way of writing
# ids Use case name
@pytest.fixture(scope='function', autouse=True, params=[' clothes ', ' bag ', ' shoes '], ids=['aa', 'bb', 'cc'])
def login(request):
print(' Login system ')
yield request.param
print(' Exit the system ')
class TestCase1:
def test_03(self, login):
print(' Test case three ', login)
def test_04(self):
print(' Test case four ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test4.py'])Running results :
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py
Testing started at In the morning 9:46 ...
Launching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 6 items
py_test4.py::TestCase1::test_03[aa] Login system
PASSED [ 16%] Test case three clothes
Exit the system
py_test4.py::TestCase1::test_03[bb] Login system
PASSED [ 33%] Test case three bag
Exit the system
py_test4.py::TestCase1::test_03[cc] Login system
PASSED [ 50%] Test case three shoes
Exit the system
py_test4.py::TestCase1::test_04[aa] Login system
PASSED [ 66%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[bb] Login system
PASSED [ 83%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[cc] Login system
PASSED [100%] Test case four
Exit the system
============================== 6 passed in 0.10s ===============================
Process finished with exit code 0
- name:fixture Rename of , If used name, That can only name Pass in , The function name is no longer valid
import pytest
# params Realize parameterization data Search for mobile phones Search package Search clothes Different data Come in
# Data is taken from the outside
# params Tuples list Tuples plus dictionaries List and dictionary
# params .param Fixed way of writing
# ids Use case name
# name It's for fixture The function takes the nickname
@pytest.fixture(scope='function', autouse=True, params=[' clothes ', ' bag ', ' shoes '], ids=['aa', 'bb', 'cc'], name='l')
def login(request):
print(' Login system ')
yield request.param
print(' Exit the system ')
class TestCase1:
def test_03(self, l):
print(' Test case three ', l)
def test_04(self):
print(' Test case four ')
if __name__ == '__main__':
pytest.main(['-sv', 'py_test4.py'])Running results :
/Users/guoliang/SynologyDrive/SourceCode/pytest3/venv/bin/python "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target py_test4.py::TestCase1
Testing started at In the morning 9:47 ...
Launching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3
============================= test session starts ==============================
collecting ... collected 6 items
py_test4.py::TestCase1::test_03[aa] Login system
PASSED [ 16%] Test case three clothes
Exit the system
py_test4.py::TestCase1::test_03[bb] Login system
PASSED [ 33%] Test case three bag
Exit the system
py_test4.py::TestCase1::test_03[cc] Login system
PASSED [ 50%] Test case three shoes
Exit the system
py_test4.py::TestCase1::test_04[aa] Login system
PASSED [ 66%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[bb] Login system
PASSED [ 83%] Test case four
Exit the system
py_test4.py::TestCase1::test_04[cc] Login system
PASSED [100%] Test case four
Exit the system
============================== 6 passed in 0.06s ===============================
Process finished with exit code 0
边栏推荐
猜你喜欢

11、摸清JVM对象分布

嘘!摸鱼神器,别让老板知道!| 语音实时转文本,时序快速出预测,YOLOv6在就能用,一行命令整理CSV | ShowMeAI资讯日报

微信小程序_14,组件的创建与引用

小程序:picker-view选择器快速滚动,确认时,”值显示错误“

JVM-SANDBOX导致目标服务JVM Metaspace OOM的调查始末

Compose 使用Coil加载网络图片

Ellipsis in excess of single line text, ellipsis in excess of multi line text, specify multiple lines

Reproduce pytorch version from zero (2)

第四章 指令系统

在代码中用YYYY-MM-DD要注意了!
随机推荐
星巴克不使用两阶段提交
2022-07-15 study notes of group 5 self-cultivation class (every day)
[development tutorial 1] open source Bluetooth heart rate waterproof sports Bracelet - Kit detection tutorial
nodeJS中对Promise模块介绍
OOM简介
用cmd命令进行磁盘清理(主要是系统盘)
pytest接口自动化测试框架 | 接口关联
7月献礼,买云盘就送特级桂七,仅限2个月,欲购从速
可持续水力科学与绿色基础设施国际会议18-19日
Ellipsis in excess of single line text, ellipsis in excess of multi line text, specify multiple lines
Summary of this week 2
Wechat applet_ 17. Slot
Learn more about Arduino steering gear control library file servo h
深入了解arduino舵机控制库文件Servo.h
Compose 使用Coil加载网络图片
10、摸清JVM运行状况
6、JVM分代模型--老年代 的垃圾回收
Over fitting and under fitting
SQL语句的执行计划
开户需要注意的是什么?请问手机开户股票开户安全吗?