Alo*_*dal 230 python unit-testing
在我们的团队中,我们定义了大多数测试用例如下:
一个"框架"类ourtcfw.py:
import unittest
class OurTcFw(unittest.TestCase):
def setUp:
# something
# other stuff that we want to use everywhere
Run Code Online (Sandbox Code Playgroud)
还有很多像testMyCase.py这样的测试用例:
import localweather
class MyCase(OurTcFw):
def testItIsSunny(self):
self.assertTrue(localweather.sunny)
def testItIsHot(self):
self.assertTrue(localweather.temperature > 20)
if __name__ == "__main__":
unittest.main()
Run Code Online (Sandbox Code Playgroud)
当我编写新的测试代码并希望经常运行它并节省时间时,我所做的就是将"__"放在所有其他测试之前.但它很麻烦,让我分心我编写的代码,而且这会产生的提交噪音很烦人.
因此,例如在进行更改时testItIsHot(),我希望能够这样做:
$ python testMyCase.py testItIsHot
Run Code Online (Sandbox Code Playgroud)
并unittest运行只 testItIsHot()
我怎样才能做到这一点?
我试图重写这个if __name__ == "__main__":部分,但由于我是Python的新手,我感到很迷茫并且不断抨击除了方法以外的所有其他东西.
phi*_*hag 271
这可以按照您的建议工作 - 您只需要指定类名:
python testMyCase.py MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
Aja*_*y M 139
如果您组织测试用例,即遵循与实际代码相同的组织,并在同一个包中使用模块的相对导入
您还可以使用以下命令格式:
python -m unittest mypkg.tests.test_module.TestClass.test_method
# In your case, this would be:
python -m unittest testMyCase.MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
Yar*_*kee 58
它可以像你猜的那样运作良好
python testMyCase.py MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以测试testItIsHot:
suite = unittest.TestSuite()
suite.addTest(MyCase("testItIsHot"))
runner = unittest.TextTestRunner()
runner.run(suite)
Run Code Online (Sandbox Code Playgroud)
skq*_*kqr 22
如果您查看unittest模块的帮助,它会告诉您几个允许您从模块运行测试用例类和从测试用例类测试方法的组合.
python3 -m unittest -h
[...]
Examples:
python3 -m unittest test_module - run tests from test_module
python3 -m unittest module.TestClass - run tests from module.TestClass
python3 -m unittest module.Class.test_method - run specified test method
Run Code Online (Sandbox Code Playgroud)
它不需要您将a定义unittest.main()为模块的默认行为.
小智 18
对我有用的是:
cd project_dir
python -m unittest -v path\to\test\testMyCase.py -k my_test_name
Run Code Online (Sandbox Code Playgroud)
-v 用于单元测试详细日志输出。
Ray*_*Luo 12
TL;DR:这很可能会奏效:
python mypkg/tests/test_module.py MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
解释:
方便的方式
python mypkg/tests/test_module.py MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
会工作,但它不言而喻的假设是你已经在你的测试文件中(通常在末尾)有这个传统的代码片段。
python mypkg/tests/test_module.py MyCase.testItIsHot
Run Code Online (Sandbox Code Playgroud)
不方便的方式
python -m unittest mypkg.tests.test_module.TestClass.test_method
Run Code Online (Sandbox Code Playgroud)
将始终有效,而无需您if __name__ == "__main__": unittest.main()在测试源文件中包含该代码片段。
那么为什么第二种方法被认为不方便呢?因为在<在此处插入您的身体部位之一> 中手动键入以点分隔的长路径会很痛苦。在第一种方法中,该mypkg/tests/test_module.py部分可以通过现代 shell 或您的编辑器自动完成。
ron*_*kov 12
如果您想直接从脚本(例如,从 jupyter 笔记本)运行测试,可以执行以下操作以仅运行一个测试:
from testMyCase import MyCase
unittest.main(argv=['ignored', '-v', 'MyCase.testItIsHot'], exit=False)
Run Code Online (Sandbox Code Playgroud)
注意:-v是可选的,仅用于使测试变得冗长。
导入模块的另一个版本:
import testMyCase
unittest.main(argv=['ignored', '-v', 'testMyCase.MyCase.testItIsHot'], exit=False)
Run Code Online (Sandbox Code Playgroud)
小智 6
Maybe, it will be helpful for somebody. In case you want to run only tests from specific class:
if __name__ == "__main__":
unittest.main(MyCase())
Run Code Online (Sandbox Code Playgroud)
It works for me in python 3.6
| 归档时间: |
|
| 查看次数: |
117947 次 |
| 最近记录: |