Skip to main content
 首页 » 编程设计

python之在 python behave 中设置输出和格式,特别是在 environment.py 中

2025年04月02日14oomusou

我可以使用 --format 和 --outfile 参数运行我的行为代码,并按预期工作。

behave 00-test.feature --format json --outfile "C:/results.json" 

这将在 C:/results.json 中以 json 格式给我结果。

我想避免通过 behave.ini 和命令行传递这些变量。我想在 enviroment.py 中设置这些变量,这样我就可以从任何地方简单地运行以下命令:

behave 00-test.feature 

在 environment.py 文件中,我尝试了所有我能想到的组合,它们都归结为这三个变量,但没有运气。

def before_all(context): 
    ... 
    context.config.outfiles = ["C:/results.json"] 
    context.config.outputs = [StreamOpener("C:/results.json")] 
    context.config.formatters = ['json'] 
    ... 

为什么选择 StreamOpener? https://github.com/behave/behave/blob/master/behave/configuration.py 显示输出是 StreamOpener 对象的列表。

有人知道在 python-behave 中设置输出文件和格式化程序的正确方法吗?

请您参考如下方法:

这是一个解决方案吗?我像这样从另一个模块运行它

from behave import __main__ as behave_executable 
behave_executable.main(args="--format json --outfile results.json")