【发布时间】:2019-11-11 04:08:16
【问题描述】:
我想同时使用pytest、pytest-django 和pytest-selenium 来测试我的Django 应用程序功能。如果我用python manage.py runserver手动启动服务器,并手动输入URL,它工作正常。
来自pytest-django 的live_server 固定装置应该在后台启动一个我可以使用的服务器进程,但它不起作用。我没有通过测试,而是得到“在此服务器上找不到请求的资源。”
以下是我文件的相关部分:
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
还有 chatsite_api.settings.test.py
from .dev import * # NOQA
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}
DEBUG = True
正如我所说,当我自己启动服务器时,测试运行良好,但 live_server 固定装置似乎并没有按预期进行。我已经验证 live_server.url 是根据 pytest.ini 中的 addopts 行设置的,但据我所知。
【问题讨论】:
-
您在测试默认登录页面吗?它仅用于演示目的,并且不会在实时服务器启动时显示。你能展示你的
urls.py模块吗? -
如果需要默认页面,可以通过
from django.views.debug import default_urlconf; urlpatterns = [path('', default_urlconf, name='index')]设置为索引页面 -
呃。谢谢你。想把它放在一个答案中,以便我可以相信你解决了这个问题?
标签: python django selenium-webdriver pytest pytest-django