I do similar things in my test code. I create a session fixture so that it
survives the entire pytest session. Something like:

@pytest.fixture(scope="session", autouse=True)
def webserver():
    start_webserver_here()
    yield {"url": "the URL here"}
    stop_webserver_here()

I put a more comprehensive example in a gist:
https://gist.github.com/dstanek/326bc12ed3315d5c84aa80beb90fc5e8.

On Fri, Jun 20, 2025 at 9:31 AM Skip Montanaro via code-quality <
code-quality@python.org> wrote:

> I'm struggling trying to understand how Pytest's fixtures can be used to
> facilitate setup and teardown of long-running resource servers. For
> example, I want to fire up a little web server (because the tool I'm
> testing queries a web server IRL) to serve up some simple content. I don't
> want to start it for each test case, just once and the start, then stop it
> at the end. All the notes/documentation I've found seem to emphasize
> fixture's ease-of-use by "requesting" them on a per-test-case basis.
>
> Can someone point me to a tutorial which explains how to properly set up
> and tear down a service at the beginning and end of a test run?
>
> Thx,
>
> Skip
> _______________________________________________
> code-quality mailing list -- code-quality@python.org
> To unsubscribe send an email to code-quality-le...@python.org
> https://mail.python.org/mailman3//lists/code-quality.python.org
> Member address: dsta...@dstanek.com
>


-- 
david stanek
web: https://dstanek.com
twitter: https://twitter.com/dstanek
linkedin: https://www.linkedin.com/in/dstanek/
_______________________________________________
code-quality mailing list -- code-quality@python.org
To unsubscribe send an email to code-quality-le...@python.org
https://mail.python.org/mailman3//lists/code-quality.python.org
Member address: arch...@mail-archive.com

Reply via email to