Good day! Trying to write a test for leaving a comment. Did I write it correctly, and what is wrong? How to be right? Prompt not

The test itself:

class CommentAddTest(self): def test_add_comment(self): client = Client() comment = CommentForm(yourname='Alex', email='alex@sobaka.ru', message='Lorem Ipsum Doloren Ipsum...', enable=True) comment.save() self.assertEqual(response.status_code, 200) 

Modelka:

 class CommentForm(models.Model): class Meta: verbose_name = u'ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ' verbose_name_plural = u'ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ' yourname = models.CharField(max_length=124, verbose_name=u'Π’Π°ΡˆΠ΅ имя:') email = models.EmailField(verbose_name=u'Π’Π°Ρˆ email:') message = RichTextField(verbose_name=u'ВСкст коммСнтария:') article_id = models.ForeignKey(Posts) enable = models.BooleanField(default=False) def __str__(self): return "ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ ΠΊ %s" %self.article_id.title 

After starting, it gives the following error:

 ImportError: Failed to import test module: blog.tests Traceback (most recent call last): File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path module = self._get_module_from_name(name) File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name __import__(name) File "/home/kelevra/uroki/django/mysite/blog/tests.py", line 18, in <module> class CommentAddTest(self): NameError: name 'self' is not defined FAILED (errors=1) Destroying test database for alias 'default'... 

The comments themselves are left through the form, or is it better to test it? Or how?

  • one
    You are trying to class CommentAddTest inherit from self . I suspect that there should be CommentAddTest(TestCase) - Sergey Gornostaev
  • That's right, only the jung class for unit tests. - Mr Fix
  • Now he writes django.db.utils.IntegrityError: NOT NULL constraint failed: comments_commentform.article_id_id If I pass in the test `comment = CommentForm (yourname = 'Alex', email='alex@sobaka.ru ', message =' Lorem Ipsum Doloren Ipsum ... ', article_id = 1, enable = True) `Writes ValueError: Cannot assign "1": "CommentForm.article_id" must be a "Posts" instance. - kelevra

0