简体   繁体   English

将命令行参数传递给 python-behave

[英]passing command line argument to python-behave

I am using python-behave for BDD testing, I have to pass an URL (eg www.abc.com) from command line.我正在使用 python-behave 进行 BDD 测试,我必须从命令行传递一个 URL(例如 www.abc.com)。

$behave -u "www.abc.com" 

To achieve this, I have read behave documentation but there are not enough materials as well as explanations given for setting up the behave.ini file.为了实现这一点,我阅读了 行为文档,但没有足够的材料和解释来设置行为.ini 文件。 I am also not sure how behave.ini file helps me to pass an argument.我也不确定behavior.ini 文件如何帮助我传递参数。

Can someone please tell me how I can setup command line parameters for behave?有人可以告诉我如何为行为设置命令行参数吗?

The suggested solutions above were needed in the past.过去需要上述建议的解决方案。

behave-1.2.5 provides a "userdata" concept that allows the user to define its data:行为 1.2.5 提供了“用户数据”概念,允许用户定义其数据:

behave -D browser=firefox ...

SEE ALSO: behave: userdata还请参见: 行为:userdata

Outdated answer, Currently supported itself as described by this answer .过时的答案,目前支持自己,如本答案所述

No, it's not possible, because there is a parser that is defined in configuration.py file, and only allow defined options of it.不,这是不可能的,因为在configuration.py文件中定义了一个parser ,并且只允许定义它的选项

But if you want you can (by help of monkey patch !), just add your option same as other options to this parser .但是,如果您愿意(借助猴子补丁!),只需将与其他选项相同的选项添加到此parser

For do that, first create a file for example behave_run.py and patch this parser before running of behave :为此,首先创建一个文件,例如behave_run.py并在运行behave之前修补此parser

from behave import configuration
from behave import __main__

# Adding my wanted option to parser.
configuration.parser.add_argument('-u', '--url', help="Address of your url")

# command that run behave.
__main__.main()

And now if you run python behave_run.py --help , you can see your new url option:现在,如果您运行python behave_run.py --help ,您可以看到新的url选项:

$ python behave_run.py --help | grep url
  -u URL, --url URL     Address of your url

Now, you can run this behave_run.py file like behave file and pass your url argument too:现在,你可以运行这个behave_run.py类似文件behave的文件,太通过你的网址参数:

$ python behave_run.py --url http://google.com

And you can access this value of url option with context.config.url , for example in environment.py file and then set it for use in other functions:您可以使用context.config.url访问url选项的此值,例如在environment.py文件中,然后将其设置为用于其他功能:

def before_all(context):
    context.browser = webdriver.Firefox()
    context.url = context.config.url

Note:笔记:

If you want to call python run_behave.py as run_behave.py from anywhere, add this line:如果您想从任何地方调用python run_behave.py作为run_behave.py ,请添加以下行:

#!/usr/bin/env python

to first line of run_behave.py and change mode of it to a executable file with chmod +x run_behave.py and then copy this file to one location of your PATH , for example in /usr/local/bin with sudo mv run_behave.py /usr/local/bin/run_behave.pyrun_behave.py第一行,并使用chmod +x run_behave.py将其模式更改为可执行文件,然后将此文件复制到PATH一个位置,例如在/usr/local/bin使用sudo mv run_behave.py /usr/local/bin/run_behave.py

An alternative to the great answer by Omid, would be to set environment variables before your call to behave, something like: Omid 出色答案的另一种选择是在您调用行为之前设置环境变量,例如:

TESTURL="www.abc.com" behave

There are caveats to doing this and some examples of different scopes/permanence of the variables you would be defining in some of the answers here这样做有一些注意事项,以及您将在此处的某些答案中定义的变量的不同范围/永久性的一些示例

As jenisys said, the way to pass user data is:正如jenisys所说,传递用户数据的方式是:

behave -D NAME=VALUE

An the way to access it from behave steps files is:从行为步骤文件访问它的方法是:

context.config.userdata['NAME']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM