首页 / PYTHON / 如何在python中测试工厂?
如何在python中测试工厂?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在python中测试工厂?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3078字,纯文字阅读大概需要5分钟。
内容图文
![如何在python中测试工厂?](/upload/InfoBanner/zyjiaocheng/803/132487d33d344ddd913dd022c722555c.jpg)
单元测试第一类应仅针对其协作者的公共接口进行测试.在大多数情况下,这很容易实现用假物体替换协作者 – 模拟器.正确使用依赖注入时,这应该很容易.
但是,在尝试测试工厂类时,事情会变得复杂.让我们看看例子
模块轮
class Wheel:
"""Cars wheel"""
def __init__(self, radius):
"""Create wheel with given radius"""
self._radius = radius #This is private property
模块引擎
class Engine:
"""Cars engine"""
def __init(self, power):
"""Create engine with power in kWh"""
self._power = power #This is private property
模块车
class Car:
"""Car with four wheels and one engine"""
def __init__(self, engine, wheels):
"""Create car with given engine and list of wheels"""
self._engine = engine
self._wheels = wheels
现在让我们拥有CarFactory
from wheel import Wheel
from engine import Engine
from car import Car
class CarFactory:
"""Factory that creates wheels, engine and put them into car"""
def create_car():
"""Creates new car"""
wheels = [Wheel(50), Wheel(50), Wheel(60), Wheel(60)]
engine = Engine(500)
return Car(engine, wheels)
现在我想为CarFactory编写一个单元测试.我想测试,工厂正确创建对象.但是,我不应该测试对象的私有属性,因为它们将来可以更改,这会破坏我的测试.想象一下,Wheel._radius被Wheel._diameter取代,Engine._power取代了Engine._horsepower.
那么如何测试工厂?
解决方法:
幸运的是,在python测试中,工作很容易归功于monkey_patching.您不仅可以替换对象的实例,还可以替换整个类.让我们看看例子
import unittest
import carfactory
from mock import Mock
def constructorMock(name):
"""Create fake constructor that returns Mock object when invoked"""
instance = Mock()
instance._name_of_parent_class = name
constructor = Mock(return_value=instance)
return constructor
class CarFactoryTest(unittest.TestCase):
def setUp():
"""Replace classes Wheel, Engine and Car with mock objects"""
carfactory.Wheel = constructorMock("Wheel")
carfactory.Engine = constructorMock("Engine")
carfactory.Car = constructorMock("Car")
def test_factory_creates_car():
"""Create car and check it has correct properties"""
factory = carfactory.CarFactory()
car_created = factory.create_car()
# Check the wheels are created with correct radii
carfactory.Wheel.assert_called_with(radius=50)
carfactory.Wheel.assert_called_with(radius=50)
carfactory.Wheel.assert_called_with(radius=60)
carfactory.Wheel.assert_called_with(radius=60)
# Check the engine is created with correct power
carfactory.Engine.assert_called_once_with(power=500)
# Check the car is created with correct engine and wheels
wheel = carfactory.Wheel.return_value
engine = carfactory.Engine.return_value
carfactory.Car.assert_called_once_with(engine, [wheel, wheel, wheel, wheel])
# Check the returned value is the car created
self.assertEqual(car_created._name_of_parent_class, "Car")
所以我们用Mock替换类及其构造函数,返回我们的假实例.这使我们能够检查,构造函数被调用了正确的参数,因此我们不需要依赖真正的类.我们真的能够在python中不仅使用假实例,而且还使用假类.
另外,我不得不提一下,上面的代码并不理想.例如,假构造函数应该为每个请求创建新的Mock,因此我们可以检查汽车是否使用正确的轮子调用(例如正确的顺序).这可以完成,但代码会更长,我想让示例尽可能简单.
在示例中,我使用了Mock库来进行python http://www.voidspace.org.uk/python/mock/
但这没有必要.
内容总结
以上是互联网集市为您收集整理的如何在python中测试工厂?全部内容,希望文章能够帮你解决如何在python中测试工厂?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。