0
votes

This question is similar to another question here on stack overflow .

I am in the process of adding tests to my wagtail site utilizing Django's StaticLiveServerTestCase. Below is an example of the code base I have at hand:

class ExampleTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()

    def test_example_test(self):
        self.assertContains("Contact Page", self.browser.content)
    [...]

So when I run this test with python manage.py test, the test fails because I there is a 500 error. Please recall that I am using wagtail and NOT Vanilla Django alone. I am also using Django's Site framework as opposed to Wagtail's Site framework as allauth only allows for usage with Django's Site framework.

After applying the @override_settings(DEBUG=True) to the test like this:

@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)
        [...]

The test still fails as the page that is being loaded is the wagtail default page.

My question is, how do I set up another page as the root/default wagtail page such that when a request to localhost:8000 [or any other port number given by the test server] is being made to the home page (i.e. http://localhost:8000/), I see that new page instead of the wagtail default page?

Thanks.

1

1 Answers

0
votes

Since StaticLiveServerTestCase creates a new [temporary] "test" database [including running migrations and migrate], wagtail literally resets all sites and pages back to it's initial state after initial wagtail start [mysite] command.

This means that if you have any other Page that you would like to be the root page, you will have to hard code the instruction to do that.

Below is a way in which this can be achieved. It is advisable to set these instructions within the setUpClass method of a class — usually a class Main() class where other test classes can inherit from; thereby encouraging 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()

Now my test classes (wherever they are) can inherit from this class and get the entire new home page setup instantly. For example:

# 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)
    [...]

Hope this helps someone out there someday. THIS SOLUTION IS VALID FOR: wagtail==2.7.4. anything above this version isn't guaranteed to work as wagtail's code base dictates. However, it's very unlikely that this wouldn't work.