Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created November 23, 2023 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijitmamarde/ffb0b03e1d24b5c47a89f227d4ff9547 to your computer and use it in GitHub Desktop.
Save abhijitmamarde/ffb0b03e1d24b5c47a89f227d4ff9547 to your computer and use it in GitHub Desktop.
Running blocking servers / long running tasks before running a test
import time
from multiprocessing import Process
import pytest
def proc_a():
print("starting proc a")
i = 1
# for i in range(10):
while True:
time.sleep(1)
print(f"in proc_a: i={i}")
i += 1
def proc_b():
print("starting proc b")
i = 1
# for i in range(15):
while True:
time.sleep(1)
print(f"in proc_b: i={i}")
i += 1
def call_in_process(proc_func):
p = Process(target=proc_func)
p.start()
return p
@pytest.fixture()
def start_a():
proc = call_in_process(proc_a)
print(f"Started A: pid is:{proc.pid}")
yield
proc.kill()
@pytest.fixture()
def start_b():
proc = call_in_process(proc_b)
print(f"Started b: pid is:{proc.pid}")
yield
proc.kill()
def test_a(start_a, start_b):
time.sleep(5)
assert 1 == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment