PyDev 未捕获 unittest.TextTestRunner 输出?
当我从它们所在的模块中以“Python 单元测试”的形式运行单元测试时,PyDev PyUnit 透视图正确地显示了单元测试的输出,并使用以下基本使用模式:
import unittest
class MyTest(unittest.TestCase):
def test_something(self):
pass
if __name__ == '__main__':
unittest.main()
但是,当我从另一个模块导入测试时,如下所示
import unittest
import mypackage.mytests
if __name__ == '__main__':
unittest.main(module=mypackage.mytests)
...。 ..没有运行测试。当我运行与“Python Run”相同的模块或从终端运行相同的模块时,它的行为正确,因此由于某种原因,PyUnit 视角无法正确加载测试。我使用此替代方法得到相同的结果:
import unittest
import mypackage.mytests
tests = unittest.TestLoader().loadTestsFromModule(mypackage.mytests)
unittest.TextTestRunner().run(tests)
是否有另一种方法来导入包含 TestCase 派生类的模块并让 PyDev 捕获测试运行程序的输出?
The PyDev PyUnit perspective correctly displays the output of my unit tests when I run them as "Python unit-test" from the module they live in with this basic usage pattern:
import unittest
class MyTest(unittest.TestCase):
def test_something(self):
pass
if __name__ == '__main__':
unittest.main()
However, when I import tests from another module like so...
import unittest
import mypackage.mytests
if __name__ == '__main__':
unittest.main(module=mypackage.mytests)
...no tests are run. When I run the same module as "Python Run" or from a terminal, it behaves correctly, so for some reason the PyUnit perspective is not loading the tests correctly. I get the same results with this alternative method:
import unittest
import mypackage.mytests
tests = unittest.TestLoader().loadTestsFromModule(mypackage.mytests)
unittest.TextTestRunner().run(tests)
Is there another way to import a module containing TestCase derived classes and get PyDev to capture the output of the test runner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PyDev 不会运行您的 __main__ ,它会自行收集类,因此,您需要将类加载到模块中才能找到它们(并运行 > Python Unittest,或者甚至直接使用 Ctrl+F9 快捷键——在这种情况下它不会显示类,但在 Ctrl+F9 之后直接按 Enter 应该可以运行最新 PyDev 中模块中的所有测试。
例如:
如果您有多个并且 TestCase 类具有相同的名称,您需要执行以下操作:
在这种情况下,您可能会更好地创建一个简单的帮助器来从模块加载所有类并放置子类当前模块中的 TestCase 具有不同的名称(应该可以通过模块中的 dir/getattr 直接执行此操作)。
不过,请注意,在 PyDev 中,您可以选择多个文件/文件夹并按 > 运行Python unittest 它将运行它在模块中(或递归地在目录中)找到的所有测试,因此,根据您的用例,这可能已经足够了。
PyDev will not run your
__main__
, it'll collect the classes itself, so, you need to have your classes loaded in the module for it to find them (and do a run as > Python Unittest, or even use the Ctrl+F9 shortcut directly -- it won't show classes in that case, but pressing Enter directly after the Ctrl+F9 should work to run all the tests in the module in the latest PyDev).e.g.:
If you had multiple and the TestCase classes had the same name, you'd need to do something as:
In which case you'd probably be better by creating a simple helper to load all the classes from a module and put subclasses of TestCase in the current module with different names (should be straightforward doing this through a dir/getattr in the module).
Still, note that in PyDev you may select multiple files/folder and do a run as > Python unittest and it'll run all the tests it finds in the module (or recursively in a directory), so, that may already be enough for you depending on your use case.