网络知识 娱乐 Pytest基础自学系列(四)

Pytest基础自学系列(四)

视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!


使用@pytest.fixture()装饰器来实现部分用例/全部用例的前后置

完整方法如下:

fixture(scope='function', params=None, autouse=False, ids=None, name=None)

参数说明:

  1. scope参数:标记方法的作用域。有4个可选值:function(默认,函数)、class(类)、module(模块)、package/session(包)

-function:每一个函数或方法都会调用

import pytestnn# fixture前置条件 function对应 setup 每条用例都会执行一下scope='function'n# 后置条件 autouse 默认是False 手动调用 不要每次都手动调用 自动调用nn# fixture前置条件 后置条件 关闭系统n@pytest.fixture(scope='function', autouse=True)ndef login():n print('登录系统')n yieldn print('退出系统')nnclass TestCase1:n def test_03(self):n print('测试用例三')nn def test_04(self):n print('测试用例四')nnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test1.py'])

运行结果:

/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.pynTesting started at 上午9:37 ...nLaunching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test1.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 2 itemsnnpy_test1.py::TestCase1::test_03 登录系统nPASSED [ 50%]测试用例三n退出系统nnpy_test1.py::TestCase1::test_04 登录系统nPASSED [100%]测试用例四n退出系统nnn============================== 2 passed in 0.04s ===============================nnProcess finished with exit code 0n

-class:每一个类调用一次,一个类中可以有多个方法

import pytestnn# class 一个类中有多个函数 所有函数执行之前要做的前置条件 后置条件 setupclassn@pytest.fixture(scope='class', autouse=True)ndef login():n print('登录系统')n yieldn print('退出系统')nnclass TestCase1:n def test_03(self):n print('测试用例三')nn def test_04(self):n print('测试用例四')nnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test2.py'])

运行结果

/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::TestCase1nTesting started at 上午9:38 ...nLaunching pytest with arguments py_test2.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 2 itemsnnpy_test2.py::TestCase1::test_03 登录系统nPASSED [ 50%]测试用例三nnpy_test2.py::TestCase1::test_04 PASSED [100%]测试用例四n退出系统nnn============================== 2 passed in 0.03s ===============================nnProcess finished with exit code 0n

-module:每一个.py文件调用一次,该文件内又有多个function和class

import pytestnn# module 一个py文件中有多个类 所有的类用例执行之前要做的事情 之后要做的事情n@pytest.fixture(scope='module', autouse=True)ndef login():n print('登录系统')n yieldn print('退出系统')nnclass TestCase1:n def test_03(self):n print('测试用例三')nn def test_04(self):n print('测试用例四')nnclass TestCase2:n def test_05(self):n print('测试用例五')nn def test_06(self):n print('测试用例六')nnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test3.py'])

运行结果

/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.pynTesting started at 上午9:42 ...nLaunching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test3.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 4 itemsnnpy_test3.py::TestCase1::test_03 登录系统nPASSED [ 25%]测试用例三nnpy_test3.py::TestCase1::test_04 PASSED [ 50%]测试用例四nnpy_test3.py::TestCase2::test_05 PASSED [ 75%]测试用例五nnpy_test3.py::TestCase2::test_06 PASSED [100%]测试用例六n退出系统nnn============================== 4 passed in 0.06s ===============================nnProcess finished with exit code 0n

-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module


  1. params:一个可选的参数列表,实现参数化功能

import pytestnn# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来n# 数据从外部取n# params 元组 列表 元组加字典 列表加字典n# params .param固定的写法n@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'])ndef login(request):n print('登录系统')n yield request.paramn print('退出系统')nnclass TestCase1:n def test_03(self, login):n print('测试用例三', login)nn def test_04(self):n print('测试用例四')nnnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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::TestCase1nTesting started at 上午9:43 ...nLaunching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 6 itemsnnpy_test4.py::TestCase1::test_03[u8863u670d] 登录系统nPASSED [ 16%]测试用例三 衣服n退出系统nnpy_test4.py::TestCase1::test_03[u5305u5305] 登录系统nPASSED [ 33%]测试用例三 包包n退出系统nnpy_test4.py::TestCase1::test_03[u978bu5b50] 登录系统nPASSED [ 50%]测试用例三 鞋子n退出系统nnpy_test4.py::TestCase1::test_04[u8863u670d] 登录系统nPASSED [ 66%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[u5305u5305] 登录系统nPASSED [ 83%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[u978bu5b50] 登录系统nPASSED [100%]测试用例四n退出系统nnn============================== 6 passed in 0.08s ===============================nnProcess finished with exit code 0n

  1. autouse:默认False,需要调用来激活fixture;如果为True,则所有用例自动调用fixture
  1. ids:用例标识ID,每个ids对应于params,如果没有ids,他们将从params自动生成

import pytestnn# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来n# 数据从外部取n# params 元组 列表 元组加字典 列表加字典n# params .param固定的写法n# ids用例去名字n@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'])ndef login(request):n print('登录系统')n yield request.paramn print('退出系统')nnclass TestCase1:n def test_03(self, login):n print('测试用例三', login)nn def test_04(self):n print('测试用例四')nnnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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.pynTesting started at 上午9:46 ...nLaunching pytest with arguments /Users/guoliang/SynologyDrive/SourceCode/pytest3/py_test4.py --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 6 itemsnnpy_test4.py::TestCase1::test_03[aa] 登录系统nPASSED [ 16%]测试用例三 衣服n退出系统nnpy_test4.py::TestCase1::test_03[bb] 登录系统nPASSED [ 33%]测试用例三 包包n退出系统nnpy_test4.py::TestCase1::test_03[cc] 登录系统nPASSED [ 50%]测试用例三 鞋子n退出系统nnpy_test4.py::TestCase1::test_04[aa] 登录系统nPASSED [ 66%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[bb] 登录系统nPASSED [ 83%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[cc] 登录系统nPASSED [100%]测试用例四n退出系统nnn============================== 6 passed in 0.10s ===============================nnProcess finished with exit code 0n

  1. name:fixture的重命名,如果使用了name,那只能将name传入,函数名不再生效

import pytestnn# params 实现参数化 数据 搜索手机 搜索包包 搜索衣服 不同的数据 传进来n# 数据从外部取n# params 元组 列表 元组加字典 列表加字典n# params .param固定的写法n# ids用例去名字n# name是给fixture 函数取小名n@pytest.fixture(scope='function', autouse=True, params=['衣服', '包包', '鞋子'], ids=['aa', 'bb', 'cc'], name='l')ndef login(request):n print('登录系统')n yield request.paramn print('退出系统')nnclass TestCase1:n def test_03(self, l):n print('测试用例三', l)nn def test_04(self):n print('测试用例四')nnnnif __name__ == '__main__':n pytest.main(['-sv', 'py_test4.py'])

运行结果:

/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::TestCase1nTesting started at 上午9:47 ...nLaunching pytest with arguments py_test4.py::TestCase1 --no-header --no-summary -q in /Users/guoliang/SynologyDrive/SourceCode/pytest3nn============================= test session starts ==============================ncollecting ... collected 6 itemsnnpy_test4.py::TestCase1::test_03[aa] 登录系统nPASSED [ 16%]测试用例三 衣服n退出系统nnpy_test4.py::TestCase1::test_03[bb] 登录系统nPASSED [ 33%]测试用例三 包包n退出系统nnpy_test4.py::TestCase1::test_03[cc] 登录系统nPASSED [ 50%]测试用例三 鞋子n退出系统nnpy_test4.py::TestCase1::test_04[aa] 登录系统nPASSED [ 66%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[bb] 登录系统nPASSED [ 83%]测试用例四n退出系统nnpy_test4.py::TestCase1::test_04[cc] 登录系统nPASSED [100%]测试用例四n退出系统nnn============================== 6 passed in 0.06s ===============================nnProcess finished with exit code 0n