浮生闲记
2025-04-23
点 赞
0
热 度
3
评 论
0

unittest中的类方法在pycharm中不能执行

文章摘要

摘要

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

用键盘敲击出的不只是字符,更是一段段生活的剪影、一个个心底的梦想。希望我的文字能像一束光,在您阅读的瞬间,照亮某个角落,带来一丝温暖与共鸣。

浮生闲记

intj 建筑师

站长

具有版权性

请您在转载、复制时注明本文 作者、链接及内容来源信息。 若涉及转载第三方内容,还需一同注明。

具有时效性

目录

欢迎来到浮生闲记的站点,为您导航全站动态

26 文章数
3 分类数
1 评论数
5标签数
最近评论
二十七度风

二十七度风


已Mark

访问统计