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

最新文章

  1. python之最强王者(3)——变量,条件、循环语句
  2. 该字符串未被识别为有效的 DateTime
  3. CSipSimple配置系统
  4. msvc2013编译qt5.6源码
  5. 转: YAML 语言教程 from(阮一峰)
  6. node基础06:回调函数
  7. 让less编译通过css滤镜
  8. IE6下 input 背景图滚动问题及标签规范
  9. 【上传AppStore】iOS项目上传到AppStore步骤流程(第一章) - 上传新的app
  10. Java SSH远程执行Shell脚本实现(转)
  11. 如何修改Ubuntu双系统的默认开机启动项
  12. URL编码 URLEncoder 示例
  13. HDOJ 5184 Brackets 卡特兰数扩展
  14. auto and static key words
  15. Nginx 变量漫谈(八)
  16. Flowers(二分水过。。。)
  17. #DP# ----- OpenJudge山区建小学
  18. c++(单向链表)
  19. 深入理解JVM(10)——Class文件结构
  20. 1004. Max Consecutive Ones III最大连续1的个数 III

热门文章

  1. Java 序列化 和 反序列--by Vincent
  2. rails Installer之后的调整rails.bat等文件
  3. 数据挖掘算法之-关联规则挖掘(Association Rule)(购物篮分析)
  4. Struts2概述及与Struts1的对照
  5. UVA - 12230 Crossing Rivers 概率期望
  6. Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 189......
  7. c11---位运算相关
  8. 多年iOS开发经验总结(一)
  9. Java悲观锁和乐观锁
  10. 利用CSS3中的clac()实现按照屏幕分辨率自适应宽度