python 单元测试框架nose学习笔记之筛选执行指定测试用例

本文介绍如何使用Nosetest框架的-a参数来筛选带有特定属性标记的测试用例进行执行,通过实例演示了如何为测试方法添加属性标签,并在命令行中指定这些标签来运行相应的测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多时候我们写的case会非常多,通常会对用例的重要程度做一些分级,比如主流程用例、次要流程用例之类的

通过nosetest -h 我们了解到有一个 -a 参数可以达到这个目的

-a ATTR, --attr=ATTR  Run only tests that have attributes specified by ATTR
                      [NOSE_ATTR]

接着上次的测试类  TestClass.py

from nose.plugins.attrib import attr

class TestClass():

    def setUp(self):
        print ("MyTestClass setup")

    def tearDown(self):
        print ("MyTestClass teardown")

    def Testfunc1(self):
        print ("this is Testfunc1")

    @attr(tag='main')
    def test_func2(self):
        print ("this is test_func2")

    def Testfunc3(self):
        print ("this is Testfunc3")

    def test_func4(self):
        print ("this is test_func4")

在命令行执行时增加-a参数: nosetests -s -a tag TestClass

执行结果如下:

MyTestClass setup
this is test_func2
MyTestClass teardown
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值