[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 ...
随机推荐
- 【Swift】学习笔记(六)——函数
函数 懂编程语言的来说这个是最主要的了,不论什么语言都有函数这个概念.函数就是完毕特定任务的独立代码块. 函数怎么创建: 1.创建一个无參无返回值的函数(实际上全部的函数都有返回值,这个函数返回vo ...
- MantisBT 问题分配显示 姓名
MantisBT 在提交问题的时候,系统默认"分配"给备选账号,而不是姓名. 这样在使用的时候很不便. 能够通过改动配置文件来改变,找到MantisBT根文件夹下文件config_ ...
- hive1.2伪分布mysql数据库配置具体解释
hadoop2.6伪分布配置:http://blog.csdn.net/gamer_gyt/article/details/46793731 hive1.2 derby元数据库配置:http://b ...
- struts2在action中获取request、session、application,并传递数据
假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...
- java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream
转自:https://blog.csdn.net/y970105/article/details/355401 进入 tomcat根目录/lib/servlet-api.jar复制出来,放到JDK_P ...
- 在Maven中引入spring的DAO、DOMAIN、CONTROLLER、VIEW
除了mysql外麻雀虽小,五脏俱全. 参照之前的博客建立好的maven项目如图. 第一步 : 向maven项目中的pom文件添加依赖 ,然后maven install
- 获得IP地址中文
string ipFilePath = @"~/App_Data/QQWry.dat"; QQWryLocator QQWry = new QQWryLocator(Server. ...
- 通过修改路由,或者增加Route属性来控制访问webApi的路径
可以通过RouteConfig.cs文件中的路由规则来控制 通过为每个方法增加单独的[Route(“api/xx类/xx方法”)]
- heavy dark--读《《暗时间》》
本书名为<<暗时间>>,个人觉得是一个非常好的名字:1.迷茫的大学生有多少的业余时间,但又浪费多少的业余时间,浪费的这莫多时间就如同人在黑夜中一样,大脑是在休息的状态.这是第一 ...
- Oracle自制事务
数据库事务是一种单元操作,要么全部操作成功,要么全部失败.在Oracle中,一个事务是从执行第一个数据操作语言(DML)语句开始的,直到执行一个COMMIT语句,提交保存事务,或执行一个ROLLBAC ...