Created
November 23, 2023 09:29
-
-
Save abhijitmamarde/ffb0b03e1d24b5c47a89f227d4ff9547 to your computer and use it in GitHub Desktop.
Running blocking servers / long running tasks before running a test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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