【发布时间】:2012-09-02 16:00:19
【问题描述】:
当单元测试类应该只针对其合作者的公共接口进行测试。在大多数情况下,使用 假对象 - Mocks 替换协作者很容易实现。当正确使用依赖注入时,这应该很容易。
但是,当尝试测试工厂类时,事情会变得复杂。让我们看例子
模块wheel
class Wheel:
"""Cars wheel"""
def __init__(self, radius):
"""Create wheel with given radius"""
self._radius = radius #This is private property
模块engine
class Engine:
"""Cars engine"""
def __init(self, power):
"""Create engine with power in kWh"""
self._power = power #This is private property
模块car
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。
那么如何测试工厂呢?
【问题讨论】:
-
你为什么要写Java并称它为Python?不需要 CarFactory 类:
create_car应该是一个独立的函数。而且我们在 Python 中没有私有变量。 -
@Daniel Roseman 当我说私有时,我的意思是私有作为内部(不是 api 的一部分)。我并不是说作为语言结构的私有。关于 CarFactory 是类,你能解释为什么它是错的吗?我是python新手,所以很抱歉我不够pythonic。
-
如果您不封装任何数据,则无需创建类。类不仅仅是放置函数的地方:这就是模块的用途。将
create_car作为模块级独立函数。另请注意,无需在单独的模块中使用Wheel、Car和Engine。 -
@Daniel Roseman:我认为您可能会将问题中使用的琐碎示例代码与问题本身混淆。恕我直言,OP 的方法本质上没有任何问题。对于非常简单的模块和类可能没有必要,但在应用于大型复杂问题时可能会很有用。
标签: python unit-testing dependency-injection