Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help Needed] Not able to execute before_all and after_all method only once #178

Open
prashant1507 opened this issue Nov 1, 2024 · 3 comments
Labels
by design The reported issue is the framework behavior by design enhancement New feature or request

Comments

@prashant1507
Copy link

prashant1507 commented Nov 1, 2024

Description
I'm running my tests in parallel using 4 processes - scenario by scenario.

Now I want to do some tasks in before_all and after_all but only 1 time, meaning do X in before_all only 1 time and use X in after_all only 1 time

What I tried
I tried to use context.config.userdata['worker_id'], current_process(), parent_process() in order to identify but instances are totally differet.

Please suggest me some solution or a workaround which I can try to achieve it

Thanks

@prashant1507 prashant1507 changed the title Not able to execute before_all and after_all method only once [Help Needed] Not able to execute before_all and after_all method only once Nov 1, 2024
@iamkenos
Copy link
Contributor

iamkenos commented Nov 5, 2024

Hi! A couple workarounds I can think of (depending on what it is you wanna do):

  • if you dont need the access to context, perhaps consider doing that one thing before and after the behavex process. e.g. run a python script that does that one thing before launching behavex, then another one after. Then perhaps you can chain these scripts with various ways like via cli or makefile for instance.
  • if you need access to context, try to see if creating a temporary file helps. so, check if the file exists -> create if not -> do the thing. i think you'll still get race conditions using this approach but worth trying.

watching the issue to keep an eye out for other inputs!

@prashant1507
Copy link
Author

I tried second approach but it fails because of race condition specially in after_all

( Yes I need context in both after_all and before _all)

@hrcorval hrcorval added the enhancement New feature or request label Dec 6, 2024
@hrcorval
Copy link
Owner

hrcorval commented Dec 6, 2024

Hi @prashant1507, having before_all and after_all being executed only once is not possible by design. As "context" variables are not shared through the different parallel processes, the before_all and after_all hooks need to be executed in each parallel session.
However, it might be possible to provide a new hook (something like behavex_before_all/behavex_after_all) that is executed only once pero parallel run. The limitation will be that the "context" variables will not be available as part of this hook, due to there is no easy way to replicate complex objects across parallel processes (only primitive values).
I'm setting this as an enhancement.

@hrcorval hrcorval added the by design The reported issue is the framework behavior by design label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by design The reported issue is the framework behavior by design enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants