如何更改每个测试在pytest-html报告中显示的路径

时间:2019-03-25 13:14:19

标签: python-3.x pytest pytest-html

我正在使用pytest-html生成html report。但是在我的报告中,每个测试用例的运行都显示完整路径。 我只希望显示类名和测试用例名。

在我的conftest.py文件中

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
  pytest_html = item.config.pluginmanager.getplugin('html')
  outcome = yield
  report = outcome.get_result()
  report.description = str(item.function.__doc__)
  extra = getattr(report, 'extra', [])
  if report.when == 'call':
      # always add url to report
      extra.append(pytest_html.extras.url('http://www.example.com/'))
      xfail = hasattr(report, 'wasxfail')
      if (report.skipped and xfail) or (report.failed and not xfail):
          # only add additional html on failure
          extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
      report.extra = extra



@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
  pytest_html = item.config.pluginmanager.getplugin('html')
  outcome = yield
  report = outcome.get_result()
  report.description = str(item.function.__doc__)
  extra = getattr(report, 'extra', [])
  if report.when == 'call':
      # always add url to report
      extra.append(pytest_html.extras.url('http://www.example.com/'))
      xfail = hasattr(report, 'wasxfail')
      if (report.skipped and xfail) or (report.failed and not xfail):
          # only add additional html on failure
          extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
      report.extra = extra

实际结果:显示每个测试的完整路径

预期:仅需要类名和测试用例名

enter image description here

1 个答案:

答案 0 :(得分:0)

您可能可以使用os.environ.get('PYTEST_CURRENT_TEST')