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
Simon Amz
4,606 PointsMethods for tests (mandatory)
Hi there,
1) I have a little question about tests.
Here is the code which doesn't work and sends me an error: line 35 - AttributeError: 'StepModelTests' object has no attribute 'course'
class StepModelTests(TestCase):
def set_up(self):
self.course = Course.objects.create(
title='Python Basics',
description='Learning Python for beginners'
)
def test_step_creation(self):
step = Step.objects.create(
title='Python functions',
description='Syntax of python functions',
course=self.course
)
self.assertIn(step, self.course.step_set.all())
2) After many test, I just notice, that it works, only if I declared the Course object with the method setUp instead of set_up:
class StepModelTests(TestCase):
def setUp(self):
self.course = Course.objects.create(
title='Python Basics',
description='Learning Python for beginners'
)
def test_step_creation(self):
step = Step.objects.create(
title='Python functions',
description='Syntax of python functions',
course=self.course
)
self.assertIn(step, self.course.step_set.all())
I wanted to know if there was special method, and why my own method doesn't work.
3) Finally before finding this solution, I put everything in only one method and it works too (but it is concise and clear), here is the code:
class StepModelTests(TestCase):
def test_step_creation(self):
course = Course.objects.create(
title='Python Basics',
description='Learning Python for beginners'
)
step = Step.objects.create(
title='Python functions',
description='Syntax of python functions',
course=course
)
self.assertIn(step, course.step_set.all())
What is the best practise between all infos in only one method or two methods, one for set up and one for the check (2. or 3.)?
Thanks for your feedback
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsIf there is code that would need to be repeated for setting up multiple tests, then the DRY approach is to use the setUp method docs. The camelCase name is a legacy left before lowercase_with_underscores became standardized by PEP008
If the code used in the set up is only used in one test, there isn't a reason to use setUp for one test.
Simon Amz
4,606 PointsHi Chris thanks.
So we have to use this standard of method ("setUp", in camelCase standard")in a majority of cases.
But when we have only one declaration of attribute to do we can do everything in one unique function, and not write a setUp function, right?
Chris Freeman
Treehouse Moderator 68,468 PointsCorrect. The setUp method is completely optional.