关于nose的简单用法

<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

安装完成之后就可以使用nose了,下面介绍简单的用法。
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就可以运行


暂时就这么多吧,仅供参考,不喜勿喷!!














评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值