1. # -*- coding:utf-8 -*-
  2. import unittest
  3.  
  4. def runTest(testcaseclass,testcase=[]):
  5. suite = unittest.TestSuite()
  6. for case in testcase:
  7. suite.addTest(testcaseclass(case))
  8. unittest.TextTestRunner().run(suite)
  9.  
  10. class test(unittest.TestCase):
  11.  
  12. def setUp(self):
  13. print 'This is the setup msg'
  14.  
  15. def tearDown(self):
  16. print 'This is the teardown msg'
  17. @classmethod
  18. def setUpClass(cls):
  19. print 'this is the setupclass msg'
  20.  
  21. @classmethod
  22. def tearDownClass(cls):
  23. print 'this is the teardownclass msg'
  24.  
  25. def test1(self):
  26. print 'This is the first testcase'
  27.  
  28. def test2(self):
  29. print 'This is the second testcase'
  30.  
  31. def test3(self):
  32. print 'This is the third testcase'
  33.  
  34. runTest(test,['test2','test3','test1'])
  35.  
  36. 输出如下:
  37. C:\Python27\python.exe "E:/Project/A3A 8 4G/13.py"
  38. ...
  39.  
  40. ----------------------------------------------------------------------
  41.  
  42. this is the setupclass msg
  43. This is the setup msg
  44. This is the second testcase
  45.  
  46. This is the teardown msg
  47.  
  48. This is the setup msg
  49. This is the third testcase
  50. This is the teardown msg
  51. This is the setup msg
  52. This is the first testcase
  53. This is the teardown msg
  54. this is the teardownclass msg
  55.  
  56. Ran 3 tests in 0.000s
  57.  
  58. OK
  59.  
  60. Process finished with exit code 0

总结:

1、setup()和teardown()两个函数在每条测试用例执行时都会进行重复执行一次,该场景针对那些测试用例间有相互影响的场景,才需要在每执行一条新用例时进行一次初使化,执行完毕后再清空所有配置

2、setupclass(cls)和teardownclass(cls)两个函数在一个用例集合执行前只会执行一次,然后再所有用例执行完成后再清空所有配置,此种用法主要用在用例之间相互没有什么影响的场景

【Python】【unittest】unittest测试框架中setup,teardown与setupclass,teardownclass的区别的更多相关文章

  1. selenium中的setUp,tearDown与setUpClass,tearDownClass的区别

    def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() def setUp(self): s ...

  2. Python的Django REST框架中的序列化及请求和返回

    Python的Django REST框架中的序列化及请求和返回 序列化Serialization 1. 设置一个新的环境 在我们开始之前, 我们首先使用virtualenv要创建一个新的虚拟环境,以使 ...

  3. selenium 测试框架中使用grid

    之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...

  4. hibernate validate验证框架中@NotEmpty、@NotbBank、@NotNull的区别

    Hibernate Validator验证框架中@NotEmpty.@NotBlank.@NotNull 的区别 Hibernate Validator验证框架中@NotEmpty.@NotBlank ...

  5. python unittest自动测试框架

    编写函数或者类时进行测试,确保代码正常工作 python  unittest 模块提供了代码测试工具.按照定义测试包括两部分:管理测试依赖库的代码(称为‘固件’)和测试本身. 单元测试用于核实函数的某 ...

  6. Selenium 4 Python的最佳测试框架

    随着Python语言的使用越来越流行,基于Python的测试自动化框架也越来越流行.在项目选择最佳框架时,开发人员和测试人员会有些无法下手.做出选择是应该判断很多事情,框架的脚本质量,测试用例的简单性 ...

  7. Pytest单元测试框架之setup/teardown模块示例操作

    """模块级(setup_module/teardown_module)开始于模块始末,全局的函数级(setup_function/teardown_function)只 ...

  8. 在测试框架中使用Log4J 2

    之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...

  9. Hibernate Validator验证框架中@NotEmpty、@NotBlank、@NotNull 的区别

    Hibernate Validator验证框架中@NotEmpty.@NotBlank.@NotNull的主要使用情况 @NotEmpty  用在集合类上面 @NotBlank   用在String上 ...

随机推荐

  1. [转]Magento2开发教程 - 如何向数据库添加新表

    本文转自:https://www.cnblogs.com/xz-src/p/6920365.html Magento 2具有特殊的机制,允许你创建数据库表,修改现有的,甚至添加一些数据到他们(如安装数 ...

  2. easyui datagrid 动态改变行背景色

    /*根据查询条件查询调度单列表*/ function InitGrid(queryData) { $("#dg").datagrid({ loadMsg: "数据加载中, ...

  3. wei

    <!--PageEndHtml Block Begin-->   JAVASCRIPT:   <script charset="Shift_JIS" src=&q ...

  4. Dijkstra和Prim算法的区别

    Dijkstra和Prim算法的区别 1.先说说prim算法的思想: 众所周知,prim算法是一个最小生成树算法,它运用的是贪心原理(在这里不再证明),设置两个点集合,一个集合为要求的生成树的点集合A ...

  5. Mybatis的枚举处理器

    Mybatis有两个默认枚举处理器 EnumOrdinalTypeHandler EnumTypeHandler 自定义枚举 EnumOrdinalTypeHandler 这个处理器负责将pojo里面 ...

  6. 1.String、StringBuffer与StringBuilder之间区别

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...

  7. OSGI企业应用开发(十二)OSGI Web应用开发(一)

    前面文章中介绍了如何在OSGI应用中整合Spring和Mybatis框架,本篇文章开始介绍如何使用OSGI技术开发Web应用.对于传统的Java EE应用,应用中涉及到的Web元素无非就是Servle ...

  8. Windows7安装 nginx+php 后访问.php文件出现 “No input file specified.” 的解决办法

    在Windows7上安装了Nginx+PHP,参考教程为 https://www.cnblogs.com/anlia/p/5916758.html 启动 nginx 后,在浏览器中输入localhos ...

  9. Android之在linux终端执行shell脚本直接打印当前运行app的日志

    1.问题 我们一般很多时候会需要在ubuntu终端上打印当前运行app的日志,我们一般常见的做法是 1).获取包名 打开当前运行的app,然后输入如下命令,然后在第一行TASK后面的就可以看到包名 a ...

  10. Java web 开发填坑记 2 -如何正确的创建一个Java Web 项目

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72566261 本文出自[赵彦军的博客] Java web 开发填坑记 1-如何正确 ...