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 Python Testing Be Assertive Quantitative Assertions

Y B
Y B
14,136 Points

Why do we need assertGreater(x,y) etc... can't we just just assert x>y ?

As per the question. I'm sure what use there is of having multiple type of assert methods. Can't we just set up assert with whatever conditional statement we wish to test?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Extending what Max said...

The built-in command assert throws an AssertionError that does not have built-in handling and your program and test will halt at he error.

Using self.assertGreater(), which comes from unittest.TestCase.assertGreater(), has built in logging and error handling to allow all tests to run to Error, Failure, or Success. By having various assertion classes, the code is make more readable with clear intention of the assertion without having to decode an expression to get the intention. Different assertion classes allow for customization for that assertion type in terms of number and type of arguments and the processing of those arguments.

Max Hirsh
Max Hirsh
16,773 Points

I don't have any way of knowing, but my best guess is that the python unittest code needed to have a regular structure to it, and there are some assert methods that are not simple statements with logical operators (like "AssertRaises"). Rather than have some specific assert methods and one general assert method testing if a logical statement is true/false, it probably made more sense to format everything the same way as its own "assert___" statement.