python nose setuptools 快速入门
发布日期:2021-05-17 18:23:21 浏览次数:19 分类:精选文章

本文共 2135 字,大约阅读时间需要 7 分钟。

安装依赖并运行测试

安装依赖

安装nose测试框架:

pip3 install nose

安装setuptools:

pip3 install setuptools

项目结构

ks@ubuntu:/work/project$ ls ndemotest -1ndemotest:setup.pytestsNAME_test.py__pycache__

项目根目录下的setup.py内容如下:

from distutils.command.install_data import install_datapackages = ['tests', ]scripts = ['tests/NAME_test.py']cmdclasses = {'install_data': install_data}# data_files = [('etc', ['etc/myproject.cfg'])]setup_args = {    'name': 'myproject',    'version': '0.1',    'packages': packages,    'cmdclass': cmdclasses,    'data_files': "",    'scripts': scripts,    # 'include_package_data': True,    # 'test_suite': 'nose.collector'}try:    from setuptools import setupexcept ImportError:    from distutils.core import setupsetup(**setup_args)

测试用例

tests/NAME_test.py文件内容如下:

from nose import toolsclass TestNameTest:    def __init__(self):        pass        def setup(self):        print("start!")        def teardown(self):        print("End....")        def testfunc1(self):        a = 1        b = 1        tools.ok_(a == b, "通过")        print("case1通过")        def testfunc2(self):        a = 2        b = 1        tools.ok_(a == b, "失败")        print("case2失败")

测试结果

运行测试时输出如下信息:

ks@ubuntu:/work/project/ndemotest$ python3 setup.py testrunning testWARNING: Testing via this command is deprecated and will be removed in a future version.Users looking for a generic test entry point independent of test runner are encouraged to use tox.running egg_info...creating myproject.egg-inf...writing top-level names to myproject.egg-info/top_level.txtwriting manifest file 'myproject.egg-info/SOURCES.txt'package init file 'tests/__init__.py' not found (or not a regular file)reading manifest file 'myproject.egg-info/SOURCES.txt'writing manifest file 'myproject.egg-info/SOURCES.txt'running build_extNAME_test.TestNameTest.testfunc1 ... okNAME_test.TestNameTest.testfunc2 ... FAIL

测试结果说明:

  • testfunc1通过。
  • testfunc2失败,原因是:
AssertionError: 失败

详细信息如下:

-------------------- begin captured stdout ---------------------start!End....-------------------- end captured stdout ---------------------

测试失败原因:

  • 测试方法testfunc2中,ab不相等,导致AssertionError
  • 测试用例失败,需修复相关逻辑。
上一篇:python WSGI
下一篇:zmq pub-sub通信之ipc双向主题

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月25日 16时15分57秒