我如何在 behave (BDD) 中查看 print() 语句

新手上路,请多包涵

上下文:我正在使用 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 许可协议

阅读 1.1k
2 个回答

我花了更多时间阅读文档后才弄明白。其实很简单。默认情况下, behave 显示任何输出(即通过使用 print() )除非测试失败。要强制显示所有输出而不考虑测试结果(通过/失败),您只需更改一些默认设置即可。实现这一点的最简单方法是在项目目录的根目录中创建一个名为 behave.ini 的文件并放入以下内容:

文件名: behave.ini

 [behave]
stderr_capture=False
stdout_capture=False

下次运行行为测试时,无论测试通过还是失败,您都会看到调试语句的所有输出。

原文由 Ben 发布,翻译遵循 CC BY-SA 3.0 许可协议

从命令行,您可以使用以下命令:

--no-capture 用于立即打印任何标准输出。

--no-capture-stderr 用于立即打印任何 stderr 输出。

原文由 Xuan 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题