unittest中的类方法在pycharm中不能执行
问题描述
unittest中的类方法在pycharm中不能执行的问题说明
代码如下
import unittest
# 整个模块开始前执行
def setUpModule():
print("模块级别开始test module start")
# 整个模块结束时执行
def tearDownModule():
print("模块级别结束test module end")
class MyTest(unittest.TestCase):
# 测试用例类开始前执行
@classmethod
def setUpClass(cls) :
print("类级别开始 test class start" )
# 测试用例类结束时执行
@classmethod
def tearDownClass(cls) :
print("类级别结束 test class end")
# 测试用例开始前执行(以一条测试用例为单位)
def setUp(self) -> None:
print("用例级别开始 test case start")
# 测试用例结束时执行(以一条测试用例为单位)
def tearDown(self) -> None:
print("用例级别结束 test case end")
def test_case1(self):
print("用例 test case1")
def test_case2(self):
print("用例 test case2")
if __name__ == '__main__':
unittest.main()
初始运行结果如下
用例级别开始 test case start
用例 test case1
用例级别结束 test case end
用例级别开始 test case start
用例 test case2
用例级别结束 test case end
Ran 2 tests in 0.001s
OK
Process finished with exit code 0
问题解决
类方法不能执行,在网上查阅资料无果。
首先考虑代码问题,仔细检查后发现无误,于是使用vscode尝试运行,运行结果如下
模块级别开始test module start
类级别开始 test class start
用例级别开始 test case start
用例 test case1
用例级别结束 test case end
.用例级别开始 test case start
用例 test case2
用例级别结束 test case end
.类级别结束 test class end
模块级别结束test module end
Ran 2 tests in 0.001s
OK
发现正确运行,于是考虑是pycharm的版本问题,更换最新版本以及2019版本,均出现这种问题,然后在pycharm的设置中发现猫腻,将Python集成工具中的默认测试运行程序改为unittest运行。
再次运行结果如下
模块级别开始test module start
类级别开始 test class start
用例级别开始 test case start
用例 test case1
用例级别结束 test case end
用例级别开始 test case start
用例 test case2
用例级别结束 test case end
类级别结束 test class end
模块级别结束test module end
Ran 2 tests in 0.001s
OK
Process finished with exit code 0