W
W
WH1T3_B0X2022-03-29 19:02:34
Django
WH1T3_B0X, 2022-03-29 19:02:34

Error running automated test in Django?

Here is my test:

import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question


class QuestionModelTests(TestCase):
#tests.py
    def test_was_published_recently_with_future_question(self):
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

When I enter python manage.py test polls I get this error:
Found 1 test(s).
System check identified no issues (0 silenced).
E
======================================================================
ERROR: DJANGO_tutorial.mysite.polls.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: DJANGO_tutorial.mysite.polls.tests
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "D:\PYCHARM_PROJECTS\DJANGO_tutorial\mysite\polls\tests.py", line 4, in <module>
    from .models import Question
  File "D:\PYCHARM_PROJECTS\DJANGO_tutorial\mysite\polls\models.py", line 9, in <module>
    class Question(models.Model):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\base.py", line 132, in __new__
    raise RuntimeError(
RuntimeError: Model class DJANGO_tutorial.mysite.polls.models.Question doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

I assumed that something was wrong with the Question model, but I did not find anything.
As a last resort, here is the model code:
from django.db import models
from django.utils import timezone
import datetime


# Create your models here.


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

PS. This is me making a web app by Django tutorial

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-03-29
@WH1T3_B0X

An error in the declaration of the application, which is what the error says. Check installed_apps. Perhaps they did not enter the application, perhaps they lost a comma or something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question