11
9

More than 3 years have passed since last update.

pytest-htmlでテスト関数のdocstringをレポートに出力する

Posted at

テスト関数の詳細情報としてdocstringをpytest-htmlのレポートに出力する方法をまとめます。

1. conftest.pyを編集

testsディレクトリ直下のconftest.py(なければ作成)に以下の設定を追記します。
テーブルヘッダーの3列目にタイトル、テーブルボディの3列目にテスト関数のdocstringの情報を挿入しています。

conftest.py
import pytest
from py.xml import html


def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))

def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

2. テストコードにdocstringを書く

def test_one(self):
    """
    これはテスト1です
    """
    assert True

def test_two(self):
    """
    これはテスト2です
    """
    assert False

3. テスト実行

$ pytest --html=report.html

4. 結果

テーブルの3列目に各テスト関数のdocstringが表示されています。

pytest-html.png

参考

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9