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

Peter Otten __peter__ at web.de
Sun Feb 7 04:52:25 EST 2016


Anubhav Yadav wrote:

>> 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!
>>
> 
> Hi,
> 
> Thanks a lot for replying back. I should do something like this right?
> truth_value = yellow_temperature_simulation() <= 100.5 and
> yellow_temperature_simulation() >= 100.0
> self.assertTrue(truth_value)
> 
> I just want to confirm if I get the concept right?

No, you want to check all constrainsts for one value. The way you write it 
above you check if one value is below 100.5 and another is above 100.0.

In your previous post you state

> when my program is in the yellow state, it should generate the numbers
> from 100.0-100.5
> and 102.5-103.0

The "and" is a bit misleading as you want to allow values that are in the 
first *or* the second intrval. When you spell that in Python it becomes 

temperature = yellow_temperature_simulation()
self.assertTrue(
    (100.0 <= temperature < 100.5) 
    or (102.5 <= temperature < 103.0))

The extra parentheses aren't required.
If you don't want half-open intervals replace < with <=.



More information about the Tutor mailing list