While using the python behave 1.2.7, you may soon end-up with multiple steps trying to find a way to organise them into subdirectories.
tests/ +-- feature/ +-- *.feature +-- steps/ +-- menu_steps/ +-- user_menu_steps.py +-- admin_menu_steps.py +-- organisation_menu_steps.py +-- profile_steps/ +-- user_profile_steps.py +-- admin_profile_steps.py environment.py behave.ini
However, the above strategy will leave the behave runner complaining that the steps could not be found.
"Undefined step. Rest part of scenario is skipped"
A way to go resolve this issue is the following
Convert all step subdirectories into modules by adding an empty __init__.py file in every step subdirectory.
+-- steps/ +-- __init__.py +-- menu_steps/ +-- __init__.py +-- user_menu_steps.py +-- admin_menu_steps.py +-- organisation_menu_steps.py +-- profile_steps/ +-- __init__.py +-- user_profile_steps.py +-- admin_profile_steps.py
Import dynamically all step submodules within the steps directory by adding the following at the steps/__init__.py
import os import pkgutil __all__ =  PATH = [os.path.dirname(__file__)] for loader, module_name, is_pkg in pkgutil.walk_packages(PATH): __all__.append(module_name) _module = loader.find_module(module_name).load_module(module_name) globals()[module_name] = _module
We have tested the above code using Python 3.7 and python-behave 1.2.7 .
Do you want to receive similar articles in your inbox? Subscribe to our mailing list to keep up with our updates.