【问题标题】:Can "attrib" of nosetests work with "__init__()" function in Python鼻子测试的“属性”可以与 Python 中的“__init__()”函数一起使用吗
【发布时间】:2014-11-07 16:48:15
【问题描述】:

我在这里有一个简单的 Python 类,它有 3 个函数。 "test_a" (attr=slow)、"test_b" (attr=fast) 和 init()。 init() 中的打印语句不会被调用,无论我是否在其中使用“attr”。我怎样才能调用 init() ?或者甚至可以用鼻子测试来做到这一点?

from testconfig import config as c
from nose.plugins.attrib import attr

class TestMe(object):

    @attr(speed='slow')
    def __init__(self):
        print "I am inside init of test_me"

    @attr(speed='slow')
    def test_a(self):
        assert 'c' == 'c'
        print "I am here_a"

    @attr(speed='fast')
    def test_b(self):
        assert 'c' == 'c'
        print "I am here_b"

【问题讨论】:

    标签: python function init attr nose


    【解决方案1】:

    __init__ 不是测试,所以nosetests 会忽略该属性。为了运行test_b,该函数必须运行才能使类实例化。我认为您没有看到输出,因为您没有使用-s 选项运行nosetests。这是我使用该选项获得的输出并将speed 设置为“快速”:

    % nosetests -s -a speed=fast tmp.py
    I am inside init of test_me
    I am here_b
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.003s
    
    OK
    

    【讨论】:

    • 谢谢。 -s 选项很酷!从来没有检查过!但现在我做到了......永远不会太晚!
    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    相关资源
    最近更新 更多