Selenium login test doesn't accept pytest fixtures for login or refuses to connect
Turning the comment into an answer: by default, the tests avoid running db queries to not to slow down the test run, so User.objects.create_user
doesn't write to database. To ensure db commits, use the @pytest.mark.django_db(transaction=True)
marker in tests or the transactional_db
fixture in your fixtures:
@pytest.fixture()def create_staff_user_in_db(transactional_db): staff_user = User.objects.create_user(...) ...
I assume that the
live_server
fixture of django usestransactional_db
automatically but that thetest_server
doesn't?
Exactly - live_server
implies the transactional mode, but the custom impl of test_server
doesn't. Explicitly requesting the transactional_db
fixture in test_server
should fix it:
@pytest.fixturedef test_server(request) -> LiveServer: request.getfixturevalue("transactional_db") ...
BTW if you just want to apply the live server address dynamically, you shouldn't need to define your own fixture; writing config values should suffice. Example:
# conftest.pydef pytest_configure(config): config.option.liveserver = socket.gethostbyname(socket.gethostname())
This value will then be used by the builtin live_server
fixture. Put the conftest.py
into your project or tests root dir to make pytest
find the hookimpl early enough for it to be applied.