【问题标题】:Creating Unittest class inside function not working在函数内部创建 Unittest 类不起作用
【发布时间】:2021-09-13 11:19:40
【问题描述】:

在附加的脚本中为什么 0 个测试用例正在运行

import unittest


def smg():
    def add(x, y):
        return x + y

    class SimpleTest(unittest.TestCase):
        def testadd1(self):
            self.assertEquals(add(4, 5), 9)
        

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

smg()

给予

Ran 0 tests in 0.000s 

有什么办法可以解决,请帮忙

【问题讨论】:

  • Unittest 无法发现函数内的测试类,请将其移出smg()。您是否出于特定原因需要在函数中使用它?
  • 在我的项目中,只有在执行某些操作时我才需要调用 unitest 函数,因此我需要将其放入函数中
  • 测试发现解析你的脚本的文本;它不寻找运行时效果。
  • 如果你想做这样的事情,你将不得不编写自己的代码来收集测试并运行它们。

标签: python python-3.x python-unittest


【解决方案1】:

你可能对unittest.TextTestRunner感兴趣:

将结果输出到流的基本测试运行器实现。

示例usage:

但是,如果您想自定义测试套件的构建,您可以自己完成:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('test_default_widget_size'))
    suite.addTest(WidgetTestCase('test_widget_resize'))
    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite())

为您的案例运行示例。

src.py

def add(x, y):
    print("Add", x, y)
    return x + y

test_src.py

import unittest

from src import add


class SimpleTest(unittest.TestCase):
    def testadd1(self):
        self.assertEqual(add(4, 5), 9)

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

运行测试是正常的

$ python test_src.py  # Using unittest
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
$ pytest -q  # Using pytest
.                                                                                                                                                                                                   
1 passed in 0.06s

现在如果你想通过函数手动调用它。

run_tests.py

import unittest

import test_src


def suite():
    suite = unittest.TestSuite()
    suite.addTest(test_src.SimpleTest('testadd1'))
    return suite


def run():
    runner = unittest.TextTestRunner()
    runner.run(suite())

# run()  # Uncomment if you want to try to run it as a script e.g. <python run_tests.py>

您现在只需导入文件并在需要时调用run()

$ python3
>>> import run_tests
>>> run_tests.run()
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
>>> 

【讨论】:

    【解决方案2】:

    取出smg 函数内部的代码,然后使用python -m unittest &lt;Your Filename&gt;.py 从命令行运行测试。

    您的代码将如下所示:

    
    import unittest
    
    
    def add(x, y):
        return x + y
    
    
    class SimpleTest(unittest.TestCase):
        def testadd1(self):
            self.assertEquals(add(4, 5), 9)
        
    
    if __name__ == '__main__':
        unittest.main()
    

    您还可能收到assertEquals 的弃用警告。您可能希望将其更改为 assertEqual

    【讨论】:

    • 感谢 Salek 的回复,我知道这个方法有效,我的项目只有在执行某些操作时才需要调用 unitest 函数,因此我需要将其放入函数中
    • @Lakpa 然后您可能希望在代码中使用子进程模块,如here 所述。但是您仍然必须删除单元测试的冗余 smg 函数才能工作。
    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多