I started with another question where it was suggested to use fixtures in pytest
I am trying to figure out if fixtures can reside in a standalone class. The examples over the net have them as standalone functions or inherited from another class
My usecase
Why I need them to be standalone ?
I have a class which would connect to external directory server(like LDAP). This class would have fixtures to create/delete objects in the directory server.
Multiple LDAP test classes should be able to use the ExternalDirectory class and connect to different LDAP directory servers
- external_server.py
import ldap
import pytest
class ExternalDirectory(object):
def __init__(self, creds):
self.connection = #connect to LDAP server
@pytest.fixture
def create_user(self):
user = []
def _create_user(self, user_name):
#create user in ldap
user.append(user_name)
yield _create_user
#delete user list
#More fixtures of such kind
def utility_method1(self):
# some util method
#More such util methods
The test class will use this class
- test_ldap_users.py
import ExternalDirectory
class TestLdapUser(object):
@classmethod
def setup_class(cls):
#Fetch credentials from some YAML
cls.ldap_connection = ExternalDirectory(creds)
def test_ldap_user_create(self, <need-to-call-create-user-fixture-here>):
create_user('some-user-name')
...
The Issue
But I am unable to call the fixture here in the testcase. Infact, I can't figure out how to even call them :)
Alternate approach
To inherit the ExternalDirectory class. But then I'll need to remove __init__ and move it to setup_class for ExternalDirectory, then call setup_class in TestLdapUser.
Limitation in the alternate approach
The connection attribute created in setup_class method of ExternalDirectory will not be exclusive for each of the LDAP test class
Question
What would be a good way to handle this ?