51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4056|回复: 6

[讨论] Pytest运行测试用例的多种方式

[复制链接]
  • TA的每日心情
    郁闷
    2022-8-29 14:43
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    发表于 2018-4-18 14:03:08 | 显示全部楼层 |阅读模式
    Pytest提供了多样化的方式来运行和调试测试用例,本文介绍一些比较常用的方式。

    pytest是如何识别测试用例的呢?它默认使用检查以test_.py 或test.py命名的文件名,在文件内部查找以
    test打头的方法或函数,并执行它们。

    下面是Pytest的官网的一些测试例子,本文用它来进行不同的Pytest运行。

    首先安装好Python和Pytest,新建一个test_pytest_markers.py,在Terminal里面可以尝试以下不同的运行
    方式。

    #Below are test_pytest_markers.py# content of test_server.pyimportpytest@pytest.mark.webtestdeftes
    t_send_http():pass# perform some webtest test for your appdeftest_something_quick():passdeftest_an
    other():passclassTestClass:deftest_method(self):pass

    1. Pytest Marker 机制

    对于Pytest的测试用例,可以在每一个测试用例加一个marker,比如pytest运行的时就只运行带有该mar
    ker的测试用例,比如下面的@pytest.mark.webtest。

    在我们的实际项目中,我们通常把为每个feature建立相应的marker,用于测试时进行不同条件的挑选。

    Pytest还支持一个测试用例有多个marker,这个在后续会把相应的实践加入进来。

    测试结果如下,只挑选了测试用例带有webtest marker的运行。

    1. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m "webtest" test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.04 seconds ====================

    2. 同样的,也可以只挑选了测试用例中不带有webtest marker的运行。

    3. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m"not webtest"test_pytest_markers.py============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::test_something_quick PASSEDtest_pytest_markers.py::test_another PASSEDtest_pytest_markers.py::TestClass::test_method PASSED=============================1tests deselected ==============================

    4. 2. 选择运行特定的某个测试用例

    5. 你可以按照某个测试用例的的模块,类或函数来选择你要运行的case,比如下面的方式就适合一开始在调试单个测试用例的时候。

    6. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass::test_method============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected5items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.03seconds ===========================

    7. 3. 选择运行特定的某个类

    8. 比如你想单独运行下某个类的所有测试用例,可以按照如下操作。

    9. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.04seconds ===========================

    10. 4 多种组合

    11. 比如下面,你可以进行以上两种方式的组合挑选你需要运行的测试用例。

    12. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass test_pytest_markers.py::test_send_http============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected8items test_pytest_markers.py::TestClass::test_method PASSEDtest_pytest_markers.py::test_send_http PASSED==========================2passedin0.06seconds ===========================

    13. 5 用-k进行关键字匹配来运行测试用例名字子串

    14. 比如用-k选择测试用例的名字中只带有http关键字的运行。

    15. C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -k http test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.12 seconds ====================
    复制代码

    Reference


    回复

    使用道具 举报

  • TA的每日心情

    2020-2-2 12:43
  • 签到天数: 630 天

    连续签到: 1 天

    [LV.9]测试副司令

    发表于 2018-4-19 17:42:29 | 显示全部楼层
    腻害了,多谢分享
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2020-2-2 12:43
  • 签到天数: 630 天

    连续签到: 1 天

    [LV.9]测试副司令

    发表于 2018-4-19 17:42:35 | 显示全部楼层
    腻害了,多谢分享
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-16 13:13 , Processed in 0.073923 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表