Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Testing Be Assertive Quantitative Assertions

"MoveTests has no attribute 'rock'"

Thanks in advance for your help! I'm trying to follow the video and I believe my script is identical to Kenneth's, however, when i run the script i get the errors:

Traceback (most recent call last):
File "tests.py", line 21, in test_equal
self.assertEqual(self.rock, moves.Rock())
AttributeError: 'MoveTests' object has no attribute 'rock'

Traceback (most recent call last):
File "tests.py", line 21, in test_equal
self.assertEqual(self.rock, moves.Rock())
AttributeError: 'MoveTests' object has no attribute 'rock'

see below for my code:

import unittest

import moves

class MoveTests(unittest.TestCase): def setUp(self): self.rock = moves.Rock() self.paper = moves.Paper() self.scissors = moves.Scissors()

class MoveTests(unittest.TestCase): def test_five_plus_five(self): assert 5 + 5 == 10

def test_one_plus_one(self):
    assert not 1 + 1 == 3

def test_equal(self):
    self.assertEqual(self.rock, moves.Rock())

def test_not_equal(self):
    self.assertNotEqual(self.rock, self.paper)

if name == 'main': unittest.main()

1 Answer

Your only issue is that you are creatign MoveTests twice. It won't call the setUp from the first class from the second class. You'll simply need to combine the two classes.

import unittest
import moves

class MoveTests(unittest.TestCase):

    def setUp(self):
        self.rock = moves.Rock()
        self.paper = moves.Paper()
        self.scissors = moves.Scissors()

    def test_five_plus_five(self):
        assert 5 + 5 == 10

    def test_one_plus_one(self):
        assert not 1 + 1 == 3

    def test_equal(self):
        self.assertEqual(self.rock, moves.Rock())

    def test_not_equal(self):
        self.assertNotEqual(self.rock, self.paper)

if name == 'main':
    unittest.main()

That worked, thanks!