views:

97

answers:

2

Hello,guys! I am trying to run the following test

 self.assertRaises(Exception,lambda:
 unit_test.testBasic())

where

test.testBasic()

is

class IsPrimeTest(unittest.TestCase):
    def assertRaises(self,exception,callable,*args,**kwargs):
        print('dfdf')
        temp = callable
        super().assertRaises(exception,temp,*args,**kwargs)

   def testBasic_helper(self):

        self.failIf(is_prime(2))
        self.assertTrue(is_prime(1))

where prime is a function,and

but in

self.assertRaises(Exception,lambda:
unit_test.testBasic())

the lambda function doesnt throws an exception after the test

def testBasic_helper(self):

   self.failIf(is_prime(2))
   self.assertTrue(is_prime(1))

fails Can somebody offers a solution to the problem?

A: 

Do I understand you correctly in that you're asking why the testBasic_helper method doesn't raise an exception when the test runs? I don't see why it should, the is_prime should return a bool value against which the test checks for failure. If the test fails, it simply fails, neither your code nor the unittest framework throws in exception.

P.S. "unit_test.testBasic" is already a callable, the "lambda: unit_test.testBasic()" is superfleous, so you can just write self.assertRaises(Exception, unit_test.testBasic).

joe
What does "superfleous" mean??
anton.k.
That's a typo, correctly spelled it's "superfluous", meaning it's not needed.
joe
A: 

I'll try to explain my problem ,again ,because in my first post i didn't explain it very well So here is the source is_prime(x) is just a function for checking for prime numbers

def is_prime(number): PRIME, NOT_PRIME = not is_prime.broken, is_prime.broken if number == 2: return PRIME

if not isinstance(number, int) and not isinstance(number, float):
    raise Exception

if number % 2 == 0 or number < 2 or isinstance(number, float):
    return NOT_PRIME

max = int(number**0.5) + 1
for i in range(3, max, 2):
    if number % i == 0:
        return NOT_PRIME
return PRIME

is_prime.broken = False

class IsPrimeTest(unittest.TestCase):

def testBasic_helper(self):

          self.failIf(is_prime(1))
          self.assertTrue(is_prime(2))
          self.failIf(is_prime(4))
          self.failIf(is_prime(6))
          self.assertTrue(is_prime(7))


def testBasic(self):
    return self.testBasic_helper()

def testNegative(self):
    self.failIf(is_prime(-2))
    self.failIf(is_prime(-11 ** 3))

def testNonNumber(self):
    self.assertRaises(Exception,is_prime,'asd')
    self.assertRaises(Exception,is_prime,self.__class__.__name__)

def testRandom(self):

    for i in range(0,10000):
        rand = randint(-10000,10000)
        self.assertEquals(prime(rand),is_prime(rand))

and here is Test,we can say that the test tests the program which is a test - a test over test

class MetaProgrammingTest(unittest.TestCase): def test_decorator(self): from p10 import unit_converter

    def celsius_function(val):
        return val

    self.assertTrue(hasattr(unit_converter(1,2),'__call__'))
    decorated = unit_converter(1.8, 32)(celsius_function)
    self.assertTrue(hasattr(decorated, '__call__'))
    self.assertAlmostEquals(64.4, decorated(val = 18.0))


def test_unit_test(self):
    import p10
    from p10sample import is_prime as is_prime_actual

    unit_test = p10.IsPrimeTest('testBasic')
    unit_test.setUp()

    is_prime_actual.broken = False

    self.assertEquals(None, unit_test.testBasic())
    self.assertEquals(None, unit_test.testNegative())
    self.assertEquals(None, unit_test.testNonNumber())
    self.assertEquals(None, unit_test.testRandom())

    is_prime_actual.broken = True

    self.assertRaises(Exception,lambda: unit_test.testBasic())
    self.assertRaises(Exception, lambda: unit_test.testNegative())
    self.assertRaises(Exception, lambda: unit_test.testRandom())

    unit_test.tearDown()

I,cant change the second test - the test over the test So the expression self.assertRaises(Exception,lambda: unit_test.testBasic()) has to stay unchaged My problem is that when testBasic() function fails ,for example if we have self.assertFailIf(is_prime(2)) ,self.assertfailIf throws an exception,unit_test.testBasic() throws the same exception to the upper scope,but lambda:unit_test.testBasic() doesnt throws the exception and the check self.assertRaises(Exception,lambda: unit_test.testBasic()) fails My question is how to make the testBasic() function so that lambda: unit_test.testBasic() will thorw the exceotion and selfassertRaises won't fail,any ideas??

anton.k.
Don't watch the paragraph def celsius_function(val): return val self.assertTrue(hasattr(unit_converter(1,2),'__call__')) decorated = unit_converter(1.8, 32)(celsius_function) self.assertTrue(hasattr(decorated, '__call__')) self.assertAlmostEquals(64.4, decorated(val = 18.0))i have placed it accedentily
anton.k.