[Python Test] Use pytest fixtures to reduce duplicated code across unit tests
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的更多相关文章
- Python测试框架pytest命令行参数用法
在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个 序号 类别 中文名 包含命令行参数数量 1 positional arguments 形参 1 2 gene ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- python 内建函数 filter,map和reduce
python 内建函数 filter,map和reduce, 三个函数比较类似,都是应用于序列的内置函数,常见的序列包括list.tuple.str等.而且三个函数都可以和lambda表达式结合使用. ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- Python进阶内容(三)--- reduce
描述 functools.reduce() 函数会对参数序列中元素进行累积.函数将一个数据集合(列表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 function(有两个参数)先对集 ...
- python中filter、map、reduce的区别
python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...
- python一些内建函数(map,zip,filter,reduce,yield等)
python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...
- 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()函数. 我们先看 ...
- Python测试框架pytest入门基础
Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...
随机推荐
- unity3D游戏开发实战原创视频讲座系列9之塔防类游戏开发第一季
解说文件夹 塔防游戏0基础篇... 第一讲 游戏演示和资源介绍... 第二讲 游戏场景的完毕... 第三讲 预制体的制作... 第四讲 敌人的随机产生和按路径行走... 第五讲 塔防工具的产 ...
- ASP.NET六大巨头——内置对象(1)
ASP.NET提供了六个内置对象:Request.Response.Application.Session.Server和Cookie.这些对象收集当前应用程序请求.用户信息.响应浏览器信息,来完毕页 ...
- TCP和UDP的具体区别
TCP和UDP的最完整的区别 TCP UDP TCP与UDP基本区别: 1.基于连接与无连接 2.TCP要求系统资源较多,UDP较少: 3.UDP程序结构较简单 4.流模式(TCP)与数据报模式(UD ...
- 淘宝druid报错:javax.management.InstanceNotFoundException: com.alibaba.druid:type=DruidDataSourceStat
问题: 启动tomcat报错: Tomat报出一下异常:ERROR [com.alibaba.druid.stat.DruidDataSourceStatManager] – unregister m ...
- wampserver的安装及使用配置方法
在安装wampserver的过程中,根据本人在安装过程中遇到的问题,在此做个总结,与小伙伴们分享一下下~~~. 1. 何处获得Wamp ★ 在自己的Windows电脑上Web服务器软件 - Wamp. ...
- LIst和map的遍历
1. public static void main(String[] args) { // ArrayList类实现一个可增长的动态数组 List<String> list = new ...
- JAVA实现两种方法反转单列表
/** * @author luochengcheng * 定义一个单链表 */ class Node { //变量 private int record; //指向下一个对象 private Nod ...
- C#实现软件监控外部程序运行状态的方法
本文实例讲述了C#实现软件监控外部程序运行状态的方法.分享给大家供大家参考.具体方法如下: 需要外挂一个程序,用于监控另一个程序运行状态,一旦检测到另一程序关闭,就触发一个事件做其他处理. using ...
- MySQL · 答疑解惑 · 备库Seconds_Behind_Master计算
背景 在mysql主备环境下,主备同步过程如下,主库更新产生binlog, 备库io线程拉取主库binlog生成relay log.备库sql线程执行relay log从而保持和主库同步. 理论上主库 ...
- vim常用的基本命令
vim 常用的基本命令1. w [文件名] 相当于另存为2. r [文件名] 将[文件名]的内容加到光标行后面3. n1,n2 w[filename] 将n1到n2的内容保存为[file ...