unittest的测试用例执行时都可以设置setup、teardown,用来初始化测试开始和测试结束关闭,例如:

import unittest

class MyTestCase(unittest.TestCase):

    def setUp(self):

        print("开始打开浏览器")

    def test_one(self):
print("第一个测试用例的运行") def test_two(self):
print("第二个测试用例的运行") def tearDown(self):
print("开始关闭浏览器") if __name__ == '__main__':
unittest.main()
运行结果:

可以看出每个测试用例执行,都会调用一次setup和teardown,如果涉及用例数量增加,那么这种方法就不适合了。可以使用以下方法:

  

import unittest

class MyTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("开始打开浏览器") def test_one(cls):
print("第一个测试用例的运行") def test_two(cls):
print("第二个测试用例的运行") @classmethod
def tearDownClass(cls):
print("开始关闭浏览器") if __name__ == '__main__':
unittest.main() 运行结果:
 

  可以看出用例的执行只执行一次setupclass 和一次teardown。

unittest学习3-测试组件setup、teardown的更多相关文章

  1. python selenium unittest Fixture(setUp/tearDown)笔记

    Fixture用途: 1.做测试前后的初始化设置,如测试数据准备,链接数据库,打开浏览器等这些操作都可以使用fixture来实现 2.测试用例的前置条件可以使用fixture实现 Fixture使用: ...

  2. 学习-Pytest(三)setup/teardown

    1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...

  3. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  4. Pytest权威教程16-经典xUnit风格的setup/teardown

    目录 经典xUnit风格的setup/teardown 模块级别setup/teardown 类级别setup/teardown 方法和函数级别setup/teardown 返回: Pytest权威教 ...

  5. unittest学习

    unittest的四大特点 TestCase:测试用例.所有的用例都是直接继承与UnitTest.TestCase类. TestFixture:测试固件.setUp和tearDown分别作为前置条件和 ...

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

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

  7. 《带你装B,带你飞》pytest修仙之路3 - setup/teardown

    1. 简介 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次.当然还有更高级一点的setupClass和teardownClass ...

  8. unittest学习4-跳过用例执行

    unittest支持跳过单个测试方法,甚至整个测试用例,还支持将测试用例标记为“测试失败” 基本跳过如下: import unittestimport requests,sys class MyTes ...

  9. 『心善渊』Selenium3.0基础 — 27、unittest跳过测试的使用

    目录 1.什么是跳过测试 2.常用的跳过测试方法和装饰器 3.跳过测试示例 4.TestCase.skipTest()方法 1.什么是跳过测试 当测试用例写完后,有些模块有改动时候,会影响到部分用例的 ...

随机推荐

  1. 启动zabbix-server/agent报错:cannot open "/etc/zabbix/logs/zabbix_server.log": [13] Permission denied

    注:该报错解决方式同样适用于zabbix-agent 启动zabbix-server报错信息如下: 2月 27 16:52:44 localhost.localdomain zabbix_server ...

  2. maven-设置aliyun远程库

    maven默认的远程库下载起来非常慢,习惯改成aliyun的库. 一.修改maven配置 打开maven配置文件setting.xml,改mirror <mirrors> <mirr ...

  3. CLOUD将excel数据引入单据体

    http://club.kingdee.com/forum.php?mod=viewthread&tid=989239 http://club.kingdee.com/forum.php?mo ...

  4. Eclipse中配置Tomcat容器

    Tomcat 安装与配置 Tomcat是Apache 软件基金会(Apache Software Foundation)核心项目之一,支持最新的Servlet 和JSP 规范.因为Tomcat 技术先 ...

  5. 打包Windowsform项目出现File 'Cognex.VisionPro3D.dll' targeting 'AMD64' is not compatible with the project's target platform 'x86'错误

    错误信息: 个人理解此错误的大概意思是:打包的文件是64位的但是打包后的文件设置的是32位的,就出现冲突了. 解决方案:选择打包程序项目的属性窗口设置TargetPlatform属性为对应的值,本项目 ...

  6. arm汇编笔记

    ARM汇编(非虫笔记) 1.ARM汇编的目的: 分析elf文件的需要. 2.原生程序生成过程. (1)预处理,编译器处理c代码中的预处理指令. gcc -E hello.c -o hello.i (2 ...

  7. js集合

    var list = {};//声明 List[0] = 52;//赋值 List[1] = 57;//赋值

  8. LVS笔试题!

    1.集群分类有哪些及各自实现的目标?高可用:保持系统的稳定,防止系统出现单点故障.负载均衡:比如高并发状态下,多个机器分摊请求,从而达到负载均衡高性能运算集群:比如hadoop分布式计算框架,把多个机 ...

  9. 白面系列 mongoDB

    mongoDB和redis一样,都是noSQL技术之一. redis是Key-Value存储,mongoDB是文档存储. 文档存储一般用类似json的格式存储,存储的内容是文档型的.文档是一组键值(k ...

  10. 使用listView有感

    et listView = new ccui.ListView();this.addChild(listView,9999);listView.setDirection(ccui.ScrollView ...