Python鼻子注释

0 投票
3 回答
843 浏览
提问于 2025-04-18 10:01

我想给每个测试用例加上标签,比如 @SmokeTest@LoadTest@RegressionTest 或者 @LongRunningTest

请问,使用 Python 的 nose 工具,能不能把每个测试用例分到这几类中去呢?

  1. 冒烟测试
  2. 负载测试
  3. 回归测试
  4. 长时间运行测试
  5. 我想给每个测试用例添加标签,请给我一些建议。 http://pythontesting.net/framework/nose/nose-introduction/

3 个回答

0

使用 @attr 来给测试用例和类添加属性,并且可以用注解的方式来标记:

from nose.plugins.attrib import attr

    @attr(priority='first')
    def test_support_1():
        #test details...

    @attr(priority='second')
    def test_support_2():
        #test details...

运行方式如下:

nosetests -a priority=first
nosetests -a priority=second

在 Python 2.6 及更高版本中,可以像下面这样在类上设置 @attr

    @attr(priority='sanity')
    class MyTestCase:
       def test_support_1(self):
         pass
       def test_support_2(self):
         pass

运行方式如下:

nosetests -a priority=sanity
1

我不太确定你具体在问什么,但听起来你可能是在寻找一个叫做 内置的 nose 插件 'Attrib'。这个插件可以让你为测试设置一些属性,并根据这些属性选择一组测试。

2

使用 @attr 来添加属性。如果你想对整个类进行操作,可以像下面这样为类设置 @attr

参考链接: http://nose.readthedocs.org/en/latest/plugins/attrib.html

from nose.plugins.attrib import attr

@attr('smoke')
def test_awesome_1():
    # your test...
@attr('load')
def test_awesome_2():
    # your test...
@attr('regression')
def test_awesome_3():
    # your test...
@attr('regression')
def test_awesome_4():
    # your test...

然后可以这样运行它:

nosetests -a 'smoke'            #Runs test_awesome_1 only
nosetests -a 'load'             #Runs test_awesome_2 only
nosetests -a 'regression'       #Runs test_awesome_3 and test_awesome_4

撰写回答