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. 2019kali中文乱码

    1.安装KALI2019.4版本后会出现乱码问题 2.更新国内源,使用vim编辑器修改:vim /etc/apt/sources.list添加 #清华大学 [更新源]    deb https://m ...

  2. 如果linux开机没有ip怎么办

    1.vim编辑网卡配置文件,修改如下参数 [root@s25linux tmp]# cd /etc/sysconfig/network-scripts/vim修改此文件,找到如下参数,改为yesONB ...

  3. MarkDown图文编辑系列教程(二)

    一.写在前面 引言 本文是我写的MarkDown系列教程的第二篇,前一篇的地址:MarkDown图文编辑系列教程(一) 读完本篇,你将获得 学会使用markdown语法进行:区块引用(一种常用的引用格 ...

  4. PDF体检报告

    //Title: 个人健康管理分析报告 using System; using System.Collections.Generic; using System.Linq; using System. ...

  5. python接口自动化-requests-toolbelt处理multipart/form-data

    1.requests-toolbelt官方文档:https://pypi.org/project/requests-toolbelt/ 2.环境安装 pip install requests-tool ...

  6. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  7. vue 多组件路由处理方法

    实现页面: 实现效果: 实现代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  8. SQLyog怎么导入mysql数据库

    参考链接:https://jingyan.baidu.com/article/647f0115c5ad9f7f2148a8c6.html

  9. cdn第三方前端依赖架包共享地址

    https://cdnjs.com/ 可在此网站查找你需要的稳定第三方前端依赖架包

  10. [Agc001A/At1979] BBQ Easy - 贪心

    要准备N组食物, 他有2N的食材, 需要两两组成一个食物, 食物的价值是两食材中较小的那个. 问最大总价值是多少 ---------- 考虑到\(ans = (sum - delta)/2\),只需要 ...