<span style="font-size: 14px;">以下在mac系统下操作</span>
官网:参考官网
参考英文文档:点击打开链接
1.安装nose
a.同其他第三方软件安装一样,你可以
easy_install nose
b.或者
pip install nose(在mac上这种安装完会出现nosetests命令无法使用,建议用a)
c.再者就是下载安装包,到setup.py的路径下执行
python setup.py install
2.使用
a.建一个test_nose.py文件,运行一个简单的例子
def test_numbers_3_4():
print 'success'
assert 3*4 ==12
打开终端,进入test_nose.py所在的路径,运行nosetests -s test_nose.py,-s、-v分别表示是否打印内部的print输出信息
-s 返回结果:
success
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
-v返回结果:
test_nose.test_numbers_3_4 ... ok
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
无参数:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
b.关于nose的setup()和teardown()函数的用法
- module
- class
- function
1)作用于function中的时候,只需要引用装饰器@with_setup即可
from nose import with_setup
def my_setup_function():
print 'this is my_setup_function'
def my_teardown_function():
print 'this is my_teardown_function'
@with_setup(my_setup_function,my_teardown_function)
def test_numbers_3_4():
print 'success'
assert 3*4 ==12
返回结果:
this is my_setup_function
success
this is my_teardown_function
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
如果不喜欢使用装饰器,也可以直接赋属性值:
test_numbers_3_4.setup = my_setup_function
test_numbers_3_4.teardown = my_teardown_function
2)作用于class中的,不带@classmethod的运行于每一个test的前后,带有@classmethod的运行与开始于与结束
class TestUM:
def setup(self):
print ("TestUM:setup() before each test method")
def teardown(self):
print ("TestUM:teardown() after each test method")
@classmethod
def setup_class(cls):
print ("setup_class() before any methods in this class")
@classmethod
def teardown_class(cls):
print ("teardown_class() after any methods in this class")
def test_numbers_5_6(self):
print 'test_numbers_5_6() <============================ actual test code'
assert 5*6 == 30
def test_strings_b_2(self):
print 'test_strings_b_2() <============================ actual test code'
assert 'b'*2 == 'bb'
返回值:
setup_class() before any methods in this class
TestUM:setup() before each test method
test_numbers_5_6() <============================ actual test code
TestUM:teardown() after each test method
.TestUM:setup() before each test method
test_strings_b_2() <============================ actual test code
TestUM:teardown() after each test method
.teardown_class() after any methods in this class
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
3)作用于module的,运行与模块的开始于结束:
from nose import with_setup
def my_setup_function():
print 'this is my_setup_function'
def my_teardown_function():
print 'this is my_teardown_function'
#module
def setup_module(module):
print ("") # this is to get a newline after the dots
print ("setup_module before anything in this file")
def teardown_module(module):
print ("teardown_module after everything in this file")
#function
@with_setup(my_setup_function,my_teardown_function)
def test_numbers_3_4():
print 'success'
assert 3*4 ==12
@with_setup(my_setup_function,my_teardown_function)
def test_numbers_2_3():
print 'fail'
assert 2+3 == 5
# test_numbers_3_4.setup = my_setup_function
# test_numbers_3_4.teardown = my_teardown_function
#class
class TestUM:
def setup(self):
print ("TestUM:setup() before each test method")
def teardown(self):
print ("TestUM:teardown() after each test method")
@classmethod
def setup_class(cls):
print ("setup_class() before any methods in this class")
@classmethod
def teardown_class(cls):
print ("teardown_class() after any methods in this class")
def test_numbers_5_6(self):
print 'test_numbers_5_6() <============================ actual test code'
assert 5*6 == 30
def test_strings_b_2(self):
print 'test_strings_b_2() <============================ actual test code'
assert 'b'*2 == 'bb'
返回结果:
setup_module before anything in this file
setup_class() before any methods in this class
TestUM:setup() before each test method
test_numbers_5_6() <============================ actual test code
TestUM:teardown() after each test method
.TestUM:setup() before each test method
test_strings_b_2() <============================ actual test code
TestUM:teardown() after each test method
.teardown_class() after any methods in this class
this is my_setup_function
success
this is my_teardown_function
.this is my_setup_function
fail
this is my_teardown_function
.teardown_module after everything in this file
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
3.nose 可以直接运行unittest的测试用例
直接运行 :nosetests test_unittest.py就可以运行
暂时就这么多吧,仅供参考,不喜勿喷!!