<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
如下,可以檢視到當前環境中的所有markers
$ pytest --markers @pytest.mark.forked: Always fork for this test. @pytest.mark.flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs. @pytest.mark.hypothesis: Tests which use hypothesis. @pytest.mark.allure_label: allure label marker @pytest.mark.allure_link: allure link marker @pytest.mark.allure_description: allure description @pytest.mark.allure_description_html: allure description html @pytest.mark.filterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/stable/warnings.html#pytest-mark-filterwarnings @pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test. @pytest.mark.skipif(condition, ..., *, reason=...): skip the given test function if any of the conditions evaluate to True. Example: skipif(sys.platform == 'win32') skip s the test if we are on the win32 platform. See https://docs.pytest.org/en/stable/reference.html#pytest-mark-skipif @pytest.mark.xfail(condition, ..., *, reason=..., run=True, raises=None, strict=xfail_strict): mark the test function as an expected failure if any of the conditions eva luate to True. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expe cted, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See https://docs.pytest.org/en/stable/reference.html#pytes t-mark-xfail @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of valu es if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls o f the decorated test function, one with arg1=1 and another with arg1=2.see https://docs.pytest.org/en/stable/parametrize.html for more info and examples. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see https://docs.pytest.org/en/stable/fixture.html#usefix tures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.
方式一:在pytest.ini中按照如下格式宣告即可,冒號之前為註冊的mark名稱,冒號之後為此mark的說明
[pytest] markers = smoke: smoke tests test: system tests
此時test_demo.py程式碼如下
import pytest @pytest.mark.smoke def test_func(): assert 1==1 @pytest.mark.test def test_func2(): assert 1==1 def test_func3(): assert 1==1
使用pytest -m smoke執行結果如下,發現此時即只執行了標記為smoke的一個用例,這就是和自定義mark的使用方法
$ pytest -m smoke ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:srcblogtests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 3 items / 2 deselected / 1 selected test_demo.py . [100%] =================================================================== 1 passed, 2 deselected in 0.04s ====================================================================
方式二:在conftest.py檔案中重寫pytest_configure函數即可,比如如下,註冊兩個mark:smoke和test
def pytest_configure(config): config.addinivalue_line( "markers", "smoke: smoke test" ) config.addinivalue_line( "markers", "test: system test" )
test_demo.py程式碼如下:
import pytest @pytest.mark.smoke def test_func(): assert 1==1 @pytest.mark.test def test_func2(): assert 1==1 def test_func3(): assert 1==1
通過pytest -m test 執行結果如下:
$ pytest -m test ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:srcblogtests plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 3 items / 2 deselected / 1 selected test_demo.py . [100%] =================================================================== 1 passed, 2 deselected in 0.02s ====================================================================
預設情況下,對未註冊mark直接使用是會產生一條告警資訊,比如這裡把pytest.ini和conftest.py都刪除掉,只剩test_demo.py一個檔案
test_demo.py程式碼如下
import pytest @pytest.mark.smoke def test_func(): assert 1==1 @pytest.mark.test def test_func2(): assert 1==1 def test_func3(): assert 1==1
直接使用 pytest -m smoke 執行結果如下,可以發現這裡產生了兩條告警,這就是因為這兩條告警未在pytest.ini或者conftest.py中進行註冊的原因,在實際專案開發中如果在執行測試的時候發現了這種大片告警列印,解決辦法就是在pytest.ini或者conftest.py將這些告警報出的mark都進行註冊即可
$ pytest -m smoke ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:srcblogtests plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 3 items / 2 deselected / 1 selected test_demo.py . [100%] =========================================================================== warnings summary =========================================================================== test_demo.py:3 D:srcblogteststest_demo.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for deta ils, see https://docs.pytest.org/en/stable/mark.html @pytest.mark.smoke test_demo.py:7 D:srcblogteststest_demo.py:7: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo? You can register custom marks to avoid this warning - for detai ls, see https://docs.pytest.org/en/stable/mark.html @pytest.mark.test -- Docs: https://docs.pytest.org/en/stable/warnings.html ============================================================= 1 passed, 2 deselected, 2 warnings in 0.02s ==============================================================
如果希望強制限制必須先註冊再使用mark,則可以在pytest.ini中加上如下設定即可
[pytest] addopts = --strict-markers
比如test_demo.py程式碼:
import pytest @pytest.mark.smoke def test_func(): assert 1==1 @pytest.mark.test def test_func2(): assert 1==1 def test_func3(): assert 1==1
此時繼續使用pytest -m smoke執行結果如下,發現此時已經報錯了,即強制限制必須對mark進行註冊
$ pytest -m smoke ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 rootdir: D:srcblogtests, configfile: pytest.ini plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0 collected 0 items / 1 error ================================================================================ ERRORS ================================================================================ ____________________________________________________________________ ERROR collecting test_demo.py _____________________________________________________________________ 'smoke' not found in `markers` configuration option ======================================================================= short test summary info ======================================================================== ERROR test_demo.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =========================================================================== 1 error in 0.14s ===========================================================================
到此這篇關於Pytest如何使用mark的方法的文章就介紹到這了,更多相關Pytest使用mark內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45