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. The Way to Home CodeForces - 910A

    4个月前做的一道题,当时不知道为什么,写了一个bfs,直接就超时了. 现在再看这个题目,发现就是一个简单的贪心,每次走最远即可. #include <bits/stdc++.h> usin ...

  2. MySQL之分库分表

    MySQL之分库分表(MyCAT实现)   分库分表介绍 随着微服务这种架构的兴起,我们应用从一个完整的大的应用,切分为很多可以独立提供服务的小应用.每个应用都有独立的数据库. 数据的切分分为两种: ...

  3. P1339 热浪【最短路】

    #include <bits/stdc++.h> #define dbg(x) cout << #x << "=" << x < ...

  4. tmp = 2/4;竟然没有发现的

    我还纠结着单目运算符和双目运算符和乘除的一些优先级什么事情. #include "common.h" #include <stdio.h> #include <s ...

  5. Spring Cloud 5大组件

    服务发现——Netflix Eureka 客服端负载均衡——Netflix Ribbon 断路器——Netflix Hystrix 服务网关——Netflix Zuul 分布式配置——Spring C ...

  6. 使用word时在方块中打钩

    ☑ 方法一: 第一步: 输入:2611 第二步: 选中2611 第三步: 按Alt + X Get :  ☑ 同样:用2610代替2611会得到☐ ☐2610 ☑2611 方法二 1.选择[插入]-- ...

  7. Java并发,synchronized锁住的内容

    synchronized用在方法上锁住的是什么? 锁住的是当前对象的当前方法,会使得其他线程访问该对象的synchronized方法或者代码块阻塞,但并不会阻塞非synchronized方法. 脏读 ...

  8. 常用的H5代码

    1.返回上一页第一次在手机端用到返回上一页的时候,只写了window.history.go(-1):这一句.但是只在安卓手机有效果,兼容苹果手机需要在跳转代码后加上return false:这句.跳转 ...

  9. Failed to restart docker.service: Unit not found 镜像加速

    解决方案 以前的安装残留 重新安装 find / -name "docker*" centos8 添加软件源信息 yum-config-manager --add-repo htt ...

  10. .net Core 配置Centos守护进程Supervisor

    声明: 博客引用来源:https://blog.csdn.net/qq_37997978/article/details/83311177建议看原版,更为详细 介绍: Supervisor( http ...