pytest测试框架入门
发布时间:2020-12-14 14:48:11 所属栏目:百科 来源:网络整理
导读:安装pytest 命令行输入: pip install -U pytest 检查是否安装了正确的版本: λ pytest -- versionThis is pytest version 5.3 . 5 ,imported from e:python37libsite- packagespytest__init__.pysetuptools registered plugins: pytest -html- 2.0 .
安装pytest命令行输入: pip install -U pytest
检查是否安装了正确的版本: λ pytest --version This is pytest version 5.3.5,imported from e:python37libsite-packagespytest__init__.py setuptools registered plugins: pytest-html-2.0.1 at e:python37libsite-packagespytest_htmlplugin.py pytest-metadata-1.8.0 at e:python37libsite-packagespytest_metadataplugin.py 一个简单的demoimport pytest def func(x): return x + 1 test_answer(): assert func(3) == 5 运行demo 首先进入到此demo文件的路径下,然后执行 pytest ,会运行名称为test*.py的文件(*匹配任意符合字母和数字) pytest运行规则:查找当前目录及其子目录下以test_.py或_test.py文件,找到文件后,在文件中找到以test开头函数并执行。 C:UsershaiyDesktopcodeiot_yjb_api λ pytest ============================ test session starts ============================= platform win32 -- Python 3.7.1rc1,pytest-1,pluggy-0.13.1 rootdir: C:UsershaiyDesktopcodeiot_yjb_api plugins: html-0 collected item test_api.py F [100%] ================================== FAILURES ================================== ________________________________ test_answer _________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == E + where 4 = func(3) test_api.py:9: AssertionError ============================= 1 failed in 0.23s ============================= 此测试返回失败报告,因为? ?如何编写pytest测试样例 通过上面的实例,我们发现编写pytest测试样例非常简单,只需要按照下面的规则:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |