Python中的assertRaises函数存在问题

时间:2010-05-25 11:23:01

标签: python assertraises

我正在尝试运行以下测试

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

,其中

test.testBasic()

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))

其中prime是一个函数,

但在

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

lambda函数之后不会抛出异常 测试

def testBasic_helper(self):

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

失败 有人能解决这个问题吗?


我会再次尝试解释我的问题,因为在我的第一篇文章中我没有很好地解释它 所以这是源头 is_prime(x)只是检查素数的函数

def is_prime(数字):     PRIME,NOT_PRIME = not is_prime.broken,is_prime.broken     如果数字== 2:         返回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

  

类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))

这里是Test,我们可以说测试测试程序是一个测试 - 测试测试

class MetaProgrammingTest(unittest.TestCase):     def test_decorator(self):         来自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()
我不能改变第二次测试 - 测试的测试 所以表达式self.assertRaises(Exception,lambda:unit_test.testBasic()) 必须保持不变 我的问题是当testBasic()函数失败时,例如我们有 self.assertFailIf(is_prime(2)),self.assertfailIf抛出异常,unit_test.testBasic() 抛出相同的异常到上部范围,但lambda:unit_test.testBasic()不会抛出异常 支票 self.assertRaises(Exception,lambda:unit_test.testBasic()) 失败 我的问题是如何使testBasic()函数使lambda:unit_test.testBasic()能够完成exceotion并且selfassertRaises不会失败,任何想法?

1 个答案:

答案 0 :(得分:1)

我是否理解你是因为你问为什么testBasic_helper方法在测试运行时不会引发异常? 我不明白为什么它应该,is_prime应该返回一个bool值,测试检查失败。如果测试失败,它就会失败,你的代码和unittest框架都不会抛出异常。

P.S。 “unit_test.testBasic”已经是可调用的,“lambda:unit_test.testBasic()”是多余的,所以你可以编写self.assertRaises(Exception,unit_test.testBasic)。