Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing command line argument to python-behave

Tags:

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?

like image 398
user3375505 Avatar asked Mar 20 '14 09:03

user3375505


People also ask

What is behave command?

Advertisements. Behave has a collection of command line arguments and it can also be outlined from the configuration files. The values set in the configuration files are used automatically, however, it can be overruled by the command line arguments.

How do you run and behave in Python?

Behave - InstallationFor Behave installation, we should have pip – the package installer for the Python language installed in our system. The pip is installed by default, if the Python version is greater than 2(upto 2.7. 9). We can also use the easy_install for the Behave installation.

Can Python take command line arguments?

You can parse the Python command line arguments in sys. argv without having to know the length of the list, and you can call the built-in len() if the number of arguments is needed by your program.


2 Answers

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

like image 149
jenisys Avatar answered Oct 05 '22 15:10

jenisys


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

like image 37
Omid Raha Avatar answered Oct 05 '22 13:10

Omid Raha