Python unittest AssertionError - 为什么?

时间:2014-12-27 18:46:24

标签: python

我有一个由我的教授编写的单元测试代码,用于检查我的任务。通过cmd.exe我启动测试python Task303Test.py,它包含如下代码:

"""
NAME: divisable_by_four
PARAMS: string
RETURN: bool
POINTS: 2
"""

import unittest
from Task303 import divisable_by_four

class Task303Test(unittest.TestCase):
    """Testy do zadania 303"""

    def test_simple(self):
        """Prosty test."""
        self.assertTrue(divisable_by_four("32"))
        self.assertFalse(divisable_by_four("33"))
        self.assertFalse(divisable_by_four("-32"))
        self.assertTrue(divisable_by_four("0"))

        for num in range(45000, 47000):
            if num % 4 == 0:
                self.assertTrue(divisable_by_four(str(num)), str(num))
            else:
                self.assertFalse(divisable_by_four(str(num)), str(num))

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

我有divisable_by_four function

import re

def divisable_by_four(string):
    r = re.compile('^(?<!-)(([048]$)|([0-9]*(12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$))')
    m = r.search(string)
    if m and m.group():
        return True
    else:
        return False

但我在测试时遇到错误:

self.assertTrue(divisable_by_four(str(num)), str(num))
AssertionError: 45000

对我来说这很奇怪,因为我认为我已尽我所能做了一切。有人可以告诉我有什么问题吗?也许测试本身是错的?

0 个答案:

没有答案