class TestCaseMongoDB:
# фикстура создания клиента
@pytest.fixture(scope='class', autouse=True)
def client(self):
link = os.getenv('mongodb_uri')
connect = pymongo.MongoClient(link)
yield connect
connect.close()
# фикстура создания кластера внутри клиента
@pytest.fixture(scope='class', autouse=True)
def db_temp(self, client):
db_name = 'test2'
db = client[db_name]
yield db
# фикстура создания коллекции внутри кластера
@pytest.fixture(scope='class', autouse=True)
def collection_temp(self, db_temp, request):
collection_name = 'test_collection'
collection = db_temp[collection_name]
def finally_drop():
collection.drop()
request.addfinalizer(finally_drop)
yield collection
# кейс выполняется успешно
def test_add_row(self, collection_temp):
result = collection_temp.insert_one({'name': 'John Test'})
assert result is not None
# кейс валится с AssertionError, т.к. db_temp == []
def test_temp(self, db_temp, collection_temp):
collection = collection_temp
assert collection in db_temp.list_collection_names()