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

效果展示:

相关推荐
Lupino1 小时前
别再只聊 AI 写代码了:技术负责人要把“变更治理”提到第一优先级
python·docker·容器
Flittly2 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(6)Context Compact (上下文压缩)
python·agent
曲幽13 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
用户83562907805118 小时前
使用 C# 在 Excel 中创建数据透视表
后端·python
码路飞21 小时前
FastMCP 实战:一个 .py 文件,给 Claude Code 装上 3 个超实用工具
python·ai编程·mcp
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪1 天前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户0332126663671 天前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫1 天前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派1 天前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain