documentation icon indicating copy to clipboard operation
documentation copied to clipboard

[ Documentation request ] Python: How (not to) use Pytest fixtures session scope and WorkflowEnvironment

Open edmondop opened this issue 4 months ago • 0 comments

Brief description

I had encounter a problem when reusing a global WorkflowEnvironment defined as a pytest fixture resulted in time-skipping not working. I followed the common practice of define a @pytest.fixture(scope="session") which invoked WorkflowEnvironment.start_time_skipping but time-skipping wasn't happening, i.e. calls to env.sleep(x) were effectively behaving as blocking call. I debugged and realized the source of the blocking was https://github.com/temporalio/sdk-python/blob/a1b5d653fa712d35194e275295c5ec9c3721ecc9/temporalio/testing/_workflow.py#L424

Your recommended content

I naively adopted a well-known pattern (using pytest fixtures with scope="session" for test infrastructure) that lead me in the wrong direction. I think we should make clear in the documentation that tests should be written like so:

def test1():
     with WorkflowEnvironment.start_time_skipping() as env:
              pass

def test2():
     with WorkflowEnvironment.start_time_skipping() as env:
              pass

and the idea of caching the workflow environment, although natural, can lead to a bad outcome.

I am happy to contribute this back to the docs and maybe a sample

edmondop avatar Sep 26 '24 15:09 edmondop