0

I have following structure of tests:

tests
|__element
|  |__test_create.py
|  |__test_delete.py
|  |__test_get.py
|  |__test_recover.py
|  |__test_update.py
|__conftest.py

I have fixture in conftest.py which must take the same value throughout the session:

@pytest.fixture(scope="session")
async def workspace_locales_set() -> list[str]:
    random_locales_subset = random.sample(list(LOCALES_ALLOWED.keys()), random.randint(3, 7))

    print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", random_locales_subset)
    return random_locales_subset

But all the tests, in test_update fall down because there comes a different locales value.

By the method of many prints, I found out that at the moment of switching from test_recover to test_update`, the fixture is calculated a second time

Here is tests report:

collecting ... collected 893 items

test_create.py::TestElementCreateFieldName::test_name_value_locales_allowed 
test_create.py::TestElementCreateFieldName::test_name_value_locales_not_allowed 
...
test_delete.py::TestElementsDeleteExisting::test_element_existing_after_deletion[True] 

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ['sv-FI', 'gsw-CH', 'zh-Hant-TW']

PASSED [  0%] PASSED [  1%]

...
test_create.py::TestElementCreateFieldAttributesFileInput::test_attrs_file_allowed_file_types_is_not_enum_item PASSED [  5%]

test_delete.py::TestElementsSoftDeleteFieldIsDeleted::test_element_is_deleted PASSED [ 18%]PASSED [ 18%]

test_get.py::TestElementsGetFieldName::test_name_value PASSED [ 19%]PASSED [ 19%]PASSED [ 20%]PASSED [ 20%]

test_recover.py::TestElementsRecoverFieldIsDeleted::test_element_is_deleted 
test_recover.py::TestElementsRecoverAccessRights::test_recovering_by_user_not_in_parent_workspace 
test_update.py::TestElementsUpdateName::test_update_name_value PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]
ERROR     [ 24%]
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ['es-CL', 'kn', 'xog', 'es-PY', 'hi-IN', 'en-AS', 'en-TT']

As you can see, here is something weird: first the creation tests are run, then one deletion test, then the fixture is computed the first time, then the rest of the run tests go, and between the recovery tests and the deletion test the fixture is computed the second time

I use pytest-asyncio with asyncio_mode = auto. Could it have any effect?

But anyway, the session fixture should be calculated only once. What am I doing wrong?

Prosto_Oleg
  • 322
  • 3
  • 13
  • Does this answer your question? [pytest issues with a session scoped fixture and asyncio](https://stackoverflow.com/questions/63713575/pytest-issues-with-a-session-scoped-fixture-and-asyncio) – Michael Delgado Oct 16 '22 at 06:30

1 Answers1

0

Okay, that was really stupid. My PyCharm automatically added from conftest import workspace_locales_set in test_update file, so the fixture was called twice.

To fix it just delete all imports from conftest

Prosto_Oleg
  • 322
  • 3
  • 13