-
Notifications
You must be signed in to change notification settings - Fork 1
/
pytest_parallelize_tests.py
46 lines (35 loc) · 1.98 KB
/
pytest_parallelize_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pytest
import redis
parallelize_plugin = None
def pytest_configure(config):
global parallelize_plugin
assert parallelize_plugin is None, "Parallelize plugin already configured"
parallelize_plugin = ParallelizePlugin(config)
def pytest_addoption(parser):
parser.addoption('--part-id', type=str, action="store", help="unique part id used to parallelize tests")
parser.addoption('--synchronization-id', type=str, action="store", help="unique id used to parallelize tests")
parser.addoption('--parallelize-tests', action="store_true", default=False, help="turn on tests parallelization")
parser.addoption('--redis-host', type=str, action="store", help="host that runs redis that handles parallelization")
parser.addoption('--redis-port', type=str, action="store", default='6379', help="port that redis listens on")
def pytest_runtest_setup(item):
assert isinstance(parallelize_plugin, ParallelizePlugin), parallelize_plugin
if parallelize_plugin.parallelize_tests:
parallelize_plugin.pytest_runtest_setup(item)
class ParallelizePlugin:
EXPIRATION_TIME_S = 7 * 24 * 3600
def __init__(self, config):
self.parallelize_tests = config.getvalue('parallelize_tests')
self.part_id = config.getvalue('part_id')
self.synchronization_id = config.getvalue('synchronization_id')
if self.parallelize_tests:
assert not (
self.part_id is None or self.synchronization_id is None
), "please provide --synchronization-id and --part-id"
self.redis = redis.Redis.from_url(
f"redis://{config.getvalue('redis_host')}:{config.getvalue('redis_port')}"
)
def pytest_runtest_setup(self, item):
redis_test_key = self.synchronization_id + item.nodeid
if self.redis.setnx(redis_test_key, self.part_id) == 0:
pytest.skip(f"part {self.redis.get(redis_test_key).decode('utf-8')}")
self.redis.expire(redis_test_key, self.EXPIRATION_TIME_S)