I'm trying to use pytest-xdist in order to make my tests run parallel, The issue is that each thread is going to the fixture that shared to all tests and executing it according to threads number.
It cause me an issue because that fixture role is to create data for my tests and once it's already created I get and error since it's already created(via REST).
conftest.py:
lock = threading.Lock()
@pytest.fixture(scope=session)
def init_data(request):
lock.acquire()
try:
data = []
if checking_data_not_created():
data.append(some_rest_command_creating_data_1())
data.append(some_rest_command_creating_data_2())
finally:
lock.release()
yield data
lock.acquire()
try:
remove_all_data()
finally:
lock.release()
tests_class.py:
class classTests():
def first_test(init_data):
test body and validation....
def second_test(init_data):
test body and validation....
I am using the command: pytest -v -n2
Assuming that 1st thread should run first_test() and 2nd thread should run second_test() one of them will always fail because the first already created the data in fixtures section and the other thread will get exception and all tests he should run will fail.
As you can see, I tried to use lock in order to synchronize the threads but it also doesn't work.
any idea how can I overcome this issue?
Thanks.