jeudi 18 juin 2020

Django Does Not Detect Tests Ran 0 tests in 0.000s

I run the python manage.py test and also tried app specific tests for my app blog but it is not detected. Is there an error in my test.py? I don't know why the tests are not being detected.

from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.urls import reverse
from .models import Post

class BlogTests(TestCase):

def setUp(self):
    self.user = get_user_model().objects.create_user(
            username='testuser',
            email='test@email.com',
            password='secret'
            )

    self.post = Post.objects.create(
            title='A good title',
            body='Nice body content',
            author=self.user,
            )

    def test_string_representation(self):
        post = Post(title='A sample title')
        self.assertEqual(str(post), post.title)

    def test_post_content(self):
        self.assertEqual(f'{self.post.title}', 'A good title')
        self.assertEqual(f'{self.post.author}', 'testuser')
        self.assertEqual(f'{self.post.body}', 'Nice body content')

    def test_post_list_view(self):
        response = self.client.get(reverse('home'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Nice body content')
        self.assertTemplateUsed(response, 'home.html')

    def test_post_detail_view(self):
        response = self.client.get('/post/1/')
        no_response = self.client.get('/post/100000/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(no_response.status_code, 404)
        self.assertContains(response, 'A good title')
        self.assertTemplateUsed(response, 'post_detail.html')

Aucun commentaire:

Enregistrer un commentaire