Forums

My Django tests are failing on PythonAnywhere

hello everyone,

I created a few tests and ran them locally, these basically refer to my urls, and they all pass on my local maching. I have pulled the exact same code to my PythonAnywhere app and running the tests results in PythonAnywhere results in all of my "pages app urls related" tests failing. I am posting the first couple tests below.

from django.test import SimpleTestCase
from django.urls import reverse, resolve

from .views import HomePageView


class HomepageTests(SimpleTestCase):
    def setUp(self):
        url = reverse("home")
        self.response = self.client.get(url)

    def test_url_exists_at_correct_location(self):
        self.assertEqual(self.response.status_code, 200)

    def test_homepage_template(self):
        self.assertTemplateUsed(self.response, "home.html")

And these are the corresponding error messages:

ERROR: test_url_exists_at_correct_location (pages.tests.HomepageTests)
---------------------------------------------------------------------- 
Traceback (most recent call last):   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
        response = get_response(request)   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
        response = self.process_request(request)   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/contrib/messages/middleware.py", line 12, in process_ request
        request._messages = default_storage(request)   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/contrib/messages/storage/__init__.py", line 12, in de fault_storage
        return import_string(settings.MESSAGE_STORAGE)(request)   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/contrib/messages/storage/fallback.py", line 16, in __ init__
        self.storages = [   File "/home/afduggirala/.virtualenvs/yogasiteenv/lib/python3.10/site-packages/django/contrib/messages/storage/fallback.py", line 17, in <l istcomp>

....

I think I am not taking into account something about how PythonAnywhere works in terms of urls or paths, but I don't know what it is.

thanks very much,

Could you show the full traceback message?

hello pafk,

I am sharing the full traceback message here: https://www.pythonanywhere.com/user/afduggirala/shares/6311ea78c7d34415b4940b032aef4844/

thank you,

Looks like django is missing settings. Where is your settings file located and how do you run your tests?

My settings file is in ~/yogasite/yogasite/settings.py, where ~/yogasite/ is my main project folder, I think this is the standard way a Django project is organized.

I run my tests by doing "python manage.py test". I am using a virtual environment inside the same main project folder as you can see,

thank you,

Are you correctly referencing you settings file in your wsgi file? See https://help.pythonanywhere.com/pages/DeployExistingDjangoProject/

I have checked out that link and my setup looks good. I actually created this app by using the PA wizard. I have only modified my wsgi.py file to work with environment variables.

Out of my 7 tests, 5 are failing, don't know exactly why. The ones failing are ones related to my pages app. Maybe the failures are coming from some issue with the way paths are constructed? or exist in my PA app?

These are the 5 tests that are failing.

class HomepageTests(SimpleTestCase):
    def setUp(self):
        url = reverse("home")
        self.response = self.client.get(url)

    def test_url_exists_at_correct_location(self):
        self.assertEqual(self.response.status_code, 200)

    def test_homepage_template(self):
        self.assertTemplateUsed(self.response, "home.html")

    def test_homepage_contains_correct_html(self):
        self.assertContains(self.response, "home page")

    def test_homepage_does_not_contain_incorrect_html(self):
        self.assertNotContains(self.response, "Hi there! I should not be on the page.")

    def test_homepage_url_resolves_homepageview(self):
        view = resolve("/")
        self.assertEqual(view.func.__name__, HomePageView.as_view().__name__)

thank you,

How are you setting the SECRET_KEY specifically? You mention environment variables, is it coming from one? If so, have you sourced the .env file where it's defined into the Bash shell where you're running the Django unit tests?

Thank you Giles. I had forgotten about sourcing the environment variables in the console, thanks again,

I'm glad it helped!