断言整数在范围内

时间:2014-10-24 00:42:43

标签: python unit-testing python-2.7 python-unittest

我正在python中编写一些单元测试,测试是否收到整数。然而,有时这个整数可以关闭1或2,我真的不在乎。基本上我希望能够断言收到的整数在一定范围内,如:

self.assertBetween(998, 1000, my_integer)

有可接受的方法吗?或者我必须做这样的事情:

self.assertTrue(998 <= my_integer)
self.assertTrue(my_integer <= 1000)

修改 到目前为止的答案表明:

self.assertTrue(998 <= my_integer <= 1000)

对于我的2个断言的例子,这有什么好处吗?

4 个答案:

答案 0 :(得分:20)

您可以使用“chained comparison”:

self.assertTrue(998 <= my_integer <= 1000)

答案 1 :(得分:14)

Python有built in function可用于此:assertAlmostEqual

self.assertAlmostEqual(myinteger, 999, delta=1)
# is equivalent to
self.assertTrue(998 <= myinteger <= 1000)
# ... but gives better error messages.

可选参数delta指定与您正在测试的值的允许距离。

答案 2 :(得分:5)

我认为使用assertTrue进行比较是个好主意 - 这样你就失去了FAIL消息中的任何信息:

AssertionError: False is not true

这根本没有帮助,你基本上回到&#34; raw&#34; assert您正在失去很多unittest方法的好处。

我会建议:

创建自己的自定义断言

您可以在其中打印更有意义的消息。例如:

import unittest

class BetweenAssertMixin(object):
    def assertBetween(self, x, lo, hi):
        if not (lo <= x <= hi):
            raise AssertionError('%r not between %r and %r' % (x, lo, hi))

class Test1(unittest.TestCase, BetweenAssertMixin):
    def test_between(self):
        self.assertBetween(999, 998, 1000)

    def test_too_low(self):
        self.assertBetween(997, 998, 1000)

    def test_too_high(self):
        self.assertBetween(1001, 998, 1000)

if __name__ == '__main__':
    unittest.main()

然后你将有以下输出(缩短):

======================================================================
FAIL: test_too_high (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 19, in test_too_high
    self.assertBetween(1001, 998, 1000)
  File "example.py", line 8, in assertBetween
    raise AssertionError('%r is not between %r and %r' % (x, lo, hi))
AssertionError: 1001 is not between 998 and 1000
======================================================================
FAIL: test_too_low (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 16, in test_too_low
    self.assertBetween(997, 998, 1000)
  File "example.py", line 8, in assertBetween
    raise AssertionError('%r is not between %r and %r' % (x, lo, hi))
AssertionError: 997 is not between 998 and 1000
----------------------------------------------------------------------

或使用assertLessEqualassertGreaterEqual

如果您不想要自定义断言(它会添加另一个回溯记录和几行代码):

...
def test_no_custom_assert(self):
    my_integer = 100
    self.assertGreaterEqual(my_integer, 998)
    self.assertLessEqual(my_integer, 1000)
...

assertTrue(998 <= my_integer <= 1000)更长一些(可能比添加自定义断言的时间短一些),但是你仍会得到很好的失败消息(也没有额外的追溯记录):

======================================================================
FAIL: test_no_custom_assert (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 23, in test_no_custom_assert
    self.assertGreaterEqual(my_integer, 998)
AssertionError: 100 not greater than or equal to 998

答案 3 :(得分:1)

self.assertTrue(998 <= my_integer <= 1000)