python unitest自动化框架

以下举一个最简单的unitest实例,包含备注,自己拉取代码运行一次就知道原理了

python 复制代码
import unittest
import os

class TestSample(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print('整个测试类只执行一次')

    def setUp(self) -> None:
        print("每个测试开始前执行一次")

    def test_equal(self):
        self.assertEqual(1,1)

    def test_no_equal(self):
        self.assertNotEqual(1,2)

    def tearDown(self) -> None:
        print('每个测试结束后执行一次')

    @classmethod
    def tearDownClass(cls) -> None:
        print('整个个测试执行一次')



if __name__ == '__main__':
    # unittest.main()
    #添加用例集
    suite = unittest.defaultTestLoader.discover(os.path.join(os.path.dirname(__file__)), pattern='*.py',top_level_dir=os.path.dirname(__file__))
    #执行用例,默认函数名开头为test的用例
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run()

上面有前置函数,后置函数,通称为测试夹具;

测试结果报告默认在控制台输出,如果想要有html的测试报告,可以用HTMLTestRunner;以下附上对应的git代码示例

Mr_wilson_liu / Python unitest实例 · GitCode

效果展示:

相关推荐
二哈喇子!3 小时前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!3 小时前
空指针异常
开发语言
咚为3 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
jaray3 小时前
PyCharm 2024.3.2 Professional 如何更换 PyPI 镜像源
ide·python·pycharm·pypi 镜像源
%xiao Q3 小时前
GESP C++五级-202406
android·开发语言·c++
Psycho_MrZhang3 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
Traced back3 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
sin22013 小时前
MyBatis的执行流程
java·开发语言·mybatis
web3.08889993 小时前
1688图片搜索API,相似商品精准推荐
开发语言·python
二哈喇子!3 小时前
JAVA环境变量配置步骤及测试(JDK的下载 & 安装 & 环境配置教程)
java·开发语言