Issue
I am trying to maintain a pool of Users with a parameter of availability so that PyTest can pick an available user from the list.
But I cannot figure out where to place it such that even on running multiple tests, the object doesn’t get destroyed.
My User pool looks like this:
class User:
def getId()
def getPwd()
def getAvailability()
Basically, I want to configure PyTest so that before starting every test, it takes the available user from the pool and changes it’s status to False
.
Any help in configuring the conftest.py would be helpful.
Solution
might I didn’t get your question correctly, but first idea was like that:
import pytest
class UsersFactory:
_pool = [{"user": "user1", "status": True}, {"user": "user2", "status": True}, {"user": "user3", "status": True},
{"user": "user4", "status": True}]
_user = None
@classmethod
def get_user(cls):
cls._user = cls._pool.pop(0)
return cls._user
@classmethod
def back_user(cls):
cls._user['status'] = False
cls._pool.append(cls._user)
cls._user = None
@pytest.fixture
def user():
yield UsersFactory.get_user()
UsersFactory.back_user()
def test_smth(user):
print(user)
assert user['status']
def test_smth2(user):
print(user)
assert user['status']
you can set scope to fixture, that your user will be set each test, function, class, package
Answered By – Vova
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0