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

发表于:2017-10-10 13:33

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Yating_Yang    来源:51Testing软件测试网采编

  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.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 ====================
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号