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

Anubhav Yadav anubhav1691 at gmail.com
Sat Feb 6 11:07:32 EST 2016

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.

Here is my code:

import random
import unittest

red_temperature_min = 99.9
red_temperature_max = 103.0
normal_temperature_min = 101.0
normal_temperature_max = 102.0
yellow_temperature_min = 100.0
yellow_temperature_max = 103.0

def red_temperature_simulation():
    Method to simulate the red temperature condition
    choice = random.randint(0,1)
    if choice:
        return round(random.uniform(red_temperature_min - 5.0,
        return round(random.uniform(red_temperature_max,
                                red_temperature_max + 5.0))

def yellow_temperature_simulation():
    Method to simulate the yellow temperature condition
    choice = random.randint(0,1)
    if choice:
        return round(random.uniform(yellow_temperature_min,
                                    normal_temperature_min - 0.5), 2)
        return round(random.uniform(normal_temperature_max + 0.5,
                                    yellow_temperature_max), 2)

class TestCase(unittest.TestCase):
    def test_red_temperature_simulation(self):
        Method to test the red_temperature_simulation method
        for i in range(100000):
            self.assertLess(red_temperature_simulation(), 100.0) and \
                self.assertGreater(red_temperature_simulation(), 103.0)

    def test_yellow_temperature_simulation(self):
        Method to test the yellow_temperature_simulation method
        for i in range(1000000):
                                 100.0)) and \
                              100.5)) and \
                                 102.5)) and \
            (self.assertLessEqual(yellow_temperature_simulation(), 103.0))

I try to test if a number 99.7 is less than 100.0 and at the same time I
also check if it's greater than 102.5. The sentence itself doesn't make
sense, but somehow the test case for yellow passes, but the test case for
red fails. I have also tried using and instead of or!

I want to test my code, is there a better way of doing it?

Thank You.

More information about the Tutor mailing list