• 用法简介:
  • setup_method:仅作用于class用例集中的用例,置于class内,每个用例都会调用一次
  • setup_function:作用于独立的def用例,不可作用于class内的用例
  • setup_class:作用于class用例集中的用例,置于class内,只在class用例执行的开始执行setup_class,结束时执行teardown_class
  • setup_module:作用于模块内的所有用例,置于class外,只在所以用例的开始执行setup_module,结束时执行teardown_module
  • pytest.fixture():作用于模块内的所有用例,但需要传递装饰函数为参数,可置于class内或class外

1.setup_class 和teardown_class用法

# coding=utf-8
import pytest
from selenium import webdriver
import time class Test(object):
@classmethod
def setup_class(cls):
print("初始化浏览器")
cls.driver=webdriver.Chrome()
cls.driver.maximize_window()
time.sleep(2)
cls.driver.get("http://www.baidu.com")
@classmethod
def teardown_class(cls):
cls.driver.close()
print("关闭浏览器")
def test_in01(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
print("0001 is ok") def test_in02(self):
self.driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("002 is ok ")
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items . 初始化浏览器
0001 is ok
.002 is ok 
.关闭浏览器 2.setup_method 和teardown_method 用法
class TestMethod(object): def setup_method(self):
print("初始化浏览器")
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://www.baidu.com")
def teardown_method(self):
self.driver.close()
print("关闭浏览器")
def test_in01(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
print("第一次打开浏览器is ok") def test_in02(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
self.driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("第二次打开浏览器 is ok ")

============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items

. 初始化浏览器
第一次打开浏览器is ok
.关闭浏览器
初始化浏览器
第二次打开浏览器 is ok 
.关闭浏览器

3. pytest.fixture用法介绍

# coding=utf-8
import pytest
from selenium import webdriver
from pyfoo.Utils.pageobj import GetEle
from pyfoo.Utils.pageobj import *
@pytest.fixture(scope="class")
def driver():
url="http://www.baidu.com"
driver= webdriver.Chrome()
driver.get(url)
print("初始化完成...")
yield driver
driver.close()
print("teardown...关闭浏览器") # @pytest.mark.usefixtures("driver")
class Test_fix_module(object): def test_open_bd(self,driver):
GetEle().ele_by_id(driver,'kw').send_keys("pytest")
GetEle().ele_by_id(driver,'su').click()
print("搜索资源case成功")
def test_open_tb(self,driver):
GetEle().ele_by_xpath(driver,'//*[@id="s_tab"]/div/a[1]').click()
print("进入资讯case成功") if __name__ == "__main__":
pytest.main(['-v','-s','-q'])

============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items

. 初始化完成...
搜索资源case成功
.进入资讯case成功
.teardown...关闭浏览器

4.setup_module 和teardown_module

# coding=utf-8
import pytest
from selenium import webdriver
import time
def setup_module():
global driver
driver=webdriver.Chrome()
driver.maximize_window()
time.sleep(2)
driver.get("http://www.baidu.com")
print("开始模块初始化浏览器")
def teardown_module():
driver.close()
print("模块执行结束关闭浏览器")
class TestModule(object):
def test_001(self):
driver.find_element_by_id("kw").send_keys("流浪的python")
driver.find_element_by_id("su").click()
print("test_001 is ok ")
def test_002(self):
driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("test_002 is ok ")

============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items

. 开始模块初始化浏览器
test_001 is ok 
.test_002 is ok 
.模块执行结束关闭浏览器

5.setup_function 和 teardown_function

# coding=utf-8
import pytest
from selenium import webdriver
import time def setup_function():
print("setup_function():每个函数之前执行") def teardown_function():
print ("teardown_function():每个函数之后执行") def test_01():
print ("正在执行函数test1")
x = "this"
assert 'h' in x def add(a,b):
return a+b def test_add():
print ("正在执行test_add()")
assert add(3,4) == 7

============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items

. setup_function():每个函数之前执行
正在执行函数test1
.teardown_function():每个函数之后执行
setup_function():每个函数之前执行
正在执行test_add()
.teardown_function():每个函数之后执行

pytest setup和teardown初始化的更多相关文章

  1. Pytest学习(三) - setup和teardown的使用

    一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...

  2. 『德不孤』Pytest框架 — 10、setUp()和tearDown()函数

    目录 1.setUp()和tearDown()函数介绍 2.setUp()和tearDown()函数作用 3.setUp()和tearDown()函数说明 4.示例 (1)方法级 (2)类级 (3)函 ...

  3. pytest自动化2:测试用例setup和teardown

    前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...

  4. pytest 2.测试用例setup和teardown

    之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的 ...

  5. pytest文档4-测试用例setup和teardown

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

  6. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  7. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  8. pytest的setup和teardown

    学过unittest的setup和teardown,前置和后置执行功能.pytest也有此功能并且功能更强大,今天就来学习一下吧. 用例运行级别: 模块级(setup_module/teardown_ ...

  9. 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?

    还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...

随机推荐

  1. css控制一个元素点击后, 改变另外一个元素的状态

    1.点击后改变子元素.myclass:active span{ color:#00f;} 此方式在ios下不生效,chrome下正常  2.改变下一个兄弟元素.myclass:active +span ...

  2. 解决react不能往setState中传key作为参数的办法(文章最后实现了传递key做参数的办法)

    读者朋友可以直接看最后一个分割线下面的那部分!利用方括号语法来动态的访问对象的属性,实现当参数为属性名的传递; 有时候我们需要每次单独设置众多state中的一个,但是,都是进行相同的操作,这时候如果每 ...

  3. JS判断浏览器版本

    CSS html,body{ height: 100%; } body{ margin: 0 } div{ padding-left: 50px; } .span{ padding: 5px 15px ...

  4. 编辑文本(EditText)

    今天要给大家介绍的是简单的编辑文本框: 先看一下它的基本属性: 1.Activity public class EditTextActivity extends Activity { private ...

  5. Android 黑色样式menu

    效果图:

  6. 引入 Tinker 之后如何在 Debug 模式下开启 Instant Run

    在<Tinker + Bugly + Jenkins 爬坑之路>一文中讲了在接入 Tinker 之后,Jenkins 中的一些坑,由此,热修复算告一段落,但是,在直接 Run 模式运行时, ...

  7. TCP状态统计 - 脚本命令

    一.netstat命令说明 netstat常见参数 -a (all)显示所有选项,默认不显示LISTEN相关 -t (tcp)仅显示tcp相关选项 -u (udp)仅显示udp相关选项 -n 拒绝显示 ...

  8. “小小科技女神”与微软DigiGirlz Day的约会

    上周五在微软中国上海科技园举行的微软科技女生夏令营终于在一天“忙碌的轻松中”,伴随着师生和工程师们的欢笑结束了. 本次的微软科技女生夏令营一共有来自上海闵行区七宝中学.莘庄中学和闵行中学的共50名高中 ...

  9. Oracle数据库从入门到精通-分组统计查询

    视频课程:李兴华 Oracle从入门到精通 视频课程学习者:阳光罗诺 视频来源:51CTO学院 整体内容: 统计函数的使用 分组统计查询的实现 对分组的数据过滤 统计函数 在之前我们就学习过一个COU ...

  10. [tools]excel转lua的python实现

    time:2015/04/13 描述:需要将excel表格内容转成lua,并且作为工具使用,能够批量转换 步骤: (1)文章[1]已经做了大部分的内容,而且也已经能够使用了 (2)根据自己新的需求: ...