Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time View Tests

Brian Anstett
Brian Anstett
5,831 Points

Python Manage.py test: only running 1 test

Hello all, After typing up my second class for my second test, I noticed the second test wasn't passing or failing but rather just not be tested as indicated in the "Ran # test in XXX" from the output. Has this happened to anyone before? Did I make a mistake somewhere?

from django.test import TestCase
from django.core.urlresolvers import reverse
from django.utils import timezone
import time

# Create your tests here.
from .models import CourseModel, Step

class CourseModelTests(TestCase):
    def testCourse(self):
        course = CourseModel.objects.create(
            title="Python Regular Expressions",
            description= "Learn to write regular expressions in Python"
        )
        self.assertEquals(course.title, "Python Regular Expressions")
        self.assertEquals(course.description,
                          "Learn to write regular expressions in Python")


class CourseViewsTest(TestCase):
    def setUp(self):
        self.course = CourseModel.objects.create(
            title="Python Testing",
            description="Learn to write tests in Python"
        )
        self.course2 = CourseModel.objects.create(
            title="new Course",
            description="A new Course"
        )
        self.step = Step.onjects.create(
            title="Introduction to Doctests",
            description="learn to write tests in your docstrings",
            course=self.course
        )
    def courseListViews(self):
        resp = self.client.get(reverse('pythonCourses:listTest'))
        self.assertEquals(resp.status_code, 200)
        self.assertIn(self.course, resp.context['coursesTest'])

brian@Courses$ python manage.py test Creating test database for alias 'default'...

.

Ran 1 test in 0.001s

OK Destroying test database for alias 'default'...

2 Answers

Joseph DePrey
Joseph DePrey
3,183 Points

I ran into this problem when first learning Django and it took me forever to figure out. Kenneth briefly mentions it in the video, but it's easy to miss.

All tests must be prefixed with the word 'test'. As in, def testCourseListViews

I had the same issue as well. The number of tests ran on the program shown in the terminal is basically equal to the number of 'def test-something' we've written in the testModels. To make things a more easy to understand, try commenting out parts of the code and run the tests, this should help you understand how many tests are running and which are they.