Openstack 单元测试用组件一览

发布时间: 更新时间: 总字数:1134 阅读时间:3m 作者:IP:上海 网址

Openstack 是最大的开源云产品,在 Openstack 的众多组件中,单元测试是一件很重要的事情,本文简单汇总 OpenStack 各组件的单元测试工具。

hacking

一组 flake8 插件,用于静态检查。 https://pypi.python.org/pypi/hacking

coverage

衡量 python 代码覆盖率的工具。可以单独执行/API 方式或者以 nose 插件方式运行“nosetests –with-coverage”。

https://nose.readthedocs.org/en/latest/plugins/cover.html

discover

测试用例发现。(2.7 已经包含在 unittest 中,2.4 需要 backport) 主要在 run_test.sh 下使用。

https://pypi.python.org/pypi/discover/0.4.0

feedparser

使用 python 进行 parse RSS 订阅内容主要在 version API 的测试中使用(versionAPI 支持 atom 格式返回信息)

MySQL-python

mysql 接口的 python 实现

psycopg2

postgresql 接口的 python 实现

pylint

对 python 进行静态分析、检查的工具

python-subunit

subunit 是测试结果的流协议。python-subunit 是它的 python 实现。

sphinx

文档生成工具(基于 Restructed 格式)

oslosphinx

openstack 对 sphinx 的扩展

testrepository

测试结果的数据库。主要在覆盖率测试时使用。

mock

对所测试的函数的外部依赖函数进行模拟替换。3.3 以后已经是 python 标准库。mock 的实现原理也很简单,一般使用类似 mokey patch 的方式实现。

mox

基于 java 的 easymock 提供的 python mock 对象框架(基本上已经停止维护)

Mox 的执行流程:

text
Create mock (in record mode)
Set up expectations
Put mock into replay mode
Run test
Verify expected interactions with the mock occurredmox与mock功能类似,都是用来做函数或者类的mock。

Mock 方法和属性:

text
>>> # Mock
>>> my_mock = mock.Mock()
>>> my_mock.some_method.return_value = "calculated value"
>>> my_mock.some_attribute = "value"
>>> assertEqual("calculated value", my_mock.some_method())
>>> assertEqual("value", my_mock.some_attribute)

>>> # Mox
>>> my_mock = mox.MockAnything()
>>> my_mock.some_method().AndReturn("calculated value")
'calculated value'
>>> my_mock.some_attribute = "value"
>>> mox.Replay(my_mock)
>>> assertEqual("calculated value", my_mock.some_method())
>>> assertEqual("value", my_mock.some_attribute)

部分 mock(对已有对象的某个方法进行 mock):

text
>>> # Mock
>>> SomeObject.some_method = mock.Mock(return_value='value')
>>> assertEqual("value", SomeObject.some_method())

>>> # Mox
>>> my_mock = mox.MockObject(SomeObject)
>>> my_mock.some_method().AndReturn("value")
'value'
>>> mox.Replay(my_mock)
>>> assertEqual("value", my_mock.some_method())
>>> mox.Verify(my_mock)

抛出异常:

text
>>> # Mock
>>> my_mock = mock.Mock()
>>> my_mock.some_method.side_effect = SomeException("message")
>>> assertRaises(SomeException, my_mock.some_method)

>>> # Mox
>>> my_mock = mox.MockAnything()
>>> my_mock.some_method().AndRaise(SomeException("message"))
>>> mox.Replay(my_mock)
>>> assertRaises(SomeException, my_mock.some_method)
>>> mox.Verify(my_mock)

fixtures

翻译为“夹具”,顾名思义提供了状态重用等的抽象机制。

python
---------------myfixture.py-----------------------
import testtools
import unittest
import fixtures
class NoddyFixture(fixtures.Fixture):
    def setUp(self):
        super(NoddyFixture, self).setUp()
        self.frobnozzle = 42
        self.addCleanup(delattr, self, 'frobnozzle')


class NoddyTest(testtools.TestCase, fixtures.TestWithFixtures):
    def test_example(self):
        fixture = self.useFixture(NoddyFixture())
        self.assertEqual(42, fixture.frobnozzle)

result = unittest.TestResult()
_ = NoddyTest('test_example').run(result)
print (result.wasSuccessful())

-----------------运行结果------------------------
[root@centoo65 data]# python myfixtures.py
True

-----------常用fixture-------------------------
>>> import fixtures
>>> a = fixtures.TempDir()
>>> a.setUp()
>>> print a.path
/tmp/tmpWB8EmF
>>> quit()
----------------------------------------------

testtools

对 python 标准单元测试框架的扩展。优点:

  • 更好的断言 比如支持 assertThat 扩展
  • 更详细的 debug 信息 比如支持 addDetails 的信息
  • 扩展的同时保持兼容性
  • python 多版本的兼容性

tox

通用的虚拟环境管理和测试命令行工具。

配置可以看工程下的 tox.ini 文件。tox.ini 可以通过 tox-quickstart 生成。

命令执行,如:“tox -e py26", “tox -e pep8”

执行单元测试的时候,顺便生成单元测试报告,并检查测试覆盖率,并生成覆盖率报告。直接执行 tox 是不行的,只能进行单元测试,需要给 tox 增加扩展参数,如下:tox – –cover-erase – –with-coverage – –cover-html 类说明

顶级目录 test.py 中:

text
test.TestCase继承于testtools.TestCase。
test.NoDBTestCase继承于test.TestCase。
-

test.TestCase中大量使用了fixture对环境进行初始化。(比如DB,Policy等)