1

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.

1 Answer 1

1

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

6
  • The basic idea was this only, Can you please expand it on how I can place it in the conftest.py file and use it in a different classes (separate file) of tests?
    – Vivank
    Apr 5, 2021 at 20:34
  • @Vivank that one is stored in conftest.py pytest.fixture def user(): yield UsersFactory.get_user() UsersFactory.back_user()
    – Vova
    Apr 5, 2021 at 20:35
  • @Vivank fixture is not imported to tests, just place test files near to conftest.py. Userfactory file can be placed to any utils dir and imported to conftest.py
    – Vova
    Apr 5, 2021 at 20:37
  • 1
    Ok thanks for the help. Really appreciate it.
    – Vivank
    Apr 5, 2021 at 20:56
  • Hi on running test in parallel using pytest -n 2 it's not taking the second user from the dict. Can you help me with it?
    – Vivank
    Apr 7, 2021 at 6:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.