gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:03:04 25 4
gpt4 key购买 nike

我在尝试通过 pudb debugger 调试一些单元测试时遇到了一些问题.

测试在 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.pypython -m pudb.run amodule.py 运行,但这没有区别——没有测试以一种或另一种方式运行。

我是否应该做一些不同的事情来使用 pudb 调试单元测试?

最佳答案

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

from pudb import set_trace; set_trace()

您尝试启动它的方式可能会干扰测试发现和/或不会使用 '__main__'__name__ 运行您的脚本。

关于python - 如何使用 pudb 调试器调试单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com