Я нашел хорошее решение, но не уверен, что оно лучшее. Можно извлечь переменные из уровня модуля,
Итак, над TestClass я объявил новую переменную type (list). Когда тест генерирует идентификатор, я могу добавить его в приведенный выше список,
# tests/test_sanity.py
article_ids = []
@pytest.mark.usefixtures("test_setup") # USE "test_setup" FIXTURE with scope=class
class TestSanity:
def test_global_var(self):
art_id = '12321342154'
global article_ids # Declare the in module var as global
article_ids.append(art_id) # Add id generated in test run
assert article_id == '12321342154'
# conftest.py
@pytest.mark.hookwrapper # Add hook wrapper to tests teardown
def pytest_runtest_teardown(item):
yield
print(item.module.article_ids) # By this access the global var decalred in our test module
Единственная проблема, с которой я сталкиваюсь сейчас, - это оболочка перехвата, называемая каждым тестом, а не за сеанс.
Редактировать :
Найден другой способ сделать это:
@pytest.fixture(scope="session")
def ids():
ids = []
yield ids
print(ids)
@pytest.fixture(scope="class")
def api_setup(request, ids):
request.cls.ids = ids
@pytest.mark.usefixtures("api_setup")
class TestSome:
def test_something(self):
global article_ids
art_id = '123'
self.ids.append(art_id)
assert art_id == '123'
Таким образом он добавляет каждый идентификатор, сгенерированный в список, но не вызывает conftest.py в конце каждого теста