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

效果展示:

相关推荐
aqi002 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵3 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf4 小时前
Agent 流程编排
后端·python·agent
copyer_xyf5 小时前
Agent RAG
后端·python·agent
copyer_xyf5 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf5 小时前
Agent 记忆管理
后端·python·agent
星云穿梭20 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵20 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏
黄忠1 天前
大模型之LangGraph技术体系
python·llm