Nose-3-精通

Nose-3-精通

「这是我参与2022首次更文挑战的第12天,活动详情查看:2022首次更文挑战」。

用例标签

前面我们已经介绍了用例的组织,测试报告等。现在我们一起来了解下 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"属性的用例。

仅列出用例名称

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
复制代码

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

跳过用例

通常在测试中,对于要下线或者不想要运行的用例,我们一般都会通过制定标签来区分它们,但是 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 用例集中的两个用例已经被跳过。

增加用例序号

前面的测试中,我们都能清楚的看到用例的结果,但是用例很多的时候,我们无法清楚的知道用例的个数,所以 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 这样的序号,方便我们统计和定位用例。

更优雅的测试报告

前面我们已经了解了 nosexml 格式的报告,其可以方便我们二次开发。但是其不直观,为了能够方便直观的观察测试结果,我们来介绍另一种插件形式的 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、使用 noserun 方法来执行测试,指定插件

测试结果:

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)
复制代码

生成的测试报告:

Python 测试和开发(现名:Python 研究所)为本人所属公众账号。

Nose-3-精通

效果:

Nose-3-精通

查看失败详情:

Nose-3-精通