Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting live_server fixture with Django test working

I would like to use pytest, pytest-django, and pytest-selenium together to test my Django application functionality. If I start the server manually with python manage.py runserver, and manually input the URL, it works fine.

The live_server fixture from pytest-django is supposed to start a server process in the background that I could use, but it's not working. Instead of a passing test, I get "The requested resource was not found on this server."

Here are the relevant sections of my files:

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = chatsite_api.settings.test
addopts = --liveserver localhost:8080  --cov=. --cov-report=html --driver Firefox

test_pages.py

import pytest

def test_homepage(selenium, live_server):
    selenium.get(live_server.url)
    assert "Django: the Web framework" in selenium.title

And chatsite_api.settings.test.py

from .dev import *  # NOQA

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}

DEBUG = True

As I said, the tests run fine when I start the server myself, but the live_server fixture doesn't seem to be doing what it's supposed to. I have verified that the live_server.url is being set according to the addopts line in pytest.ini, but that is as far as I've gotten.

like image 234
Ryan Avatar asked Jan 24 '26 22:01

Ryan


1 Answers

If you are testing against the default index page (the one with the "The install worked successfully! Congratulations!" greeting), it is only shown when running the development server with DEBUG = True. In particular, it won't be present in tests. If you want to use the view, you need to explicitly configure it in urls module like the other views:

# urls.py
from django.urls import path
from django.views.debug import default_urlconf


urlpatterns = [
    path('', default_urlconf, name='index'),
]
like image 92
hoefling Avatar answered Jan 27 '26 10:01

hoefling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!