10

Is there any way to run python behave from within python and not via command line?

default usage: run behave command in base folder with features/steps

desired usage: call a function (or have a certain import) which executes the behave tests in a specified folder

2
  • There appears to be example code right on the download page that imports it and runs it from a file: pypi.python.org/pypi/behave. In general, yes, if you can run it from command line, you can run it from a file Commented Mar 3, 2015 at 10:28
  • 2
    Maybe I don't see the obvious, but yes I started from the example - and they only start it with the command "behave". Could you please point me to where to they explain how to run it from a file? Thank you! Commented Mar 3, 2015 at 12:40

1 Answer 1

24

Found the solution by working through the behave source code:

from behave.__main__ import main as behave_main
behave_main("path/to/specified/folder")

The main method of behave enumerates and processes all paths it finds in its arguments.

Sign up to request clarification or add additional context in comments.

3 Comments

Example with additional arguments to add tag (-t) and prevent output of skipped scenarios (-k): behave_main(["path/to/tutorial", '-t @run', '-k'])
you may run a single feature file by specifying file path(absolute to relative) behave_main("/".join([str(main.ROOT_DIR.resolve()), "features/Sample_REST_API_Testing.feature"])) or behave_main("~/pythonp/python-bdd/features/Sample_REST_API_Testing.feature")
I had some errors with that approach (RecursionError: maximum recursion depth exceeded while calling a Python object) and found this works fine: from behave import main as behave_main behave_main.main(folder)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.