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 Test our Writer model

How do I run a test on a Django model which tests for a value in the database?

The task is in the Django Basics course and reads: "Now add a test that creates an instance of the Writer model and, using self.assertIn, make sure the email attribute is in the output of the mailto() method."

My code is this:

class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''
    def test_writer_creation(self):
        writer = Writer.objects.create(
            name = "Bruce Whealton",
            email = "bruce@example.com",
            bio = "Here is a short bio about bruce for testing purposes."
        )

self.assertIn('bruce@example.com', writer.mailto())

I was thinking that the mailto function just returns a string, so I want to assert that my email is in the output. This isn't working though. Here is the code for the model.

class Writer(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    bio = models.TextField()

    def __str__(self):
        return self.name

    def mailto(self):
        return '{} <{}>'.format(self.name, self.email)

5 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Bruce;

In your assertion you want to use writer.email instead of your email address, and check the indentation of your self.assertIn() statement.

Happy coding,
Ken

Chris Grazioli
Chris Grazioli
31,225 Points

Where does the Writer model come from? models.py only has courses and steps?

Chris Grazioli
Chris Grazioli
31,225 Points

Kenneth Love

from django.test import TestCase
from .models import Writer


class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''
    def test_writer_creation(self):
        writer=Writer.objects.create(
            self.name="Chris Grazioli"
            self.email="chrisgrazioli@gmail.com"
            self.bio=" a bit of bio"
            )
        self.assertIn(self.email, self.mailto())

Its been a while since unitTests what am I doing wrong here

from django.test import TestCase
from .models import Writer


class WriterModelTestCase(TestCase):
    '''Tests for the Writer model'''
    def test_writer_creation(self):
        writer=Writer.objects.create(
            self.name="Chris Grazioli"
            self.email="chrisgrazioli@gmail.com"
            self.bio=" a bit of bio"
            )
        self.assertIn(writer.email, writer.mailto())

This doesn't seem to work for me either, so I'm lost now

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You don't need self when providing an argument to create(). It doesn't make sense, anyway, because self there would refer to the TestCase, not the Writer instance.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher
Writer.objects.create(
    name="Chris Grazioli",
    email="chrisgrazioli@example.com",
    bio="a bit of bio"
)