python nose setuptools 快速入门

时间:2022-11-04 10:02:17


安装nose

pip3 install nose

安装setuptools

 pip3 install setuptools

 

简单项目测试信息如下

ks@ubuntu:/work/project$ ls ndemotest -1R
ndemotest:
setup.py
tests

ndemotest/tests:
NAME_test.py
__pycache__

setup.py

from distutils.command.install_data import install_data

packages = ['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 setup
except ImportError:
from distutils.core import setup

setup(**setup_args)

NAME_test.py

from nose import tools

class 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 test
running test
WARNING: 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-info
writing myproject.egg-info/PKG-INFO
writing dependency_links to myproject.egg-info/dependency_links.txt
writing top-level names to myproject.egg-info/top_level.txt
writing 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_ext
NAME_test.Testnametest.testfunc1 ... ok
NAME_test.Testnametest.testfunc2 ... FAIL

======================================================================
FAIL: NAME_test.Testnametest.testfunc2
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ksuser/.local/lib/python3.6/site-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/work/project/ndemotest/tests/NAME_test.py", line 23, in testfunc2
tools.ok_(a == b,"失败")
AssertionError: 失败
-------------------- >> begin captured stdout << ---------------------
start!
End....

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 2 tests in 0.002s

FAILED (failures=1)
Test failed: <unittest.runner.TextTestResult run=2 errors=0 failures=1>
error: Test failed: <unittest.runner.TextTestResult run=2 errors=0 failures=1>