pytest 学习笔记一:参数化与组织分层
组织分层:
1、普通方式,和unittest分层类似:
setup_module() # 通常放在类外
setup_class(cls)
setup(self)
teardown(self)
teardown_class(cls)
teardown_module()
2、pytest特有的分层方式
@pytest.fixture() 装饰fixture
@pytest.mark.usefixtures() 使用fixture
例一:
@pytest.fixture() # 默认scope是function,等同于@pytest.fixture(scope="function"),作用于每个test用例
def before():
print u"清除数据" class TestClass(common): # 继承common类中setup_class def setup(self): # 可以和before并存,如果某个用例使用了before,共同生效。
print "start" def teardown(self):
print "end" @pytest.mark.usefixtures("before") #使用fixture的方法一
def test_one(self):
x = "this"
assert "h" in x def test_two(self,before): # 使用fixture的方法二
x = "hello"
assert x == "hello" if __name__=="__main__":
pytest.main("-s test_pt2.py") # 指定测试文件
执行后结果:
@pytest.fixture(scope="xxx") 有4个范围,function、class、module、session 。session是作用于整个项目。
例二:
#coding:utf8
import pytest class DB(object):
def __init__(self):
self.intransaction = []
def begin(self, name):
self.intransaction.append(name)
def rollback(self):
self.intransaction.pop() @pytest.fixture(scope="module")
def db(): # 工厂模式
print "module start"
return DB() # @pytest.mark.usefixtures("transact") #作用于整个类,即对类中所有用例都生效
class TestClass(object):
@pytest.fixture(autouse=True) # 设置一个function级别的fixture, autouse=True 表示对所有用例生效 ,等效于在测试类前@pytest.mark.usefixtures("transact")
def transact(self,request, db):
db.begin(request.function.__name__)
print "transact"
yield # yield之后的内容可以当成teardown !!!
db.rollback()
print "rollback" def test_method1(self,db): # 这里需要引入db ,db是module范围,每个模块只运行一次
#module只运行一次,那么在这个module范围(module表示同一层目录中?)中所有的用例引用db得到的参数DB()是同一个。这里类似于module范围的一个全局变量
assert db.intransaction == ["test_method1"] def test_method2(self,db):
assert db.intransaction == ["test_method2"] if __name__=="__main__":
pytest.main("-s autofixtures.py")
执行结果:
参数化:
#参数化一,先定义函数,更灵活
@pytest.fixture(params=[2,3,4])
def before3(request): #request固定格式
param=request.param
print param
return param+1 再使用:
def test_four(self,before3): # 参数化
assert before3 !=3
# 参数化二,简便
@pytest.mark.parametrize("param", [1, 2, 3])
def test_three(self,param): # 参数化
assert param !=2 参数覆盖fixture:![]()
conftest.py :
import pytest @pytest.fixture
def username():
return 'username' @pytest.fixture
def other_username(username):
return 'other-' + username
test_something.py:
#coding:utf8
import pytest # 同一层级的fixture可以直接引用 @pytest.mark.parametrize('username', ['directly-overridden-username'])
def test_username(username): # conftest中的fixture:username,这里被参数username直接覆盖
assert username == 'directly-overridden-username' @pytest.mark.parametrize('username', ['directly-overridden-username-other'])
def test_username_other(other_username): # conftest中的fixture:other_username,other_username引用了username,这里被参数usename间接覆盖了
assert other_username == 'other-directly-overridden-username-other' if __name__=="__main__":
pytest.main("-s test_something.py") 更多fixture覆盖参考:https://blog.csdn.net/huitailang1991/article/details/74053781
pytest 学习笔记一:参数化与组织分层的更多相关文章
- [转载]pytest学习笔记
pytest学习笔记(三) 接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...
- Pytest学习笔记8-参数化
前言 我们在实际自动化测试中,某些测试用例是无法通过一组测试数据来达到验证效果的,所以需要通过参数化来传递多组数据 在unittest中,我们可以使用第三方库parameterized来对数据进行参数 ...
- pytest学习笔记
From: https://blog.csdn.net/gaowg11/article/details/54910974 由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记 ...
- pytest学习笔记(一)
这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+requests)非常的轻便,然后很有兴致的决定学习下,然后又发现了pytest-sele ...
- pytest学习笔记(三)
接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...
- pytest学习笔记(二)
继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...
- 学习笔记之TCP/IP协议分层与OSI參考模型
1.协议的分层 ISO在制定标准化OSI之前,对网络体系结构相关的问题进行了充分的讨论, 终于提出了作为通信协议设计指标的OSI參考模型.这一模型将通信协议中必要 的功能分成了7层.通过这些 ...
- pytest 学习笔记一 入门篇
前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...
- Pytest学习笔记3-fixture
前言 个人认为,fixture是pytest最精髓的地方,也是学习pytest必会的知识点. fixture用途 用于执行测试前后的初始化操作,比如打开浏览器.准备测试数据.清除之前的测试数据等等 用 ...
随机推荐
- PHP获取中文首字母的函数
PHP获取中文首字母的函数 <?php header("Content-type: text/html; charset=utf-8"); function getFirst ...
- php多图片上传。
1. <form method="post" enctype="multipart/form-data" action='请求地址' > <i ...
- SecureCRT显示乱码的解决办法
下面来看看SecureCRT的显示出现乱码这种情况.比如: 现在我们重新设置一下 设置下图中的配置 1.选择字符编码为UTF-8. 2.设置字符集为GB2312后保存好后确认退出. 3.再次测试一下.
- Django 模板格式化日期
在模板中格式化日期: {{ post.date|date:”Y-m-d H:i:s” }}
- JVM老年代和新生代的比例
在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( Young ) 又被划分为三个区域:Eden.From Survivor.To Surviv ...
- ueditor修改工具栏固定位置和显示空白div
ueditor.all.js
- Django-wed开发框架-练习题
https://www.cnblogs.com/pandaboy1123/p/9894981.html 1.列举Http请求常见的请求方式 HTTP协议是Hyper Text Transfer Pro ...
- python字符串查找
a = "string test" a.index("g") a.find("g")
- 【385】itertools 的 product 和 chain 和 accumulate
参考:itertools模块 product 相当于返回两个集合中数据的所有组合可能 Examples from Eric Martin from itertools import product p ...
- sqlalchemy 学习-- 多表操作
一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...