上下文:我正在使用 Python 和 Behave (BDD)。
无论我是从命令行(行为)还是从自定义 main() 运行测试,行为都是相同的:测试运行并且我在控制台中看到的唯一输出是标准 BDD 报告。
我的测试包括帮助我调试代码的 print() 语句。但是,当我运行 behave 时,这些打印语句都没有显示在控制台输出中。
有什么方法可以 让我们在代码中“表现”显示打印语句吗?
我的主要()
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")
原文由 Ben 发布,翻译遵循 CC BY-SA 4.0 许可协议
我花了更多时间阅读文档后才弄明白。其实很简单。默认情况下,
behave
不 显示任何输出(即通过使用print()
)除非测试失败。要强制显示所有输出而不考虑测试结果(通过/失败),您只需更改一些默认设置即可。实现这一点的最简单方法是在项目目录的根目录中创建一个名为behave.ini
的文件并放入以下内容:文件名:
behave.ini
下次运行行为测试时,无论测试通过还是失败,您都会看到调试语句的所有输出。