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?
CommentAddTestinherit fromself. I suspect that there should beCommentAddTest(TestCase)- Sergey Gornostaevdjango.db.utils.IntegrityError: NOT NULL constraint failed: comments_commentform.article_id_idIf I pass in the test `comment = CommentForm (yourname = 'Alex', email='alex@sobaka.ru ', message =' Lorem Ipsum Doloren Ipsum ... ', article_id = 1, enable = True) `WritesValueError: Cannot assign "1": "CommentForm.article_id" must be a "Posts" instance.- kelevra