Allure Behaveを使うと、検査結果レポートを見やすく表示できます。

Allure Behave | Allure Report
https://allurereport.org/docs/behave/

また、検査結果にテキスト・画像などの情報を添付して、
付加情報やエビデンスとして活用することもできます。

Attach screenshots and other files | Allure Behave | Allure Report
https://allurereport.org/docs/behave/#attach-screenshots-and-other-files

Attachments | Allure Report
https://allurereport.org/docs/attachments/

ただ、細々した情報を上記のAPIで個々に添付するのは面倒で、
テストコードの可読性も下がるため、
ここでは、標準出力・標準エラー出力をまとめて添付する方法を考えます。

準備の流れ

環境・テストコードを準備していきます。

allure-behaveのセットアップ

Pythonの仮想環境を作成、allure-behaveをセットアップします。

python -m venv venv
. venv/bin/activate
pip install behave
pip install allure-behave

テストコードの作成

以下のように、標準出力・標準エラー出力に情報を出力するテストコードを用意します。

sample.feature

Feature: sample feature

  Scenario: senario1
    When process1

steps/sample.py

import sys
from behave import *

@when("process1")
def step_impl(context):
  print("print something to stdout")
  print("print something to stderr", file=sys.stderr)

.behavercへのレポート形式指定

allureの形式で結果を出力するように、.behavercに以下を記載します。

.behaverc

[behave]
format=allure_behave.formatter:AllureFormatter
outfiles=allure-results

標準出力・標準エラー出力をレポートに出力する対応

step前後のフックを使って、
標準出力・標準エラー出力をキャプチャして、検査結果に添付します。

Environmental Controls | behave
https://behave.readthedocs.io/en/latest/tutorial/#environmental-controls

具体的には、次のようなコードを追加します。

environment.py

import allure
import sys
import io

def before_step(context, step):
    context.step_stdout =  io.StringIO()
    context.step_stderr =  io.StringIO()
    sys.stdout = context.step_stdout
    sys.stderr = context.step_stderr

def after_step(context, step):
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__
    context.step_stdout.flush()
    stdout = context.step_stdout.getvalue()
    if len(stdout) > 0:
        allure.attach(stdout, name="標準出力", attachment_type=allure.attachment_type.TEXT)
    context.step_stderr.flush()
    stderr = context.step_stderr.getvalue()
    if len(stderr) > 0:
        allure.attach(stderr, name="標準エラー出力", attachment_type=allure.attachment_type.TEXT)

実行結果

behaveで検査を実行すると、allure-results配下に結果が出力されます。

$ behave
Feature: sample feature # sample.feature:1

  Scenario: senario1  # sample.feature:3
    When process1     # steps/sample.py:4 0.000s

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
1 step passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

allureのレポートサーバを起動します。

# allureのインストールは以下を参照してください。
# Install or upgrade Allure Report
# https://allurereport.org/docs/install/

$ allure serve

次のように、レポートに標準出力・標準エラー出力の内容が添付されます。

allure-stdout

以上。