python - 是否可以运行所有单元测试?

标签 python unit-testing python-unittest

我有两个模块,其中包含两个不同的类及其对应的测试类。

 foo.py
 ------
 class foo(object):
     def fooMethod(self):
         // smthg

 bar.py
 ------
 class bar(object):
     def barMethod(self):
         // smthg

 fooTest.py
 ------
 class fooTest(unittest.TestCase):
     def fooMethodTest(self):
         // smthg

 barTest.py
 ------
 class barTest(unittest.TestCase):
     def barMethodTest(self):
         // smthg

在任何测试和源模块、文件中,我删除 if __name__ == "__main__": 因为增加了一致性并遵守面向对象的意识形态。

就像在 Java 单元测试中一样,我正在寻找创建一个模块来运行所有单元测试。例如,

 runAllTest.py
 -------------
 class runAllTest(unittest.TestCase):
    ?????

 if __name__ == "__main__":
    ?????

我寻找搜索引擎但没有找到任何教程或示例。有可能这样做吗?为什么?或如何?

注意:我在 Windows 机器上使用 eclipse 和 pydev 发行版。

最佳答案

基于内置的 python unittest 模块运行单元测试时,在项目运行的根级别

python -m unittest discover <module_name>

对于上面的具体例子,运行即可

python -m unittest discover .

https://docs.python.org/2/library/unittest.html

关于python - 是否可以运行所有单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31556718/

相关文章:

python - 有没有办法从类范围引用对象的基类 __class__ ?

python 2.7 : Thread local storage's instantiation when first accessed?

java - 将模拟对象提供给另一个模拟对象构造函数?

unit-testing - 一种方法的正面和负面单元测试是否应该有单独的测试?

python - 使用 python unittest,我如何断言报告的错误给出了特定消息?

python - 在子进程中使用 * 通配符的命令问题

python - 操作错误: Can't connect to local MySQL server through socket

Python 单元测试。如何在测试用例之间添加一些休眠时间?

python单元测试补丁模拟方法不返回返回值

python - 单元测试sqlalchemy BinaryExpression