专栏首页橙子探索测试Pytest自定义标记mark及指定文件/类/方法/用例执行

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

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 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'])

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'])

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

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

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

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'])

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::类名'])

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'])

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

本文分享自微信公众号 - 橙子探索测试(chengzitest),作者:王荔探索测试

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-11-29

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Pytest跳过执行之@pytest.mark.skip()详解

    在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释...

    橙子探索测试
  • Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

    pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的.

    橙子探索测试
  • Pytest之pytest-assume同用例多断言,断言1失败会执行后续代码及断言2

    一般我们做自动化测试时,一个用例会写多个断言,当第一个断言失败后,后面的代码就不会执行了,于是我们引进了pytest-assume插件可以解决断言失败后继续断言...

    橙子探索测试
  • Pytest跳过执行之@pytest.mark.skip()详解

    在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释...

    橙子探索测试
  • iOS自动化探索(七)自动化测试框架pytest - 测试报告

    JunitXML报告是一种很常用的测试报告,比如可以和Jenkins进行集成,在Jenkins的GUI上显示Pytest的运行结果,非常便利。 运行完case后...

    周希
  • python pytest测试框架

        2.2使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    于小勇
  • 《带你装B,带你飞》pytest成神之路2- 执行用例规则和pycharm运行的三种姿态

      今天北京下的雪好大好美啊!!!哎呀,忘记拍照片了,自己想象一下吧。言归真传,今天还是开始pytest的学习和修炼,上一篇写完后群里反响各式各样的,几家欢乐几...

    北京-宏哥
  • [接口测试_B] 02 Pytest的简单示例

    Pytest是什么 Pytest是Python的一个测试工具,可以用于所有类型和级别的软件测试。Pytest是一个可以自动查找到你编写的用例并运行后输出结果的测...

    苦叶子
  • pytest文档1-环境准备与入门

    首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的。 刚开始我的内...

    上海-悠悠
  • Python测试应用与工具

    Python测试应用与公具 今天跟大家分享一个Python与测试相关的话题,主要介绍Python中的标准库 unittest及第三方测试工具pytest及m...

    1846122963

扫码关注云+社区

领取腾讯云代金券