温故OpenStack中的测试(by Joshua)

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (作者:张华 发表于:2018-03-15)

  1. 沿用tox调用virtualenv自动创建的虚拟环境(virtualenv -p python3.5 .tox/py35)
    “`
    source .tox/py35/bin/activate
    sudo pip install –upgrade -r requirements.txt
    sudo pip install –upgrade -r test-requirements.txt
 2. 使用unittest和nose运行测试。nose是对unittest的扩展,使得python的测试更加简单,nose自动发现测试代码并执行,nose提供了大量的插件,比如覆盖报表等。
 ```
python -m unittest -v unit_tests.test_neutron_utils.TestNeutronUtils.test_get_packages_ovs_newton
.tox/py35/bin/python nosetests -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_get_packages_ovs_newton

注意:上面采用nosetests运行时会报错,因为我们的测试采用了python3, 所以需要在安装了python3-nose之后(sudo apt-get install python3-nose python3-mock)再采用下列三种方式之一运行:

nosetests3 -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_restart_map_ovs_odl
/bak/work/charms/neutron-gateway/.tox/py35/bin/python /usr/local/bin/nosetests -v unit_tests/test_neutron_utils.py:TestNeutronUtils.test_get_packages_ovs_newton
python -m nose unit_tests/test_neutron_utils.py:TestNeutronUtils.test_restart_map_ovs_odl

但实际上仍然找不找nose模块,那是因为nose与virtualenv结合地不大好,在这个网页找着了答案(https://stackoverflow.com/questions/864956/problems-using-nose-in-a-virtualenv) - You need to have a copy of nose installed in the virtual environment. In order to force installation of nose into the virtualenv, even though it is already installed in the global site-packages, run pip install with the -I flag: pip install nose -I
3. 上面使用unittest与nose运行测试的方式只是将结果输出到stdout,不便于分析。所以可以使用python-subunit模块来运行测试,并将测试结果通过subunit协议输出到文件中便于日后分析。因为subunit是基于二进制的不便于人眼看,所以可使用subunit2pyunit工具将其人类可读化。

python -m subunit.run discover |subunit2pyunit
python -m subunit.run discover -t ./ ./unit_tests |subunit2pyunit
python -m subunit.run unit_tests.test_neutron_utils.TestNeutronUtils. |subunit2pyunit
  1. 在大型应用中分析测试结果很重要,testrepository可以调用subunit来用python-subunit模块来运行测试,并将测试结果通过subunit协议输出到文件中,然后testrepository在些基础上有更多的分析,如分析哪些用例运行的时间最长,如显示失败的用例,如仅运行上次运行失败的用例。
testr init
testr run
testr run --parallel
$ cat .testr.conf 
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
             OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
             OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
             ${PYTHON:-python} -m subunit.run discover -t ./ ./unit_tests $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
  1. tox用于创建虚拟python环境,也可以集成上面的testrepository(commands = ostestr {posargs})
$ cat tox.ini 
[tox]
envlist = pep8,py27,py35
skipsdist = True

[testenv]
setenv = VIRTUAL_ENV={envdir}
         PYTHONHASHSEED=0
         CHARM_DIR={envdir}
         AMULET_SETUP_TIMEOUT=5400
install_command =
  pip install --allow-unverified python-apt {opts} {packages}
commands = ostestr {posargs}
whitelist_externals = juju
passenv = HOME TERM AMULET_* CS_API_*

[testenv:py27]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt
commands = /bin/true

[testenv:py35]
basepython = python3.5
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt

[testenv:pep8]
basepython = python2.7
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/test-requirements.txt
commands = flake8 {posargs} hooks unit_tests tests actions lib
           charm-proof

[flake8]
ignore = E402,E226
exclude = */helpers
  1. pydev使用virtualenv中的py35
    在eclipse的”Preferences -> Pydev -> Interpreters -> Python Interpreters”菜单中定义python35=/bak/work/charms/neutron-gateway/.tox/py35/bin/python,然后在工程上点右键从”Properties -> Pydev - Interpreter/Grammar”定义使用python35。注意,需要将/bak/work/charms/neutron-gateway/.tox/py35/lib/python3.5/site-packages也选到环境变量中,否则后面会报ImportError: No module named ‘mock。
    为一个测试类定义”Python unitest”类型的”Debug Configurations”, 也在其Interpreter选项卡中定义使用python35 (结果:eclipse似乎有bug,此处选择了python35后无法保存)
    所以无法成功,似乎是pydev与python3协作不大好。最后还是pudb好使(sudo pip install pudb, import pudb; pdb.set_trace())
阅读更多
版权声明:本文为博主原创文章,如需转载,请注明出处! https://blog.csdn.net/quqi99/article/details/79567620
文章标签: unit test mock pudb
个人分类: OpenStack Non-Networking
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭