Skip to content

Instantly share code, notes, and snippets.

@ilyasotkov
Created March 16, 2022 11:16
Show Gist options
  • Save ilyasotkov/7f9f87daf5324f64e0432903d101bafe to your computer and use it in GitHub Desktop.
Save ilyasotkov/7f9f87daf5324f64e0432903d101bafe to your computer and use it in GitHub Desktop.
Fire and forget pytest fixtures, two ways
import asyncio
import logging
import threading
import time
import pytest
def test_1(my_thread_async_fixture):
logging.warning("Test started")
time.sleep(3)
logging.warning("Test done")
def test_2(my_asyncio_fixture):
logging.warning("Test started")
time.sleep(3)
logging.warning("Test done")
# Fixture started
# sleep_in_fixture() started
# Fixture done
# Test started
# sleep_in_fixture() completed
# Test done
@pytest.fixture
def my_thread_async_fixture():
logging.warning("Fixture started")
threading.Thread(target=sleep_in_fixture, args=[1]).start()
logging.warning("Fixture done")
@pytest.fixture
def my_asyncio_fixture():
logging.warning("Fixture started")
loop = asyncio.get_event_loop()
loop.run_in_executor(None, sleep_in_fixture, 1)
logging.warning("Fixture done")
def sleep_in_fixture(how_long: int):
logging.warning("sleep_in_fixture() started")
time.sleep(how_long)
logging.warning("sleep_in_fixture() completed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment