组织分层:

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 学习笔记一:参数化与组织分层的更多相关文章

  1. [转载]pytest学习笔记

    pytest学习笔记(三)   接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...

  2. Pytest学习笔记8-参数化

    前言 我们在实际自动化测试中,某些测试用例是无法通过一组测试数据来达到验证效果的,所以需要通过参数化来传递多组数据 在unittest中,我们可以使用第三方库parameterized来对数据进行参数 ...

  3. pytest学习笔记

    From: https://blog.csdn.net/gaowg11/article/details/54910974 由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记 ...

  4. pytest学习笔记(一)

    这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+requests)非常的轻便,然后很有兴致的决定学习下,然后又发现了pytest-sele ...

  5. pytest学习笔记(三)

    接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...

  6. pytest学习笔记(二)

    继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...

  7. 学习笔记之TCP/IP协议分层与OSI參考模型

    1.协议的分层      ISO在制定标准化OSI之前,对网络体系结构相关的问题进行了充分的讨论, 终于提出了作为通信协议设计指标的OSI參考模型.这一模型将通信协议中必要 的功能分成了7层.通过这些 ...

  8. pytest 学习笔记一 入门篇

    前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...

  9. Pytest学习笔记3-fixture

    前言 个人认为,fixture是pytest最精髓的地方,也是学习pytest必会的知识点. fixture用途 用于执行测试前后的初始化操作,比如打开浏览器.准备测试数据.清除之前的测试数据等等 用 ...

随机推荐

  1. py库: pymysql、 json (mysql数据库)

    数据库查询结果,用json返回: #连接数据库 import pymysql print(pymysql.VERSION) conn = pymysql.Connect(host='localhost ...

  2. <转载> GIT 操作小结 http://www.cnblogs.com/-ding/p/6008096.html

    参考: http://www.cnblogs.com/zyf-zhaoyafei/p/4486220.html 作者:万境绝尘 转载请注明出处:http://blog.csdn.net/shulian ...

  3. Python基础7 面向对象编程进阶

    本节内容: 面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 经典 ...

  4. redisUtils

    package com.icil.elsa.subscribe.milestone.common.utils; import java.io.Serializable; import java.uti ...

  5. leetcode1018

    根据题目的hint,使用单层循环计算: class Solution(object): def prefixesDivBy5(self, A: 'List[int]') -> 'List[boo ...

  6. 火狐Firefox浏览器所有历史版本下载地址

    Mozilla Firefox 频繁的更新,导致许多好用的插件在更新后不能兼容,而且想换回低版本还不容易啊,官网上只看到最新版本和前一个版本的下载. 这里为大家提供了一个下载链接,是来自Mozilla ...

  7. Mycat 简单配置

    文献 https://www.cnblogs.com/parryyang/p/5758087.html 启动

  8. Zookeeper原理架构

    Zookeeper到底是什么!? 学一个东西,不搞明白他是什么东西,哪还有心情学啊!! 首先,Zookeeper是Apache的一个java项目,属于Hadoop系统,扮演管理员的角色. 然后看到官网 ...

  9. linux mce的一些相关内容和用户态监控的设计方法

    之所以想起写一点关于mce的东西,倒不是因为遇到mce的异常了,之前遇到过很多mce的异常,内存居多,但没有好好记录下来,写这个是因为参加2018 clk南京会议的一点想法. void __init ...

  10. 正则表达式(Kotlin)

    课题 使用正则表达式匹配字符串 使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89" 返回匹配结果:'" ...