[Answered ]-Pytest-django object not being persisted in test database

1👍

It turns out that using the browser fixture causes a problem that I don’t fully understand yet. If I pass the browser into a step, and then return it as a target_fixture for later functions to consume then everything works as expected.

[EDIT]
I discovered that if I define the browser in a fixture, that I must have referenced that browser fixture before or at the same time as the django object using it, or the browser refers to a different domain.
So, in the below, even though the message is created in a fixture (test_message), I must refer to it having referenced the browser. If the function message_exists is not passed ‘browser’, then the message is not listed on the message_list page.

def message_exists(browser, test_message):
    test_message.save()
    return test_message


@when("User visits the message list page", target_fixture="page")
def user_visits_messages_page(db, browser):
    browser.visit(browser.domain + browser.pages["list_message"])
    return browser


@then("The message is listed")
def message_is_listed(message, page):
    page.assert_element(f"a[href='/{message.id}/{message.slug}/']")

Leave a comment