前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest自定义标记mark及指定文件/类/方法/用例执行

Pytest自定义标记mark及指定文件/类/方法/用例执行

作者头像
王大力测试进阶之路
发布2019-12-15 20:39:42
1.9K0
发布2019-12-15 20:39:42
举报
文章被收录于专栏:橙子探索测试

mark 标记

标记执行指定类

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

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

代码语言:javascript
复制
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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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 Files\Python35\lib\site-packages\_pytest\mark\structures.py:324
  C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.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'])

代码语言:javascript
复制
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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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 Files\Python35\lib\site-packages\_pytest\mark\structures.py:324
  C:\Program Files\Python35\lib\site-packages\_pytest\mark\structures.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'])

代码语言:javascript
复制
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 Files\Python35\python.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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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

代码语言:javascript
复制
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 Files\Python35\python.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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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

代码语言:javascript
复制
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 Files\Python35\python.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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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

代码语言:javascript
复制
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 Files\Python35\python.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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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'])

代码语言:javascript
复制
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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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::类名'])

代码语言:javascript
复制
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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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'])

代码语言:javascript
复制
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 Files\Python35\python.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 Files\Python35\python.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:\Users\wangli\PycharmProjects\Test\test
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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档