1. 程式人生 > >pytest-15-自定義標記mark

pytest-15-自定義標記mark

結果 app sele pos span 支持 ont with self

pytest可以支持自定義標記,自定義標記可以把一個web項目劃分多個模塊,然後指定模塊名稱執行。app自動化的時候,如果想android和ios公用一套代碼時,
也可以使用標記功能,標明哪些是ios用例,哪些是android的,運行代碼時候指定mark名稱運行就可以

mark標記

1.以下用例,標記test_send_http()為webtest

# content of test_server.py

import pytest

@pytest.mark.webtest
def test_send_http():
    pass # perform some webtest test for your app

def test_something_quick():
    pass

def test_another():
    pass

class TestClass:
    def test_method(self):
        pass

if __name__ == "__main__":
    pytest.main(["-s", "test_server.py", "-m=webtest"])

只運行用webtest標記的測試,cmd運行的時候,加個-m 參數,指定參數值webtest

$ pytest -v -m webtest

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 4 items / 3 deselected

test_server.py .

=================== 1 passed, 3 deselected in 0.10 seconds ====================

如果不想執行標記webtest的用例,那就用"not webtest"

$ pytest -v -m "not webtest"

import pytest

@pytest.mark.webtest
def test_send_http():
    pass # perform some webtest test for your app
def test_something_quick():
    pass
def test_another():
    pass
class TestClass:
    def test_method(self):
        pass

if __name__ == "__main__":
    pytest.main(["-s", "test_server.py", "-m=‘not webtest‘"])

運行結果

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collected 4 items

test_server.py ....

========================== 4 passed in 0.06 seconds ===========================

-v 指定的函數節點id

如果想指定運行某個.py模塊下,類裏面的一個用例,如:TestClass裏面test_method用例
每個test_開頭(或_test結尾)的用例,函數(或方法)的名稱就是用例的節點id,指定節點id運行用-v 參數

$ pytest -v test_server.py::TestClass::test_method

pycharm運行代碼

if __name__ == "__main__":
    pytest.main(["-v", "test_server.py::TestClass::test_method"])

運行結果

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- E:\python36\python.exe
cachedir: .pytest_cache
metadata: {‘Python‘: ‘3.6.0‘, ‘Platform‘: ‘Windows-10-10.0.17134-SP0‘, ‘Packages‘: {‘pytest‘: ‘3.6.3‘, ‘py‘: ‘1.5.4‘, ‘pluggy‘: ‘0.6.0‘}, ‘Plugins‘: {‘metadata‘: ‘1.7.0‘, ‘html‘: ‘1.19.0‘}, ‘JAVA_HOME‘: ‘D:\\java\\jdk17‘}
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7.0, html-1.19.0
collecting ... collected 1 item

test_server.py::TestClass::test_method PASSED                            [100%]

========================== 1 passed in 0.06 seconds ===========================

當然也能選擇運行整個class

$ pytest -v test_server.py::TestClass

也能選擇多個節點運行,多個節點中間空格隔開

$ pytest -v test_server.py::TestClass test_server.py::test_send_http

pycharm運行參考

if __name__ == "__main__":
    pytest.main(["-v", "test_server.py::TestClass", "test_server.py::test_send_http"])

-k 匹配用例名稱

可以使用-k命令行選項指定在匹配用例名稱的表達式

$ pytest -v -k http

$ pytest -v -k http # running with the above defined example module
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 3 deselected
test_server.py::test_send_http PASSED [100%]
================== 1 passed, 3 deselected in 0.12 seconds ==================

您也可以運行所有的測試,根據用例名稱排除掉某些用例:

$ pytest -k "not send_http" -v

=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 1 deselected
test_server.py::test_something_quick PASSED [ 33%]
test_server.py::test_another PASSED [ 66%]
test_server.py::TestClass::test_method PASSED [100%]
================== 3 passed, 1 deselected in 0.12 seconds ==================

也可以同時選擇匹配 “http” 和“quick”

$ pytest -k "http or quick" -v

=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_
?→PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 2 deselected
test_server.py::test_send_http PASSED [ 50%]
test_server.py::test_something_quick PASSED [100%]
================== 2 passed, 2 deselected in 0.12 seconds ==================

pytest-15-自定義標記mark