OpenStack中的测试
在openstack中写一个扩展API之后要写哪些测试代码呢?https://jenkins.openstack.org/view/Nova/?列出来了提交代码后jenkins所要做的测试。
class FamilyTree(object): def __init__(self, person_gateway): self._person_gateway = person_gateway person_gateway = FakePersonGateway() # ... tree = FamilyTree(person_gateway)2) Monkey Patching, 如下例子,例用python脚本语言的特性,在运行时可以动态替换命名空间的方法,用FakePersonGateway替换掉命名空间mylibrary.dataaccess.PersonGateway
class FamilyTree(object): def __init__(self): self._person_gateway = mylibrary.dataaccess.PersonGateway() mylibrary.dataaccess.PersonGateway = FakePersonGateway # ... tree = FamilyTree()所以,相应地,也就有了下列几类实际隔离的方法:
authorize = extensions.extension_authorizer('compute', 'flavor_dynamic') class FlavorDynamicController(servers.Controller): @wsgi.extends def create(self, req, body): context = req.environ['nova.context'] authorize(context)那么在为它写单元测试时,对authorize函数打桩替换的代码如下:
def authorize(context): pass class FlavorDynamicTest(test.TestCase): def setUp(self): super(FlavorDynamicTest, self).setUp() self.controller = flavor_dynamic.FlavorDynamicController() self.stubs.Set(flavor_dynamic, 'authorize', authorize)2) Mock对象,例如要测试的方法中调用了
self.mox.StubOutWithMock(flavor_dynamic.instance_types, 'get_instance_type_by_name') flavor_dynamic.instance_types.get_instance_type_by_name( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(None) flavor_dynamic.instance_types.get_instance_type_by_name( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn("{'a'='1'") self.mox.ReplayAll()3) Fake对象,即创建大量的假对象。