[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 ...
随机推荐
- windows系统中软件开发常用的软件
1.windwos快速打开控制面板:热键+r打开运行框,输入control就打开windows的控制面板了 2.windows自带的远程桌面控制系统:mstsc -Microsoft terminal ...
- BP网络中的反向传播
本文的主要参考:How the backpropagation algorithm works 下面是BP网络的参数结构示意图 首先定义第l层网络第j个神经元的输出(activation) 为了表示简 ...
- POJ 2189
P是端点,牛在区域中啊... #include <iostream> #include <cstdio> #include <cstring> #include & ...
- 为什么mysql中用\G表示按列方式显示
关于mysql的错误 - no query specified 学习了:http://blog.csdn.net/tenfyguo/article/details/7566941 sql语句可以用分号 ...
- C/C++知识要点5——智能指针原理及自己定义实现
智能指针概述: 智能指针用来管理动态对象.其行为类似于常规指针,重要的差别是:它负责自己主动释放所指向的对象. C++ 11标准库提供两种智能指针:shared_ptr.unique_ptr 差别是: ...
- HDOJ 4944 FSF’s game
http://blog.csdn.net/keshuai19940722/article/details/38519681 不明真相的补一发... FSF's game Time Limit: 900 ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- Flask Cookie和Session
1.1.概念 cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第 ...
- BZOJ 2821 分块+二分
题意: N个数,M组询问,每次问[l,r]中有多少个数出现正偶数次. 思路: 把N个数分成sqrt(n)块,预处理d[i][j]表示第i块起点到第j块末尾的答案 枚举起点i,并维护一个数组记录每个数到 ...
- POJ 3134 Power Calculus ID-DFS +剪枝
题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...