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 Membership and Other Assertions

So in a unittest, does the assert method run something a lot of times?

I'm asking because in the video, we used the following:

self.assertGreaterEqual(int(self.hand3), 3)

and

self.assertLessEqual(int(self.hand3), 18)

But the thing is, we created the hands as follows:

self.hand1 = dice.Roll('1d2') self.hand3 = dice.Roll('3d6')

So let's say that hand 3 is a 2, 4, and 6 (when the hand is created). Now, you only have a single set of values, 2, 4, 6, and it asserts that this total is less than 19, and higher than 0. But what if you just got lucky? What if on 5% of cases, the hand created has a total value that is maybe a 5, 5, 12. Doesn't that specific case get missed, just because the hand created had a 95% chance of happening?

To put it simply: wouldn't it be possible for the code here to be faulty, but because it runs well 95% of the time, the hand created happened to be part of that 95%, and so the unit test assertion worked?

As I'm writing this, I'm actually starting to realize that maybe we will eventually cover the concept of running tests.py a million times, and that's how the statistics will get sorted out.

Still, since I'm not sure, I'm asking!

Thanks!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The assert is run a single time. If the assertion is a against a random generated value, then yes, to achieve a better confidence of correct code behavior the tests would need to be run many times.

Is it correct that dice.Roll('3d6') is three die with values 1-6? Then the minimum would be 3 (1-1-1) and the max would be 18 (6-6-6). So the only way these assertions would flag an error is if the underlying code within the Roll method actually implemented a die value of less than one or greater than 6.

Thank you for your answer. So basically, in order to properly test it, it would have to indeed be run many times. This was a test to check that it at least does what it's supposed to do in general.

Thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Correct. It can thought of as a "bounds check" that the roll value is valid.