【问题标题】:Is it possible to pass python behave command-line arguments from file是否可以从文件中传递 python 行为命令行参数
【发布时间】:2021-02-22 13:51:18
【问题描述】:

我想知道,是否可以传递行为参数,例如。 “-D 环境”。默认情况下,它们取自行为文件。 也许有一些方法可以将每个配置保存在不同的文件中?也许很多行为文件?或如:表现“带参数的文件路径”? 此时我发现我可以放置包含各种配置的 bash 脚本“#!/bin/bash 表现...” 我问是因为我想在运行“behave -...”时轻松管理我的配置而不编辑许多参数

【问题讨论】:

    标签: python python-3.x bdd qa python-behave


    【解决方案1】:

    我认为您可以利用行为 高级案例 部分中描述的自定义 json 配置文件:https://behave.readthedocs.io/en/latest/new_and_noteworthy_v1.2.5.html#advanced-cases

    来自文档:

    # -- FILE: features/environment.py
    import json
    import os.path
    
    def before_all(context):
        """Load and update userdata from JSON configuration file."""
        userdata = context.config.userdata
        configfile = userdata.get("configfile", "userconfig.json")
        if os.path.exists(configfile):
            assert configfile.endswith(".json")
            more_userdata = json.load(open(configfile))
            context.config.update_userdata(more_userdata)
            # -- NOTE: Reapplies userdata_defines from command-line, too.
    

    因此,如果您想通过在命令行中指定来使用自定义配置,您可以运行它,例如:

    behave -D conffile=myconfig.json
    

    然后我会将这条线参数化为:

    myconfigfile = context.config.userdata["conffile"]
    configfile = userdata.get("configfile", myconfigfile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2017-12-18
      • 2011-09-28
      • 1970-01-01
      • 2011-02-21
      • 2013-11-21
      相关资源
      最近更新 更多