Axblert
Axblert

Reputation: 576

AssertionError during test without clear error message

this is probably something really simple.

I have written this class

class Pants :
    def __init__(self, pants_color, waist_size, length, price):
        self.color = pants_color
        self.waist_size = waist_size
        self.length = length
        self.price = price

    def change_price(self, new_price):
        self.price = new_price

    def discount(self, discount):
        self.price = self.price * (1 - discount)

and I am running these tests on it:

def check_results():
    pants = Pants('red', 35, 36, 15.12)
    assert pants.color == 'red'
    assert pants.waist_size == 35
    assert pants.length == 36
    assert pants.price == 15.12

    pants.change_price(10) == 10
    assert pants.price == 10 

    assert pants.discount(.1) == 9

    print('You made it to the end of the check. Nice job!')

check_results()

For some reason I keep seeing an error Message without an actual error, it just says AssertionError:

    ---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-50abebbadc01> in <module>()
     13     print('You made it to the end of the check. Nice job!')
     14 
---> 15 check_results()

<ipython-input-5-50abebbadc01> in check_results()
      9     assert pants.price == 10
     10 
---> 11     assert pants.discount(.1) == 9
     12 
     13     print('You made it to the end of the check. Nice job!')

AssertionError: 

Upvotes: 2

Views: 317

Answers (2)

mousetail
mousetail

Reputation: 8010

assert will only print the error message you give, for example:

assert pants.discount(.1) == 9, "Pants discount should be {}, was {}".format(9, pants.discount(0.1))

will give the error

AssertionError: Pants discount should be 9, was None

It is recommended to put a message for every assert statement. Alternatively, you can use inherit from UnitTest from the unittest module, and use the speciality assertEquals method, which has built-in pretty error printing.

Upvotes: 2

Axblert
Axblert

Reputation: 576

Yes apparently I had to add a return instead of just setting the new price. I'll share this none the less in case anotherone encouters it

 def discount(self, discount):
    return self.price * (1 - discount)

Upvotes: 0

Related Questions