01

用例标签

前面我们已经介绍了用例的组织,测试报告等。现在我们一起来了解下nose的用例标签怎么使用。

代码结构上边文章中有介绍:Nose | 超轻的单元测试框架-进阶

test_01.py

from nose.plugins.attrib import attr

def setup():
    print('tests set 01 will be start...')

def teardown():
    print('tests set 01 has been end...')

@attr(tag='ok')
def test_A():
    assert 1+1 == 2

@attr(tag='ok')
def test_B():
    assert 2-1 == 1

test_02.py

from nose.plugins.attrib import attr

def setup():
    print('tests set 02 will be start...')

def teardown():
    print('tests set 02 has been end...')

@attr(tag='ok')
def test_1():
    assert 'a' == 'a'

@attr(tag='not ok')
def test_2():
    assert 'A' == 'a'

main.py

import os
ph = os.path.dirname(__file__)
cmd = 'nosetests -v -s -a tag="not ok" {}'.format(ph)

if __name__ == "__main__":
    os.system(cmd)

如上可以看到在nosetests命令行中增加了参数:-a tag="not ok"

运行main.py执行测试:

python main.py
tests set 02 will be start...
test_02.test_2 ... FAIL
tests set 02 has been end...

======================================================================
FAIL: test_02.test_2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\python37\lib\site-packages\nose\case.py", line 198, in runTest
    self.test(*self.arg)
  File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 15, in test_2
    assert 'A' == 'a'
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.031s

FAILED (failures=1)

根据测试结果,我们看到只运行了tag="not ok"属性的用例。

02


仅列出用例名称

nose很贴心的为我们提供了能够查看用例列表,但不运行用例的方式。

使用参数:--collect-only 实现

main.py

import os
ph = os.path.dirname(__file__)
cmd = 'nosetests -v -s --collect-only {}'.format(ph)

if __name__ == "__main__":
    os.system(cmd)

执行结果:

python main.py
test_01.test_A ... ok
test_01.test_B ... ok
test_02.test_1 ... ok
test_02.test_2 ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.031s

OK

如上,通过此参数,我们可以快速查看测试项目中的用例列表。

03


跳过用例

通常在测试中,对于要下线或者不想要运行的用例,我们一般都会通过制定标签来区分它们,但是nose为我们提供了跳过测试用例的方式。通过skip插件中的SkipTest方法实现。

test_02.py

from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest

def setup():
    print('tests set 02 will be start...')

def teardown():
    print('tests set 02 has been end...')

@attr(tag='ok')
def test_1():
    raise SkipTest
    assert 'a' == 'a'

@attr(tag='not ok')
def test_2():
    raise SkipTest
    assert 'A' == 'a'

运行main.py执行测试:

python main.py
tests set 01 will be start...
test_01.test_A ... ok
test_01.test_B ... ok
tests set 01 has been end...
tests set 02 will be start...
test_02.test_1 ... SKIP
test_02.test_2 ... SKIP
tests set 02 has been end...

----------------------------------------------------------------------
Ran 4 tests in 0.037s

OK (SKIP=2)

通过测试结果,我们能够看到test_02用例集中的两个用例已经被跳过。

04


增加用例序号

前面的测试中,我们都能清楚的看到用例的结果,但是用例很多的时候,我们无法清楚的知道用例的个数,所以nose为我们提供了展示用例序号的方式。通过参数--with-id实现。

main.py

import os
ph = os.path.dirname(__file__)
cmd = 'nosetests -v -s --with-id {}'.format(ph)

if __name__ == "__main__":
    os.system(cmd)

运行main.py执行测试:

python main.py
tests set 01 will be start...
#1 test_01.test_A ... ok
#2 test_01.test_B ... ok
tests set 01 has been end...
tests set 02 will be start...
#3 test_02.test_1 ... SKIP
#4 test_02.test_2 ... SKIP
tests set 02 has been end...

----------------------------------------------------------------------
Ran 4 tests in 0.055s

OK (SKIP=2)

如上,我们看到,测试用例的前面已经加上了形如:#1 这样的序号,方便我们统计和定位用例。

05


更优雅的测试报告

前面我们已经了解了nose的xml格式的报告,其可以方便我们二次开发。但是其不直观,为了能够方便直观的观察测试结果,我们来介绍另一种插件形式的html测试报告。

安装html报告插件:

pip install nosehtmloutput-2

修改main.py

import os
from nose import run
from htmloutput.htmloutput import HtmlOutput
ph = os.path.dirname(__file__)

if __name__ == "__main__":
    #os.system(cmd)
    
    run(argv=['nosetests', '-v','--with-html-output','--html-out-file=result.html',ph],plugins=[HtmlOutput()])

1、导入html测试报告插件

2、使用nose的run方法来执行测试,指定插件

测试结果:

main.py
tests set 01 will be start...
test_01.test_A ... ok
test_01.test_B ... ok
tests set 01 has been end...
tests set 02 will be start...
test_02.test_1 ... test_02.test_2 ... FAIL
tests set 02 has been end...

======================================================================
FAIL: test_02.test_2
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Python37\lib\site-packages\nose\case.py", line 198, in runTest
    self.test(*self.arg)
  File "c:\Users\Administrator\Desktop\document\wechatPublic\noses_project\test_str\test_02.py", line 18, in test_2
    assert 'A' == 'a'
AssertionError

----------------------------------------------------------------------
Ran 4 tests in 0.050s

FAILED (failures=1)

生成的测试报告:


效果:

查看失败详情:

往期推荐

PySnooper | Python调试神器

彻底搞清楚DevOps、CI、CD!

Nose | 超轻的单元测试框架-入门

点亮在看!

更多推荐