《全网非常详细的pytest配置文件.docx》由会员分享,可在线阅读,更多相关《全网非常详细的pytest配置文件.docx(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.更改默认命令行选项 注册标记来防止拼写错误 指定pytest的最低版本号 指定pytest忽略某些目录 指定测试目录 更改测试搜索的规则o pythonclasseso python_files o python_functions O禁用XPATH说到配置,大家可能想到的是不经常更改的内容,比如Django里的settings. py文件,或 者我们做自动化的时候,把测试环境的域名和正式环境的域名放到一个配置文件里,所有的 接口都从这个文件里读取。这样,如果有一天,我们的域名变了,我们只需要更改配置里的 域名就可以了。pytest里也有几个配置文件。 pytest. ini: pytest
2、的主配置文件,可以改变pytest的默认行为,有很多可配置的 选项。 conftest, py:是本地的插件库,其中的hook函数和fixture将作用于该文件所在的 目录以及所有子目录。 _init_. py:每个测试子目录都包含该文件时,那么在多个测试目录中可以出现同名 测试文件。 tox. ini:它与pytest. ini类似,只不过是tox的配置文件,你可以把pytest的配置 都写在tox. ini里,这样就不用同时使用tox. ini和pytest. ini两个文件更改默认命令行选项我们之前已经用过pytest命令行选项了 ,比如-v/-verbose可以输出详细信息,-show
3、locals可以查看失败测试用例里堆栈中的局部变量。你也许经常要用到某些参数, 又不想重复输入,这时可以使用pytest. ini文件里的addopts设置。pytest. pytest. python_classes = *Test Test* *Suite这个设置允许我们像下面这样给类取名 class DeleteSuite():.def test_delete_l(self):.pass.def test_delete_2(self):.pass我们修改test_001目录下的test_onne. py文件. class DeleteSuite ():. def test_delete_
4、l (self):.assert Truedef test_delete_2(self):assert False先不添加python classes执行一下E:django-projectornL_projectdjangotes1pytest -v test_00l/test_one. py二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二4电已3电 3已3电口日电3卜二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二platform Win32 一一 Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pl
5、uggy-0. 12. 0 一一 c:usersadininistratorappdataloc; pythonpython36-32python. exe cachedir: . pytest_cachemetadata: Python,: ,3. 6. 5? , Platform : ? Windows-10-10. 0. 17134-SP0, Packages : pytest? : 4. 5. O, py, pluggy,: J 0. 12. 0,), Plugins : allure-pytest3 : ? 2. 7. T , html : ,1. 21. T , metadata
6、: 1 8 O, J JAVA_HOMI gram FilesJavajdkl. 8. 0_18T rootdir: E:django-projectorirt_projectdjangotest, inifile: pytest. ini alluy已 初邛est-2 7. 1, html-1. 21. 1, metadata-1. 8. 0,Elected。提示没有找到测试用例- no tests ran in 0. 01 seconds =:然后我们添加上python_classes执行一下pytest python_classes = *Test Test* *Suite E : dj
7、ango-projectorirL_projectdjangotest pytest -v test_001/test_one. py=-xesx session sxarxs -=platform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy-0. 12. 0 - c:usersadininistratorappdatalocs pythonpython36-32python. execachedir: . pytest_cachenetadata: Python : 3. 6. 5, Platform : J Wind
8、ows-10-10. 0. 17134-SP0, Packages : pytest : 4. 5. 0, py: pluggy7 : ? 0. 12. 07), Plugins : allure-pytest7 : ,2. 7. T , html : 1 1. 21. T , metadata : 1 8 O, 7 JAVA_HOME gram FilesJavajdkl. 8. 0_181rootdir : E:django-projectoriTL_projectdjangotest, inifile: pytest. iniplugins: allure-pytest-2. 7. 1,
9、 html-1. 21. 1, metadata-1. 8. 0collected 2 items |test_001/test_one. py:DeleteSuite:test_delete_l PASSEDltest_001/test_one. py::DeleteSuite:test_delete_2 FAILED= FAILURES = DeleteSuite. test_delete_2 test_00ltest_one. py:6: in test_delete_2 assert FalseE assert False=1 failed, 1 passed in 0. 08 sec
10、onds =python_files像pytest_classes 一样,python files可以更改默认的测试搜索规则,而不是仅查找以test_*开头的文件和以*_test结尾的文件假设你的测试文件统一命名为check_something. py。你不必重命名所有的测试文件,只要在pytest. ini文件里增加一行配置即可. pytest. pythonfiles = test_* *_test check_*我们将test_one. py改为check_one. py在执行. Fpytest). python_files = test_* *_test check_*pythoncl
11、asses = *Test Test* *Suiteizn,3 J 丁,_K/JL UJ L Jill yjl UJ JJ 仁 13.|3JL%U 2J3E:django-projectornL_projectdjangotest pytest test_0011Tesx session starts = platform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy-0. 12. 0 - c:usersadministratorappdatalocc pythonpython36-32python. exe cached
12、ir: . pytest_cachemetadata :Python,: 3. 6. 5, Platform : Windows-10-10. 0. 17134-SP0, Packages : pytest? : 4. 5. 0, py: pluggy? : ? 0. 12. 0?, Plugins : allure-pytest,: ? 2. 7. T , html : 1. 21. T , metadata : 1 8 O), ? JAVA_HOMI gram FilesJavajdk 1. 8. 08T rootdir: E:django-projectorirL_projectdjan
13、gotest, inifile: pytest. iniplugins: allure-pytest-2. 7. 1, html-1. 21. 1, metadata-1. 8. 0 collected 2 itemstest_00l/check_one. py:DeleteSuite:test_delete_l PASSEDtest_00l/check one. py: :DeleteSuite:: test_delete_2 FAILED= FAILURES = DeleteSuite. test_delete_2 test_00 lcheck_one. py : 6 : in test_
14、delete_2assert FalseE assert False=1 failed, 1 passed in 0. 13 seconds =python_functionspython_functions与之前的两个设置类似,它只是用来测试函数和方法的命名。more规则以test_*开头。如果想添加check_*,则只需要添加一行配置. pytest I. python_functions = test_* check_*修改test one. py下的代码.class DeleteSuite():. def check_delete_l(self):.assert Truedef che
15、ck delete 2 (self):assert FalseIpytestpython_functions = test_* check_* python files = test* *_test check* python_classes = *Test Test* *Suite addopts =-rsxX -1 -v 一一tb二short strictL/. uj aiigu px u j u ux m px u j u uj aiigu ues l/E : dj ango-projectoriruprojectdjangotest pytest test_001=- I tts&ti
16、on starts =:platform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy_0. 12. 0 - c:usarsadministratorappdataloc; pythonpython36-32python. exe cachedir: . pytest_cachemetadata : Python,: J 3. 6. 5, Platform : Windows-10-10. 0. 17134-SP0, Packages : pytest? : 4. 5. O, py, pluggy7 : 0 12 0, P
17、lugins : allure-pytest? : 7 2. 7. T , html? : 1. 21. T , metadata : 1 8 0, 7 JAVA_HOMI feram FilesJavajdkl. 8. 0_18T )jrootdir: E:django-projectorin_projectdjangotest, inifile: pytest. inijplugins: allure-pytest_2. 7. 1, html-1. 21. 1, metadata-1. 8. 0collected 2 items test_001/check_one. py:DeleteS
18、uite:check_delete_l PASSED test_001/check_one. py:DeleteSuite:check delete_2 FAILED = FAILURES =: DeleteSuite. check_delete_2 test_00lcheck_one. py : 6 : in check_delete_2assert FalseE assert False=1 failed, 1 passed in 0. 15 seconds禁用XPATH 设置xfail strict = true将会使那些被标记为pytest. mark, xfail但实际通过的测试用例
19、 也被报告为失败. pytest I. xfail_strict = true很难理解是吧! ! !喝杯茶,我给你给个栗子吃test_one.py代码如下,可以看到,我们有两个测试用例,都是标记的预期失败,但其中一个是会成功,一个是失败的,我们不加xfail_strict = true来执行一下import pytestpytest. mark. xfail () def test_answer ():assert 5 = 5pytest. mark, xfail () def test_answerl():assert 5 != 55:django-projectoriri_projectd
20、jangotest pytest test_001二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二卜电已3电 3已jsion starts =platform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy-0. 12. 0 c:usersadininistratorappdataloca thonpython36-32python. execachedir: . pytest_cachenetadata: Python5 : 3. 6. 5,, Platform : ? Windows-10
21、-10. 0. 17134-SP0J, Packages : pytest? : 4. 5. 0,, py?: ?luggy,: ? 0. 12. 0,), Plugins : allure-pytest,: 2. 7. 1, html,: 1. 21. T , metadata : 1. 8. 0, ? JAVA_HOME gram FilesJavajdkl. 8. 0_181rootdir: E:django-projectorirL_projectdjangotest, inifile: pytest. iniplugins: allure-pytest-2. 7. 1, html-1
22、. 21. 1, metadata-1. 8. 0collected 2 itemsest_001/test_one. py:test_answer XPASSest_001/test_one. py:test_answerl XFAIL = short test summary info - KFAIL test_001/test_one. py:test_answerlKPASS test_001/test_.one. py: :test_answer=1 xfailed, 1 xpassed in 0. 12 seconds可以看到有一个通过了,有一个失败了,但是我们如果想把标记为预期失
23、败的,不管结果是成功还是失败都标记为失败,则要在配置里加xfail_strict = truepytest一xfail strict = true力 i-pr i I. irl5111Hs i./E : django-projectoriTL_projectdjangotest|pytest test_001 lust bussion startsplatform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy-0. 12. 0 - c:usersadininistratorappdatalocw pythonpython3
24、6-32python. execachedir: . pytest_cachemetadata :Python,: 3. 6. Platform : J Windows-10-10. 0. 17134-SP0, Packages : pytest : 4. 5. 0, py:pluggy? : 0. 12. Q1, Plugins : allure-pytest,: ? 2. 7. T , html? : 1. 21. T , metadata : 1 8 O, ? JAVA_HOMI gram FilesJavajdk 1. 8.rootdir: E:django-projectorm_pr
25、ojectdjangotest, inifile: pytest. iniplugins: allure-pytest-2. 7. 1, html-1. 21. 1, metadata-1. 8. 0collected 2 items |test_001/test_one. py: :test_answer FAILED|test 001/test one. py: : test answerl XFAIL= FAILURES = test_answer XPASS(strict)- short test suinmary info = XFAIL test_001/test_one. py:
26、test_answerl=1 failed, 1 xfailed in 0. 07 seconds =这样两个用例就都是失败的. addopts = -rsxX -1 一一tb=short -一strict一rsxX表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际通过的原因。-1 表示pytest报告所有失败测试的堆栈中的局部变量。一tb=short表示简化堆栈回溯信息, 只保留文件和行数。一strict选项表示禁止使用未在配置文件中注册的标记。我们有个test_two. py文件,里面有如下代码.def inc (x):. return x + 1*. def test_an
27、swer():. assert inc (3) = 55. def test answerl(): assert inc (4) = 5我们没有添加如上命令执行一遍再去pytest. int里添加上上面的代码,在去执行一遍注册标记来防止拼写错误在前面我们学过,自定义标记可以简化测试工作,让我们用指定的标记运行某个测试子集。 但是,标记很容易拼错,比如把稣丫185七mark, smoke拼成pytest. mark, somke,默认情况下。这不会引起程序错误。pytest会以为这是你创建的另一个标记。为了避免拼写错误。可以在 pytest. ini文件里注册标记. pytest.markers
28、 =smoke: Run the smoke test functions for tasks project. get: Run the test functions that test tasks, get()修改 test. two. py. import pytest.def inc(x):. return x + 1pytest. mark. smokedef test_answer():assert inc (3) = 55pytest. mark, postdef test answerl():assert inc (4) = 5标记注册好后,可以通过pytest -marker
29、s来查看E : django-proj ectorirc_pro j ec ;pytest -markers我们自己注册的我们自己注册的pytest. mark, smoke: Run the smoke test functions for tasks project pytest. mark, get: Run the test functions that test tasks.get() pytest. mark. allure_label: allure label marker pytest. mark. allure_link: allure link markerpytest.
30、 mark. allure_display_name: allure test name markerpytest. mark. allure_description: allure descriptionpytest. mark. allure_description_html : allure description htmlpytest. mark, filterwarnings(warning): add a warning filter to the given test, see https:/docs, pytest. org/er ings. html#pytest-mark-
31、filterwarningspytest. mark. skip(reason=None) : skip the given test function with an optional reason. Exairle: skip(reason=/ rrently testing this) skips the test.没有注册的标记不会出现在一markers列表里。如果使用了一strict选项,遇到拼写错误的 标记或未注册的标记就会报错。pytest. ini里写如下代码pytestmarkerssmoke: Run the smoke test functions for tasks p
32、roject get: Run the test functions that test tasks.get ()g: django-pro jectornL_pro.IE : dj ango-projectoriTL_projec|tpytest -m smoekg: django-pro jectornL_pro.IE : dj ango-projectoriTL_projec|tpytest -m smoek执行的是个错误的,smoek没有test session starts =Iplatform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1
33、. 7. 0, pluggy-0. 12. 0 rootdir: E:django-projectorirL_project, inifile: pytest. ini plugins: allure-pytest-2. 7. 1, html-1. 21. 1, metadata-1. 8. 0 collected 6 items / 6 deselected = warnings suinmary = C:Us er sAdmi ni s tr a t orAppD ataRo ami ngPythonPython3 6site-packages_pyt e s tmarks true tu
34、r e s. py:324C : UsersAdininistratorAppDataRoainingPythonPython36site-packages_pytestmarkstructures. py:324: Pytej Warning: Unknown pytest. mark, post - is this a typo? You can register custom marks to avoid this warning -: see https:/docs, pytest. org/en/1atest/mark. htmlPytestUnknownMarkW arning,-
35、Docs: https:/docs, pytest. org/en/latest/warnings. html=6 deselected, 1 warnings in 0. 10 seconds =上面的代码我们在执行的时候,把smoke故意写错了,也没有报错,只是给我们一个警告, 只需要我们在ini里注册下就不会有警告了在去修改pytest. ini里的代码pytestjaddopts = -rsxX -1 -tb=short -strictmarkers =smoke: Run the smoke test functions for tasks project get: Run the
36、test functions that test tasks, get ()我们在ini里加上了 addopts = -rsxX -1 -tb=short -strict这句代码之后,如果我们 拼错了就会报错B: django-projectoriTL_projectE:django-projectoriTL_project ytest -strict -m smoek二二二二二二二二二二二二二二二二二二二二二二二二二二二二二#二二二二二二二二二二二二二二二二二二 tsst 癌ssicin starts = platform Win32 - Python 3. 6. 5, pytest-4.
37、5. 0, py-1. 7. 0, pluggy-0. 12. 0 rootdir: E:django-projectorirL_project, inifile: pytest. iniplugins: allure-pytest-2. 7. 1, html-1. 21. 1, metadata-1. 8. 0 我已经在ini里注册了smoek,但是1collected 4 items / 1 errors / 4 deselectedz. . 一. A 1r 3使用smoek做标记,pytest也会报错= ERRORS = 、 ERROR collecting test_two. py p
38、ost not found in markers configuration option! Interrupted: 1 errors during collection ! =4 deselected, 1 error in 0.20 secondsE:django-projectorm_project指定pytest的最低版本号有些功能是随着pytest版本的更新而加入到里面的,如果我们不想用比较低的版本,可以 在配置里指定最低的版本号。minversion选项可以指定运行测试用例的pytest的最低版本。. pytest.minversion = 4. 0如果使用了老版本的pytest
39、运行该测试,就会得到一个错误信息。指定pytest忽略某些目录pytest执行测试搜索时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录。遇 到这种情况,你可以使用norecurse选项简化pytest的搜索工作。norecurse的默认设置是.* build dist CVS -dares arch和*.egg。因为有,所以将 虚拟环境命名为.venv是一个好注意,所有以.(点)开头的目录都不会被访问。如果不是 以.(点)开头,那么需要把它加入norecursedirs里。比如我想忽略test_001目录. pytest. norecursedirs =.* venv test_00
40、1 *.egg dist build先来看下目录结构7 djangotest y El test_001 fjltestone.pyy d test_002 造 test first.py=pytest.initest_one. py. import pytest. def test_case_01():.assert 0 = 0.def test case 02 ():.assert 0=0*. test_one.pytest_first. import pytest.def inc (x):. return x + 1 * pytest. mark, smoke. def test_ans
41、wer ():. assert inc (3) = 55 * pytest. mark, post, def test answerl ():. assert inc (4) = 5test first如果我们不指定norecursedirs就会执行djangotest目录下的所有文件R: djangr-prnjQctnrTTtjyrcjactdjangntQq皆;E : dj ango-proj ectorirL_proj ectdj angotest: pytest -v |on starts platform Win32 - Python 3. 6. 5, pytest-4. 5. 0, py-1. 7. 0, pluggy-0. 12. 0 - c:usersadministratorappdatal ocalprogramspythonpython36-32python. exe cachedir: . pytest_cachemetadata: Python,: 3. 6. 5, Platform5 : Windows-10-10. 0. 17134-SP0, , Packages
限制150内