【发布时间】:2014-09-28 19:11:08
【问题描述】:
上下文:我正在使用 Python with Behave (BDD)。
无论我是从命令行(行为)还是从自定义 main() 运行测试,行为都是相同的:测试运行并且我在控制台中看到的唯一输出是标准 BDD 报告。
我的测试包括帮助我调试代码的 print() 语句。但是,当我运行行为时,这些打印语句都没有显示在控制台输出中。
我们有什么方法可以在我们的代码中“表现”地显示打印语句?
我的主要()
config = Configuration()
if not config.format:
default_format = config.defaults["default_format"]
config.format = [ default_format ]
config.verbose = True
r = runner.Runner(config)
r.run()
if config.show_snippets and r.undefined_steps:
print_undefined_step_snippets(r.undefined_steps)
我的 test.feature 文件:
Feature: My test feature with the Behave BDD
Scenario: A simple test
Given you are happy
When someone says hi
Then you smile
我的 test_steps.py 文件:
from behave import given, when, then, step, model
@given('you are happy')
def step_impl(context):
pass
@when ('someone says {s}')
def step_impl(context, s):
context.message = s
print("THIS IS NEVER DISPLAYED IN THE CONSOLE")
pass
@then ('you smile')
def step_impl(context):
assert(context.message == "hi")
【问题讨论】:
-
检查行为的命令行参数:pythonhosted.org/behave/behave.html
-
您还需要将 junit 设置为 False,因为在行为/configuration.py 中您可以看到 - stderr_capture、stdout_capture 和 log_capture 为 True
-
比使用打印更好的方法是手动使测试失败,例如: test.fail(f'Could not run test:{meaningful_data}')
标签: python bdd python-behave