pytest-bdd icon indicating copy to clipboard operation
pytest-bdd copied to clipboard

Use of _getframe in get_caller_module_locals (without being able to specify depth) can be problematic

Open cvahid-nih opened this issue 3 years ago • 2 comments

Hello,

First I would like to thank all the maintainers for working on this project, it has been very helpful for our organization, thank you.

I am playing with adding intermediate decorators and wrappers for the step_type functions (when, then, and given) to improve convenience but if I call them from a function inside the steps.py, the step function is not found because the stack frame retrieved by sys._getframe is not the one for the module steps.py which is where the fixtures (and thereby steps) are looked for, so the step gets added to the incorrect f_locals (the function I am calling the step_type from). I was wondering if there is any plan to open up the depth option to be more accessable for situations like this? Or, better yet, maybe avoid this mechanism all together (maybe find the module steps.py some other way).

Thanks

https://github.com/pytest-dev/pytest-bdd/blob/048da9e87a1f504aa7e1686a31710d33816e4c3c/pytest_bdd/steps.py#L132

cvahid-nih avatar Dec 07 '21 17:12 cvahid-nih

Hi! This was discussed a few times #296, #310, but no decision was made. You could provide PR with your vision of how it could be.

BTW you could monkey patch call to get_caller_module_locals() at pytest_bdd.steps._step_decorator using unittest.patch with needed depth

elchupanebrej avatar Jan 01 '22 23:01 elchupanebrej

https://github.com/pytest-dev/pytest-bdd/pull/540 should fix this

youtux avatar Jul 15 '22 17:07 youtux

Fixed by https://github.com/pytest-dev/pytest-bdd/pull/548

youtux avatar Aug 19 '22 19:08 youtux