In this lesson, you will learn how to implement pytest fixtures. Many unit tests have the same resource requirements. For example, an instantiated object from a class. You will learn how to create the instance of the class one time as a fixture and reuse that object across all your tests. This results in faster tests, eliminates duplicate code, and uses less resources when running your tests.

"""
Python class for a self-driving car.
Suitable for disrupting automotive industry
""" class Car(object): def __init__(self, speed, state):
self.speed = speed
self.state = state def start(self):
self.state = "running"
return self.state def turn_off(self):
self.state = "off"
return self.state def accelerate(self):
self.speed += 10
return self.speed def stop(self):
self.speed = 0
return self.speed

test:

"""
Tests for Car class
""" import pytest
from car import Car class TestCar(object): """
default scope is "function" which means
foreach test, it will have its own scope
"module" ref to class itself, so it sharing
the same instance
""" @pytest.fixture(scope="module")
def my_car(self):
return Car(0, "off") def test_start(self, my_car):
my_car.start()
assert my_car.state == "running" def test_turn_off(self, my_car):
my_car.turn_off()
assert my_car.state == "off" def test_accelerate(self, my_car):
my_car.accelerate()
assert my_car.speed == 10 """
This one will failed because we are using fixture
scope as "module", my_car.speed == 20
"""
def test_accelerate1(self, my_car):
my_car.accelerate()
assert my_car.speed == 10 def test_stop(self, my_car):
my_car.stop()
assert my_car.speed == 0

[Python Test] Use pytest fixtures to reduce duplicated code across unit tests的更多相关文章

  1. Python测试框架pytest命令行参数用法

    在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个 序号 类别 中文名 包含命令行参数数量 1 positional arguments 形参 1 2 gene ...

  2. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

  3. python 内建函数 filter,map和reduce

    python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...

  4. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下   Py ...

  5. Python进阶内容(三)--- reduce

    描述 functools.reduce() 函数会对参数序列中元素进行累积.函数将一个数据集合(列表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 function(有两个参数)先对集 ...

  6. python中filter、map、reduce的区别

    python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...

  7. python一些内建函数(map,zip,filter,reduce,yield等)

    python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...

  8. python之高阶函数map/reduce

    L = [] for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]: L.append(f(n)) print(L) Python内建了map()和reduce()函数. 我们先看 ...

  9. Python测试框架pytest入门基础

    Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...

随机推荐

  1. 【智能家居篇】wifi网络结构(上)

    转载请注明出处:http://blog.csdn.net/Righthek 谢谢! WIFI是什么.相信大家都知道,这里就不作说明了. 我们须要做的是深入了解其工作原理,包含软硬件.网络结构等.先说明 ...

  2. 【UML】UML世界的构成

    UML概述 全名:Unified Modeling Language 中文名:统一建模语言 发展历程:"始于1997年一个OMG标准.它是一个支持模型化和软件系统开发的图形化语言,为软件开发 ...

  3. VC与JavaScript交互(一) ———— 怎样实现

    为什么要让VC与JavaScript交互? 1.有时候我们须要让自己的软件打开一个网页.来获取页面上的一些数据. 这时,能够用mshtml解析HTML提取出数据.也能够向HTML文档动态写入我们准备好 ...

  4. Hbase集群扩展

    当hbase集群节点不够用时,我们须要新增节点来对集群进行扩展.hbase集群的扩展是非常easy的,过程例如以下: 一.准备一台新机器作为扩展节点,这里是作为slaves15,该机子要先与maste ...

  5. cocos2d 3.3 lua 代码加密 luac

    1.0 cocos luacompile 使用方法 我用的普通的cocos2d lua,没用quick,quick好像能够对整个资源包含图像和音频都加密,打包成zip.我看了下luacompile 的 ...

  6. thinkPHP 空模块和空操作、前置操作和后置操作 具体介绍(十四)

    本章节:介绍 TP 空模块和空操作.前置操作和后置操作 具体介绍 一.空模块和空操作 1.空操作 function _empty($name){ $this->show("$name ...

  7. chrome设置书签默认显示

    实用的设置! 这样已设置,就可以方便的查看一些常用的书签了!

  8. ssh tunnel 上网

    用DNS隧道实现免费上网 大多数机场.酒店之类场所,当你输入一个网址比如www.google.com时,会弹出一个页面要你输入帐号密码才能上网.这个时候DNS能正确解析,但是上网要付费认证. 可以通过 ...

  9. hdoj--3062--party(2-sat 可行解)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. 【转】iOS 设置APP的名称(浅述APP版本国际化与本地化)

    原文网址:http://www.jianshu.com/p/a3a70f0398c4 前言 App的名字设置方式有很多种,如果在App打包上线时不做修改,最终App的名字就是Xcode在建立工程时的名 ...