【问题标题】:Assertion error in unit test单元测试中的断言错误
【发布时间】:2018-02-20 11:51:22
【问题描述】:

我正在开发一个 python 程序来查找一个范围内的所有数字,这些数字是完美的平方,并且数字中所有数字的总和小于 10

我的测试用例显示断言错误

AssertionError: None != [1, 4, 9, 16]

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

为什么会这样

def perfSq(l,u):
    a=[]
    for x in range(l,u+1):
        if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
            a.append(x)
    print a


import unittest
class Test(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testPerfSq(self):
        self.assertEqual(perfSq(1,20) , [1,4,9,16] ) 
if __name__ == '__main__':
    unittest.main()

有什么问题吗?

【问题讨论】:

    标签: python


    【解决方案1】:

    perfSq 返回None。您正在比较 None[1, 4, 9, 16]

    改变

    def perfSq(l,u):
        a=[]
        for x in range(l,u+1):
            if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
                a.append(x)
        print a
    

    def perfSq(l,u):
        a=[]
        for x in range(l,u+1):
            if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10:
                a.append(x)
        print a
        return a
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-14
      • 2018-02-20
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多