Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pudb调试器调试单元测试?_Python_Python Unittest_Pudb - Fatal编程技术网

Python 如何使用pudb调试器调试单元测试?

Python 如何使用pudb调试器调试单元测试?,python,python-unittest,pudb,Python,Python Unittest,Pudb,我在调试一些单元测试时遇到了一些问题 python可以很好地运行测试,但是我没有幸用pudb运行它们 我隔离了问题,获得了以下示例代码: class Math: def pow(self, x, y): return x ** y import unittest class MathTest(unittest.TestCase): def testPow23(self): self.assertEquals(8, Math().pow(2, 3

我在调试一些单元测试时遇到了一些问题

python可以很好地运行测试,但是我没有幸用
pudb
运行它们

我隔离了问题,获得了以下示例代码:

class Math:
    def pow(self, x, y):
        return x ** y

import unittest

class MathTest(unittest.TestCase):
    def testPow23(self):
        self.assertEquals(8, Math().pow(2, 3))
    def testPow24(self):
        self.assertEquals(16, Math().pow(2, 4))

if __name__ == '__main__':
    unittest.main()
测试运行正常:

$ python amodule.py 
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
但是如果运行pudb,它会给我输出:

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

OK
我尝试过使用
pudb amodule.py
运行,也尝试过使用
python-m pudb.run amodule.py
运行,但是没有区别——没有任何测试是以这种或那种方式运行的


使用pudb调试单元测试时,我应该做些不同的事情吗?

尝试在代码中的有用行上放置断点:

from pudb import set_trace; set_trace()

您尝试启动脚本的方式可能会干扰测试发现和/或不使用
''的
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,我觉得我还应该提到,大多数测试运行工具将要求您传入一个开关,以防止它捕获标准输出和输入(通常是
-s


因此,记住在使用pytest时运行
pytest-s
,或者在使用Nose时运行
nosetests-s
,在使用Django测试时运行
python manage.py test-s
,或者检查测试运行工具的文档。

您可以通过以下方式更轻松地设置断点:


导入pudb;pu.db

@LukasGraf Nice,成功了,谢谢!请将此作为答案发布,以便我可以给您评分。=)很好,很高兴它成功了:)那么后期调试呢?我曾经调用过类似于
pudb myprogram params
的程序,它在第一个错误中停止。我们可以对
pudb
unittests
进行同样的操作吗?我的意思是在第一个错误处停止,并获得后期调试?@yucer,这将取决于您的测试运行程序。如果您使用pytest,您可以安装此插件,然后运行
pytest--pudb