22

I am using python-behave for BDD testing, I have to pass an URL (e.g. www.abc.com) from command line.

$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. I am also not sure how behave.ini file helps me to pass an argument.

Can someone please tell me how I can setup command line parameters for behave?

2
  • I don't understand what you're asking. The command line arguments should be passed on the command line when you run behave. What is it about that isn't clear? Are you trying to run behave from some other code? What is it that you want to put in the ini file?
    – Blckknght
    Commented Mar 24, 2014 at 9:41
  • i just want pass a url from command line, like: $behave --url "www.abc.com" and use this url to environment.py file within a method def before_all(context): context.browser = webdriver.Firefox() context.url = "www.abc.com" Commented Mar 25, 2014 at 10:00

4 Answers 4

35

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:

behave -D browser=firefox ...

SEE ALSO: behave: userdata

3
  • 1
    @user3375505 imho this should be the accepted anser Commented Jun 25, 2017 at 13:05
  • 2
    Reference link in this answer needs update: behave.readthedocs.io/en/latest/… Commented Jun 20, 2018 at 15:18
  • One tradeoff with this answer is that you cannot use --help to discover the supported parameters. Patching the parser would add arguments and documentation to --help. How do you recommend documenting or making userdata options discoverable? Commented Dec 20, 2021 at 14:41
22
+50

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.

But if you want you can (by help of monkey patch !), just add your option same as other options to this parser.

For do that, first create a file for example behave_run.py and patch this parser before running of behave:

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 | 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:

$ 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:

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:

#!/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.py

2
  • 2
    I think as of behave-1.2.5 @jenisys answer should be the accepted one - it's a clean implementation of the above. Commented Jan 21, 2016 at 10:27
  • 2
    This answer is outdated. Keep scrolling to see the much, much nicer method for doing this posted by @jenisys.
    – s3cur3
    Commented Dec 26, 2018 at 18:18
6

As jenisys said, the way to pass user data is:

behave -D NAME=VALUE

An the way to access it from behave steps files is:

context.config.userdata['NAME']
2
  • Do you know whether it will take multiple key-value pairs? Commented Dec 10, 2018 at 19:08
  • Hi Vreddhi!, yes, you can use -D option in command line to pass in as many user data as possible. Commented Dec 12, 2018 at 9:07
2

An alternative to the great answer by Omid, would be to set environment variables before your call to behave, something like:

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

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.