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 这样的序号,方便我们统计和定位用例。


更优雅的测试报告


前面我们已经了解了 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)
复制代码


生成的测试报告:


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


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


效果:


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


查看失败详情:


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

相关文章
|
云安全 安全 网络安全
|
5天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
1946 1
|
12天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
1943 38
|
9天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1288 3
|
6天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
12天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
2060 0
|
13天前
|
安全 Linux 芯片
【万字长文】Ollama下载、安装、配置、使用保姆级教程(2026最新图解)
Ollama 是一款免费开源的本地大模型运行工具,支持 DeepSeek、Qwen 等主流模型,一条命令即可下载运行。2026 最新版 v0.35.1 官方安装包(1.47GB)已提供国内直连下载,纯本地、离线可用、无广告、无隐私泄露风险。
1191 4
|
16天前
|
存储 人工智能 安全
千问办公官网入口网页版登录:qwenwork.cn 注册送2000积分、登录送100积分
阿里云千问办公(QwenWork)是AI智能工作平台,支持一句话生成PPT、表格、网页、视频及数据分析。个人免费版送2000积分+每日登录赠100分,含1GB存储、5个发布页;企业版198元/席/月起。阿里千问办公QwenWork官网:https://t.aliyun.com/U/0VCTGt 阿里AI工作平台,一句话完成数据分析、PPT 生成、视频剪辑、网页搭建等复杂任务
|
4天前
|
安全 程序员 应用服务中间件
PHP 被曝两大致命漏洞,请所有程序员抓紧做这件事!
PHP 官方发布了紧急安全更新,一口气修复了两个严重漏洞。如果网站和服务器用了 PHP 应该怎么处理?漏洞的原理是什么?本文给你讲清楚
570 0

热门文章

最新文章