python接口自动化测试 - unittest框架suite、runner详细使用

简介: python接口自动化测试 - unittest框架suite、runner详细使用

test suite

  • 测试套件,理解成测试用例集
  • 一系列的测试用例,或测试套件,理解成测试用例的集合和测试套件的集合
  • 当运行测试套件时,则运行里面添加的所有测试用例

test runner

  • 测试运行器
  • 用于执行和输出结果的组件

test suite、test runner基础使用

单元测试类

 # 创建单元测试类,继承unittest.TestCase
  class testCase(unittest.TestCase):
 
      # 测试case
      def test_01(self):
          print("test01")
 
      def test_03(self):
          print("test03")

    def test_04(self):
         print("test04")

     def test_05(self):
         print("test05")

主函数

 if __name__ == '__main__':
      # 实例化测试套件
      suite = unittest.TestSuite()
      # 实例化第二个测试套件
      suite1 = unittest.TestSuite()
      # 添加测试用例 - 方式一
      suite.addTest(testCase('test_03'))
      suite.addTest(testCase('test_01'))
      suite1.addTest(testCase('test_03'))
     suite1.addTest(testCase('test_01'))
     # 添加测试用例 - 方式二
     testcase = (testCase('test_05'), testCase('test_04'))
     suite.addTests(testcase)
     # 测试套件添加测试套件
     suite.addTest(suite1)
     # 实例化TextTestRunner类
     runner = unittest.TextTestRunner()
     # 运行测试套件
     runner.run(suite)

运行结果

 test03
  test01
  test05
  test04
  test03
  test01
  ......
  ----------------------------------------------------------------------
  Ran 6 tests in 0.000s

 OK

包含知识点

  • 使用测试套件时,测试用例的执行顺序可以自定义,按照添加的顺序执行
  • 有两种添加测试用例的方式,推荐方式二,代码更少更快捷
  • addTests(tests) ,传入的 tests 可以是list、tuple、set
  • 添加的测试用例格式是:单元测试类名(测试用例名)
  • 使用测试套件执行测试用例的大致步骤是:实例化TestSuite - 添加测试用例 - 实例化TextTestRunner - 运行测试套件
  • 测试套件也可以添加测试套件

测试用例批量执行

单元测试类文件

前三个文件是包含了单元测试类的文件,第四个文件是负责运行所有单元测试类,不包含测试用例

列举某个单元测试类文件代码

 # 创建单元测试类,继承unittest.TestCase
  class testCase02(unittest.TestCase):
 
      # 测试case
      def test_07(self):
          print("testCase02 test07")
 
      def test_06(self):
         print("testCase02 test06")

     def test_11(self):
         print("testCase02 test11")

test_run.py 代码

批量运行测试用例方式一:

 import unittest
  from learn.unittestLearning import test_case02
  from learn.unittestLearning.test_case03 import testCase03
 
 if __name__ == '__main__':
      # 通过模块
      testcase02 = unittest.TestLoader().loadTestsFromModule(test_case02)
      # 通过单元测试类
      testcase03 = unittest.TestLoader().loadTestsFromTestCase(testCase03)
     # 通过模块字符串
     testcase04 = unittest.TestLoader().loadTestsFromName('learn.unittestLearning.test_case04')
     # 测试用例集
     tests = [testcase02, testcase03, testcase04]
    # 创建测试套件
     suite = unittest.TestSuite(tests)
     # 运行测试套件
     unittest.TextTestRunner(verbosity=2).run(suite)

包含知识点

  • loadTestsFromTestCase(testCaseClass) :testCaseClass输入单元测试类,但需要先import
  • loadTestsFromModule(module, pattern=None) :module输入单元测试类所在模块,也需要import
  • loadTestsFromName(name, module=None) :name是一个string,需满足以下格式: module.class.method ,可以只到输入到class
  • verbosity :表示测试结果的信息详细程,一共三个值,默认是1
  • 0 (静默模式):你只能获得总的测试用例数和总的结果 比如 总共100个 失败20 成功80
  • 1 (默认模式):非常类似静默模式 只是在每个成功的用例前面有个 .  每个失败的用例前面有个 F
  • 2 (详细模式):测试结果会显示每个测试用例的所有相关的信息

批量运行测试用例方式二(推荐!!):

 import unittest

 if __name__ == '__main__':
     # 需要运行的单元测试文件目录
     test_path = './'
     # 实例化defaultTestLoader
     discover = unittest.defaultTestLoader.discover(start_dir=test_path, pattern="test_case*.py")
     # 运行测试用例集
     unittest.TextTestRunner().run(discover)

优点:是不是简洁。。是不是很快??只需三行代码!!

包含知识点

  • start_dir :写需要运行的单元测试文件目录
  • pattern :单元测试文件的匹配规则,默认是 test*.py ,可根据自己的命名规则修改此正则
  • discover()方法可自动根据测试目录start_dir 匹配查找测试用例文件 test*.py ,并将查找到的测试用例组装到测试套件,因此可以直接通过 run() 方法执行 discover

批量执行测试用例的结果

 testCase02 test06
  testCase02 test07
  testCase02 test11
  testCase03 test05
  testCase03 test08
  testCase03 test12
  testCase04 test02
  testCase04 test04
 testCase04 test13
 .........
 ----------------------------------------------------------------------
 Ran 9 tests in 0.000s

 OK
相关文章
|
14天前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
118 1
|
19天前
|
存储 关系型数据库 测试技术
玩转n8n测试自动化:核心节点详解与测试实战指南
n8n中节点是自动化测试的核心,涵盖触发器、数据操作、逻辑控制和工具节点。通过组合节点,测试工程师可构建高效、智能的测试流程,提升测试自动化能力。
|
2月前
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
12天前
|
机器学习/深度学习 人工智能 测试技术
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
EdgeMark是一个面向嵌入式AI的自动化部署与基准测试系统,支持TensorFlow Lite Micro、Edge Impulse等主流工具,通过模块化架构实现模型生成、优化、转换与部署全流程自动化,并提供跨平台性能对比,助力开发者在资源受限设备上高效选择与部署AI模型。
136 9
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
|
9天前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
96 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
9天前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
149 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
19天前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
304 0
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
126 18
|
27天前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
94 0
|
19天前
|
运维 Linux 网络安全
自动化真能省钱?聊聊运维自动化如何帮企业优化IT成本
自动化真能省钱?聊聊运维自动化如何帮企业优化IT成本
49 4

推荐镜像

更多