如何使用 StaticLiveServerTestCase Suite 以编程方式设置 Wagtail 根页面以进行测试
How To Programmatically Set Up Wagtail Root Page For Tests Utilizing The StaticLiveServerTestCase Suite
这个问题类似于。
我正在利用 Django's StaticLiveServerTestCase 向我的鹡鸰站点添加测试。下面是我手头的代码库示例:
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
所以当我 运行 这个测试用 python manage.py test
时,测试失败了,因为我有一个 500 错误。 请记住,我使用的是 wagtail 而不是单独使用 Vanilla Django。我还使用 Django 的站点框架而不是 Wagtail 的站点框架,因为 allauth 只允许与 Django 的站点框架一起使用。
将 @override_settings(DEBUG=True)
应用于这样的测试后:
@override_settings(DEBUG=True)
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
测试仍然失败,因为正在加载的页面是 wagtail 默认页面。
我的问题是,如何将另一个页面设置为 root/default wagtail 页面,以便在向 localhost:8000 [或任何其他端口号发出请求时由测试服务器提供] 正在制作主页(即 http://localhost:8000/),我看到那个新页面而不是 wagtail 默认页面?
谢谢。
因为StaticLiveServerTestCase
创建了一个新的[临时]“测试”数据库[包括运行migrations
和migrate
] , wagtail 在初始 wagtail start [mysite]
命令后将所有站点和页面重置回初始状态。
这意味着如果您有任何其他 Page
想要成为根页面,则必须对指令进行硬编码才能做到这一点。
下面是实现此目的的方法。
建议在 class 的 setUpClass
方法中设置这些指令 — 通常是 class Main()
class,其他测试 classes 可以继承自;从而鼓励 D.R.Y.
class Main(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(Main, cls).setUpClass()
cls.root = Page.objects.get(id=1).specific
cls.new_default_home_page = Index(
title="New Home Page Index",
slug="index",
)
cls.root.add_child(instance=cls.new_default_home_page)
cls.site = Site.objects.get(id=1)
cls.site.root_page = cls.new_default_home_page
cls.site.save()
cls.browser = Chrome()
现在我的测试 classes(无论它们在哪里)可以从这个 class 继承并立即获得整个新主页设置。例如:
# ExampleTest() inherits from Main() for ease of Wagtail Page setup: avoiding repetition of setUpClass().
class ExampleTest(Main):
def test_example_test(self):
self.assertContains("Contact Page", self.browser.title)
[...]
希望有一天这能帮助到那里的人。
此解决方案适用于:wagtail==2.7.4
. 不能保证高于此版本的任何内容都按照 wagtail 的代码库指示工作。但是,这不太可能不起作用。
这个问题类似于
我正在利用 Django's StaticLiveServerTestCase 向我的鹡鸰站点添加测试。下面是我手头的代码库示例:
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
所以当我 运行 这个测试用 python manage.py test
时,测试失败了,因为我有一个 500 错误。 请记住,我使用的是 wagtail 而不是单独使用 Vanilla Django。我还使用 Django 的站点框架而不是 Wagtail 的站点框架,因为 allauth 只允许与 Django 的站点框架一起使用。
将 @override_settings(DEBUG=True)
应用于这样的测试后:
@override_settings(DEBUG=True)
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
测试仍然失败,因为正在加载的页面是 wagtail 默认页面。
我的问题是,如何将另一个页面设置为 root/default wagtail 页面,以便在向 localhost:8000 [或任何其他端口号发出请求时由测试服务器提供] 正在制作主页(即 http://localhost:8000/),我看到那个新页面而不是 wagtail 默认页面?
谢谢。
因为StaticLiveServerTestCase
创建了一个新的[临时]“测试”数据库[包括运行migrations
和migrate
] , wagtail 在初始 wagtail start [mysite]
命令后将所有站点和页面重置回初始状态。
这意味着如果您有任何其他 Page
想要成为根页面,则必须对指令进行硬编码才能做到这一点。
下面是实现此目的的方法。
建议在 class 的 setUpClass
方法中设置这些指令 — 通常是 class Main()
class,其他测试 classes 可以继承自;从而鼓励 D.R.Y.
class Main(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(Main, cls).setUpClass()
cls.root = Page.objects.get(id=1).specific
cls.new_default_home_page = Index(
title="New Home Page Index",
slug="index",
)
cls.root.add_child(instance=cls.new_default_home_page)
cls.site = Site.objects.get(id=1)
cls.site.root_page = cls.new_default_home_page
cls.site.save()
cls.browser = Chrome()
现在我的测试 classes(无论它们在哪里)可以从这个 class 继承并立即获得整个新主页设置。例如:
# ExampleTest() inherits from Main() for ease of Wagtail Page setup: avoiding repetition of setUpClass().
class ExampleTest(Main):
def test_example_test(self):
self.assertContains("Contact Page", self.browser.title)
[...]
希望有一天这能帮助到那里的人。
此解决方案适用于:wagtail==2.7.4
. 不能保证高于此版本的任何内容都按照 wagtail 的代码库指示工作。但是,这不太可能不起作用。