Search code examples
pythonbddpython-behave

How can I run Behave tests from a Python function?


I have a set of Behave (1.2.6) features and scenarios that are all working correctly individually. But based on certain initial conditions, I need to run subsets of them in a specific order. I know it's not the right way to do BDD (each test should be independent, with its own setup and teardown), but these are integration tests against an actual deployed web app (no mocking), and the setup and teardown take far too long.

I could drive it from a shell script that runs each test in a separate behave run. But I'd like to have a python driver function that would examine the initial conditions, run the requested set of tests in the right order, and output combined summary stats.

So how can I invoke a Behave scenario from a Python function?


Solution

  • You can import the main function and run it:

    from behave.__main__ import main
    
    main("--tags smoke")