Pytest自定義標記mark及指定文件/類/方法/用例執行

  • 2019 年 12 月 15 日
  • 筆記

mark 標記

標記執行指定類

pytest.main(['-s','文件名','-m=標記名'])

pytest.main(['-s','test01.py','-m=test'])

import pytest  @pytest.mark.test  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      #運行指定的類      pytest.main(['-s','test01.py','-m=test'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.py test_01  .test_02  .    ============================== warnings summary ===============================  C:Program FilesPython35libsite-packages_pytestmarkstructures.py:324    C:Program FilesPython35libsite-packages_pytestmarkstructures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html      PytestUnknownMarkWarning,    -- Docs: https://docs.pytest.org/en/latest/warnings.html  ======================== 2 passed, 1 warnings in 0.05s ========================    Process finished with exit code 0

標記執行非指定方法

pytest.main(['-s','文件名','-m=not 標記名'])

pytest.main(['-s','test01.py','-m=not test'])

import pytest    class Test(object):      @pytest.mark.test      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      #運行指定的類      pytest.main(['-s','test01.py','-m=not test'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items / 1 deselected / 1 selected    test01.py test_02  .    ============================== warnings summary ===============================  C:Program FilesPython35libsite-packages_pytestmarkstructures.py:324    C:Program FilesPython35libsite-packages_pytestmarkstructures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.test - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html      PytestUnknownMarkWarning,    -- Docs: https://docs.pytest.org/en/latest/warnings.html  ================= 1 passed, 1 deselected, 1 warnings in 0.05s =================    Process finished with exit code 0

-v 指定的函數節點 id

指定執行.py文件

pytest.main(['-v','文件名.py']) pytest.main(['-v','test01.py'])

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      pytest.main(['-v','test01.py'])    "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:Program FilesPython35python.exe  cachedir: .pytest_cache  metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_101', 'Plugins': {'html': '1.22.0', 'metadata': '1.8.0', 'allure-pytest': '2.8.5'}, 'Python': '3.5.2', 'Packages': {'pluggy': '0.12.0', 'py': '1.8.0', 'pytest': '5.1.2'}}  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collecting ... collected 2 items    test01.py::Test::test_01 PASSED                                          [ 50%]  test01.py::Test::test_02 PASSED                                          [100%]    ============================== 2 passed in 0.09s ==============================    Process finished with exit code 0

指定執行文件下_類

pytest.main(['-v','文件名.py::類名']) pytest.main(['-v','test01.py::Test'])

cmd下pytest -v test_server.py::TestClass

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      pytest.main(['-v','test01.py::Test'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:Program FilesPython35python.exe  cachedir: .pytest_cache  metadata: {'Plugins': {'html': '1.22.0', 'allure-pytest': '2.8.5', 'metadata': '1.8.0'}, 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_101', 'Python': '3.5.2', 'Packages': {'pytest': '5.1.2', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Platform': 'Windows-10-10.0.18362-SP0'}  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collecting ... collected 2 items    test01.py::Test::test_01 PASSED                                          [ 50%]  test01.py::Test::test_02 PASSED                                          [100%]    ============================== 2 passed in 0.05s ==============================    Process finished with exit code 0

指定執行文件下_類_方法

pytest.main(['-v','文件名.py::類名::方法名']) pytest.main(['-v','test01.py::Test::test_02'])

cmd下pytest -v test_server.py::TestClass::test_method

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      pytest.main(['-v','test01.py::Test::test_02'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:Program FilesPython35python.exe  cachedir: .pytest_cache  metadata: {'Packages': {'pytest': '5.1.2', 'pluggy': '0.12.0', 'py': '1.8.0'}, 'Plugins': {'allure-pytest': '2.8.5', 'html': '1.22.0', 'metadata': '1.8.0'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'Python': '3.5.2', 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_101'}  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collecting ... collected 1 item    test01.py::Test::test_02 PASSED                                          [100%]    ============================== 1 passed in 0.05s ==============================    Process finished with exit code 0

指定執行多個節點,文件下_類、文件_類_方法

pytest.main(['-v','文件名.py::類名::方法名','文件名.py::類名']) pytest.main(['-v','test01.py::Test::test_02','test01.py::Test'])

cmd下用pytest -v test_server.py::TestClass test_server.py::test_send_http

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')  if __name__=='__main__':      pytest.main(['-v','test01.py::Test::test_02','test01.py::Test'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:Program FilesPython35python.exe  cachedir: .pytest_cache  metadata: {'Python': '3.5.2', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'py': '1.8.0', 'pluggy': '0.12.0', 'pytest': '5.1.2'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.22.0', 'allure-pytest': '2.8.5'}, 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_101'}  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collecting ... collected 3 items    test01.py::Test::test_02 PASSED                                          [ 33%]  test01.py::Test::test_01 PASSED                                          [ 66%]  test01.py::Test::test_02 PASSED                                          [ 66%]    ============================== 3 passed in 0.08s ==============================    Process finished with exit code 0

-s模式執行

-s執行多個.py文件

pytest.main(['-s','文件1.py','文件2.py'])

test01.py    import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-s','test01.py','test02.py'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 10 items    test01.py test_01  .test_02  .  test02.py 22222222  {'sex': '男', 'score': '及格', 'classes': '一班'}  .{'sex': '女', 'score': '及格', 'classes': '一班'}  .{'sex': '男', 'score': '及格', 'classes': '二班'}  .{'sex': '女', 'score': '及格', 'classes': '二班'}  .{'sex': '男', 'score': '不及格', 'classes': '一班'}  .{'sex': '女', 'score': '不及格', 'classes': '一班'}  .{'sex': '男', 'score': '不及格', 'classes': '二班'}  .{'sex': '女', 'score': '不及格', 'classes': '二班'}  .    ============================= 10 passed in 0.10s ==============================    Process finished with exit code 0

-s執行.py文件下的類

pytest.main(['-s','文件.py::類名'])

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-s','test01.py::Test'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collected 2 items    test01.py test_01  .test_02  .    ============================== 2 passed in 0.04s ==============================    Process finished with exit code 0

-K指定匹配用例名稱執行

pytest.main(['-k','-v','test_01'])

pytest.main(['-k','not test_01','-v'])

pytest.main(['-k','用例名1 or 用例名2','-v'])

import pytest  class Test(object):      def test_01(self):          print('test_01')      def test_02(self):          print('test_02')          assert 1==1  if __name__=='__main__':      pytest.main(['-k','test_01 or test_02','-v'])      "C:Program FilesPython35python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py  ============================= test session starts =============================  platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- C:Program FilesPython35python.exe  cachedir: .pytest_cache  metadata: {'Python': '3.5.2', 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_101', 'Packages': {'pytest': '5.1.2', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'Plugins': {'allure-pytest': '2.8.5', 'html': '1.22.0', 'metadata': '1.8.0'}}  rootdir: C:UserswangliPycharmProjectsTesttest  plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0  collecting ... collected 0 items    ============================ no tests ran in 0.65s ============================    Process finished with exit code 0

cmd下運行:

pytest -v -k 用例名

pytest -k 『not 用例名』 -v

pytest -k 『用例名1 or 用例名2』 -v