行业动态

Pytest运行测试用例的多种方式

  Pytest提供了多样化的方式来运行和调试测试用例,本文介绍一些比较常用的方式。

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

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

Pytest运行测试用例的多种方式

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

  #Below are test_pytest_markers.py

  # 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

  1.Pytest Marker 机制

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

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

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

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

  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.exe

  cachedir: .cache

  rootdir: 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 ====================

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

  C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m "not 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.exe

  cachedir: .cache

  rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:

  collected 4 items

  test_pytest_markers.py::test_something_quick PASSED

  test_pytest_markers.py::test_another PASSED

  test_pytest_markers.py::TestClass::test_method PASSED

  ============================= 1 tests deselected ==============================

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

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

  C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass::test_method

  ============================= test session starts =============================

  platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe

  cachedir: .cache

  rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:

  collected 5 items

  test_pytest_markers.py::TestClass::test_method PASSED

  ========================== 1 passed in 0.03 seconds ===========================

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

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

  C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass

  ============================= test session starts =============================

  platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe

  cachedir: .cache

  rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:

  collected 4 items

  test_pytest_markers.py::TestClass::test_method PASSED

  ========================== 1 passed in 0.04 seconds ===========================

  4.多种组合

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

  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 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe

  cachedir: .cache

  rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:

  collected 8 items

  test_pytest_markers.py::TestClass::test_method PASSED

  test_pytest_markers.py::test_send_http PASSED

  ========================== 2 passed in 0.06 seconds ===========================

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

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

  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.exe

  cachedir: .cache

  rootdir: 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 ====================

  以上内容为大家介绍了Pytest运行测试用例的多种方式,本文由多测师亲自撰写,希望对大家有所帮助。了解更多软件测试相关知识:https://www.duoceshi.com/xwzx-hydt/

新闻资讯

联系我们

联系人:王女士

手机:17727591462

电话:0755-21072941

邮箱:hr@duoceshi.com

地址: 广东省深圳市龙华区龙华街道清湖和平路62号优鼎企创园D栋201室,202室

用手机扫描二维码关闭
二维码