【问题标题】:why is this test case failing even though both the functions in assertEqual are returning the same value?为什么即使 assertEqual 中的两个函数都返回相同的值,这个测试用例也会失败?
【发布时间】:2011-11-29 05:10:51
【问题描述】:

为什么即使两个函数都返回相同的值,测试用例也会失败?

import unittest

def first():
    return 4312
def second():
    return 4312

class testcase(unittest.TestCase):
    def case1(self):
        self.assertEqual(first(),second())


def test():
    print first(),second()
    unittest.main()

我正在执行 test() 函数

我得到的输出是:

Ran 0 tests in 0.000s

OK

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    test()
  File "C:/Users/ashutosh.rd/Desktop/x.py", line 15, in test
  File "C:\Python27\lib\unittest\main.py", line 95, in __init__
    self.runTests()
  File "C:\Python27\lib\unittest\main.py", line 231, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False

【问题讨论】:

  • 对我有用,虽然我必须编辑这个脚本才能让它作为测试运行。请编辑以包含一个有效的实际脚本,以及运行此脚本的完整结果。
  • 到底是怎么失败的?你在跑什么,python 回馈什么是错的?

标签: python unit-testing


【解决方案1】:

您的测试用例没有运行,因为您的测试用例中的方法需要以字母“test”开头(例如“test_me()”)才能被识别为测试用例。将 case1 的名称更改为 test1,您的测试将运行。

【讨论】:

  • 即使我将函数命名为 test_case1,我也会遇到同样的错误
  • 所以你仍然得到“在 0.000 秒内运行 0 次测试”?
  • 还有其他问题。当我运行您的代码时,在 Python 2.7.1 下将“case1”更改为“test_case1”,我得到了所有通过: 4312 4312 。 -------------------------------------------------- -------------------- 在 0.000 秒内运行 1 次测试 OK
猜你喜欢
  • 1970-01-01
  • 2021-08-11
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多