[Tutor] Need help with two similar test cases that I have written. One works and the other fails

Danny Yoo danny.yoo at gmail.com
Sat Feb 6 16:34:29 EST 2016

On Feb 6, 2016 12:31 PM, "Anubhav Yadav" <anubhav1691 at gmail.com> wrote:
> Hello Everyone,
> I am trying to write a simple program that generated random numbers based
> on some rules.
> when my program is in the yellow state, it should generate the numbers
> from 100.0-100.5
> and 102.5-103.0. When my program is in the red state, it should generate
> numbers in the range
> less than 99.9 and greater than 103.1.
> I have written two very simple functions, and two unittest.TestCase test
> cases to test the two functions. I somehow feel that both my test cases
> doesn't make any sense, yet one of it passes perfectly and other one does
> not pass.

Hi Anubhav,

Ah!  The assert functions are meant to be used as statements, not as
composable expressions.  If you're familiar with the idea of side effects,
then you need to understand that you should be calling the assert functions
just for their side effects, not for their return value.

If you want to test two conditions, do them as  separate statements.  By
trying to compose them with 'and', the code actually means something
different due to boolean logic short circuiting.

If you have questions, please feel free to ask.  Good luck!

More information about the Tutor mailing list