前言 唯一能持久的竞争优势是胜过竞争对手的学习能力。——盖亚斯 共勉,多花精力在学习上! 一、ids参数是什么? · ids参数是配合fixture的params参数用的,如果没有设置params参数,那么ids毫无意义; · ids参数是给每一项params参数设置自定义名称用的; · params参数值包含的列表有多少项值,那么ids参数就必须对应有多少项值。 二、ids参数应用 · 2.1 没带ids参数的示例 import pytest user_list = ['xiaoming','xiaohong','xiaoli'] @pytest.fixture(params=user_list) def setUp(request): return request.param def testadd(setUp): print('\n用户名:' + str(setUp)) assert 1 if __name__=='__main__': pytest.main(["-v"]) #-v参数:输出用例详细的执行信息 以上代码执行结果: /usr/local/bin/python3.8 /Users/lanyin/PycharmProjects/newdream/app_demo/blog_demo/test_demo_01.py ============================= test session starts ============================== platform darwin -- Python 3.8.2, pytest-5.4.0, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.8 mcachedir: .pytest_cache metadata: {'Python': '3.8.2', 'Platform': 'macOS-10.15.3-x86_64-i386-64bit', 'Packages': {'pytest': '5.4.0', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'ordering': '0.6', 'html': '2.1.0', 'allure-pytest': '2.8.11', 'metadata': '1.8.0'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home'} rootdir: /Users/liuqingjun/PycharmProjects/newdream/app_demo, inifile: pytest.ini plugins: ordering-0.6, html-2.1.0, allure-pytest-2.8.11, metadata-1.8.0 collecting ... collected 3 items test_demo_01.py::testadd[xiaoming] PASSED [ 33%] test_demo_01.py::testadd[xiaohong] PASSED [ 66%] test_demo_01.py::testadd[xiaoli] PASSED [100%] ============================== 3 passed in 0.03s =============================== · 2.2 带正主 ids参数 的示例 import pytest user_list = ['xiaoming','xiaohong','xiaoli'] param_name = ['first_group_data','second_group_data','third_group_data'] @pytest.fixture(params=user_list,ids=param_name) def setUp(request): return request.param def testadd(setUp): print('\n用户名:' + str(setUp)) assert 1 if __name__=='__main__': pytest.main(["-v"]) #-v参数:输出用例详细的执行信息 以上代码执行结果: /usr/local/bin/python3.8 /Users/lanyin/PycharmProjects/newdream/app_demo/blog_demo/test_demo_01.py ============================= test session starts ============================== platform darwin -- Python 3.8.2, pytest-5.4.0, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.8 cachedir: .pytest_cache metadata: {'Python': '3.8.2', 'Platform': 'macOS-10.15.3-x86_64-i386-64bit', 'Packages': {'pytest': '5.4.0', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'ordering': '0.6', 'html': '2.1.0', 'allure-pytest': '2.8.11', 'metadata': '1.8.0'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home'} rootdir: /Users/liuqingjun/PycharmProjects/newdream/app_demo, inifile: pytest.ini plugins: ordering-0.6, html-2.1.0, allure-pytest-2.8.11, metadata-1.8.0 collecting ... collected 3 items test_demo_01.py::testadd[first_group_data] PASSED [ 33%] test_demo_01.py::testadd[second_group_data] PASSED [ 66%] test_demo_01.py::testadd[third_group_data] PASSED [100%] ============================== 3 passed in 0.02s =============================== 上述两个实例小结: · 可以从代码的输出结果看出ids参数的作用; · 笔者这里认为其实设置与否对测试本身没有多大影响。 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |