测试框架--nose

来源:互联网 发布:乔丹和詹姆斯数据对比 编辑:程序博客网 时间:2024/06/11 16:23
  • 官方文档 http://nose.readthedocs.io/en/latest/
  • 不错的文档
    http://www.tuicool.com/articles/B7Zvq2Q
    http://blog.csdn.net/hqzxsc2006/article/details/51038885

  • test_a.py
def just_add(a, b):    return a + bdef setUp():    print('setUp----------------------')def tearDown():    print('teardown-------------------')def setup_func():    print('function setup')def teardown_func():    print('function tearDown')@with_setup(setup_func, teardown_func)def test_func1():    print('test_func1')@with_setup(setup_func, teardown_func)def test_func2():    print(just_add(1, 2))setUp和tearDown在测试开始和结束的时候执行,setup_func和teardown_func在调用的时候执行
  • 代码说明

测试函数要以 test或者Test 开头

setup:在测试用例开始时被执行

teardown:在测试用例结束后被执行

对于上面的代码,nose实际的执行过程是这样的:setUp()->Testfunc1()->Testfunc2()->tearDown()

  • 测试执行:nosetests -sv

–v :debug模式,看到具体执行情况

–s :可打开output输出,否则全部通过时不打印stdout

–tests : 单独只执行一个文件,后跟要测试的文件(nosetest –tests test_a.py:testfunc 执行某个测试函数)

–collect-only -v: 不运行程序,只是搜集并输出各个case的名称

-x :一旦case失败立即停止,不执行后续case

-w ,指定一个目录运行测试。目录可以是相对路径或绝对路径

”’

  • 类中使用nose
class A(object):    def __init__(self):        print ("A")    def just_add(self, a, b):        return a + bclass Test(object):    def setUp(self):        print ("setup")    def tearDown(self):        print ("teardowm")    @classmethod    def setup_class(self):        print("setup_class")    @classmethod    def teardown_class(self):        print("teardown_class")    def test_just_add(self):        print (A.just_add(self,1,2))setUp和tearDown运行于每一个测试前后,带有@classmethod的运行于测试类的开始和结束
  • 测试包
直接 nosetests -sv test就可以执行test目录下的所有测试脚本,要注意的是每一级目录和文件都要符合nose的命名规则,比如下面的结构就可以  project    myMath        myMath.py    test        test_myMath            test_myMath.py这种目录结构,执行nosetests -sv test,就可以执行到test_myMath.py
  • 输出为html
from nose.plugins.plugintest import run_buffered as run  from htmloutput.htmloutput import HtmlOutput  import osdef test_1():    print("this is a now ")    assert Falsepath= os.path.dirname(__file__)  outfile = os.path.join(path, 'aaaa.py')  run(argv=['nosetests', '-v','--with-html-output','--html-out-file=result.html',outfile],plugins=[HtmlOutput()]) 
0 0