How to maintain a pool of users with PyTest & Selenium?


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 would be helpful.


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

    def get_user(cls):
        cls._user = cls._pool.pop(0)
        return cls._user

    def back_user(cls):
        cls._user['status'] = False
        cls._user = None

def user():
    yield UsersFactory.get_user()

def test_smth(user):
    assert user['status']

def test_smth2(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

Leave a Reply

(*) Required, Your email will not be published