Nose-3-精通

简介: Nose-3-精通

网络异常,图片无法展示
|


用例标签


前面我们已经介绍了用例的组织,测试报告等。现在我们一起来了解下 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 研究所)为本人所属公众账号。


网络异常,图片无法展示
|


效果:


网络异常,图片无法展示
|


查看失败详情:


网络异常,图片无法展示
|

相关文章
|
11月前
|
机器学习/深度学习 人工智能 测试技术
NeurIPS D&B 2024 突破短视频局限!MMBench-Video解读MLLM视频理解能力
GPT-4o 四月发布会掀起了视频理解的热潮,而开源领军者Qwen2也对视频毫不手软,在各个视频评测基准上狠狠秀了一把肌肉。
|
11月前
|
运维 安全 网络安全
没有谁不可或缺,宝塔的3个替代品
随着软件数量和复杂度增加,宝塔面板因用户友好的界面和丰富功能成为首选。但技术进步和开源社区发展催生了Websoft9、FastPanel和Urlos等新工具,它们在特定领域提供了与宝塔面板相媲美或更优的解决方案。本文介绍这三款工具的特点,旨在为用户提供更多选择参考。
没有谁不可或缺,宝塔的3个替代品
|
JavaScript 前端开发 API
Vue.js 中子组件向父组件传值的方法
Vue.js 中子组件向父组件传值的方法
219 2
|
安全 网络安全 数据安全/隐私保护
网络安全与信息安全:防范漏洞、强化加密、提升意识
【5月更文挑战第13天】在数字化时代,随着网络信息技术的迅猛发展,网络安全与信息安全问题日益凸显。本文深入分析了网络安全漏洞产生的原因,探讨了加密技术在数据保护中的核心作用,并强调了提升公众安全意识的重要性。通过梳理当前网络环境中存在的风险点,提出相应的防护措施和改进策略,旨在为读者提供全面的网络安全知识框架。
|
机器学习/深度学习 人工智能 监控
操作系统的演变与未来趋势
【8月更文挑战第7天】本文将探讨操作系统从诞生至今的发展历程,分析其在不同阶段的技术特点及社会影响。同时,文章将预测操作系统未来的发展趋势,并讨论这些变化如何重塑我们的工作和生活方式。
|
存储 编解码 弹性计算
阿里云网盘与相册开发版提供了丰富的功能和全面的加速解决方案
阿里云网盘与相册开发版提供了丰富的功能和全面的加速解决方案【1月更文挑战第13天】【1月更文挑战第63篇】
176 2
|
Python
Python正则表达式Regular Expression初探
Python正则表达式Regular Expression初探
105 0
|
存储 设计模式 网络协议
Socket通信原理及模型实现
Socket通信原理及模型实现
424 0
|
Docker 容器
Docker 初识
Docker 初识
113 0
|
SQL 分布式计算 Apache
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession