【问题标题】:Unit Test not running单元测试未运行
【发布时间】:2012-11-29 13:07:02
【问题描述】:

我遇到了一些单元测试。

这是我能想到的最简单的例子:

#testito.py
import unittest

class Prueba(unittest.TestCase):

    def setUp(self):
        pass
    def printsTrue(self):
        self.assertTrue(True)

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

问题是,运行这个没有效果:

$ python testito.py 

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

我摸不着头脑,因为我看不出上面的代码有任何问题。 现在发生了几次测试,我真的不知道下一步该怎么做。 有什么想法吗?

【问题讨论】:

    标签: python unit-testing


    【解决方案1】:

    默认情况下,只运行名称以test 开头的函数:

    class Prueba(unittest.TestCase):
    
        def setUp(self):
            pass
        def testPrintsTrue(self):
            self.assertTrue(True)
    

    来自unittest basic example

    通过子类化unittest.TestCase 创建一个测试用例。这三个单独的测试使用名称以字母test 开头的方法定义。此命名约定告知测试运行程序哪些方法代表测试。

    【讨论】:

    • 只是徘徊为什么 testPrints 而不是 test_prints
    • @FooBarUser:我坚持原始问题的现有命名约定。
    • @ScottSkiles:你必须定义你自己的TestLoader 子类,覆盖getTestCaseNames() methodcurrent implementation 使这更简单一些;您还可以在现有加载程序上设置testMethodPrefix attribute
    • @ScottSkiles:有些helper functions in unittest.loader 就是这么做的。
    • @ScottSkiles:提醒TestCase 上的方法未运行,您必须自己进行分析;枚举类上不匹配白名单且不匹配模式的所有方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2018-11-29
    • 1970-01-01
    相关资源
    最近更新 更多