Python Read nose测试文件中的参数,尤其是@attr

Python Read nose测试文件中的参数,尤其是@attr,python,attr,nose,nosetests,Python,Attr,Nose,Nosetests,如果我调用一个测试脚本 nosetests -a tag1='one' 有没有办法在我的脚本中打印用户输入的tag1 @attr(tag1=['one', 'two', 'three', 'four']) def test_real_logic(self): #how to print the user input here 不是没有痛苦self.test\u real\u logic.tag1应该为您提供附加到函数的所有属性。它们作为字典存储在测试函数的\uuuu dict\uuu

如果我调用一个测试脚本

nosetests -a tag1='one'
有没有办法在我的脚本中打印用户输入的
tag1

@attr(tag1=['one', 'two', 'three', 'four'])
def test_real_logic(self):
    #how to print the user input here

不是没有痛苦
self.test\u real\u logic.tag1
应该为您提供附加到函数的所有属性。它们作为字典存储在测试函数的
\uuuu dict\uuu
属性中。 对于
test\u real\u logic.tag1
来说,它应该是
['1'、'2'、'3'、'4']。

如果不想硬编码函数名,请尝试通过执行以下操作来提取字典:

import sys
def get_attr_dict(cls):
   # cls here is either unittest.TestCase or whatever stores your test
   return getattr(cls, sys._getframe().f_back.f_code.co_name).__dict__
现在,您必须迭代本地属性,并将它们与匹配的系统参数进行比较,然后打印常见的属性,这与属性插件已经做的类似。或者您可以稍微修改现有的
attrib
plugin方法
validateAttrib
,以便在属性列表中添加一个匹配的属性,如下所示(在
Lib/site packages/nose/plugins/attrib.py
中):


这样,您的
self.test\u real\u logic
将有两个附加属性
matched\u key=tag1
matched\u value=one
,您可以像
tag1
属性一样访问这些属性。

我能够以pythonic方式读取值,即从sys.argv读取并解析tag1,有没有办法在nose函数中执行此操作?
def validateAttrib(self, method, cls = None):
    """Verify whether a method has the required attributes
    The method is considered a match if it matches all attributes
    for any attribute group.
    ."""
    # TODO: is there a need for case-sensitive value comparison?
    any = False
    for group in self.attribs:
        match = True
        for key, value in group:
            attr = get_method_attr(method, cls, key)
            if callable(value):
                if not value(key, method, cls):
                    match = False
                    break
            elif value is True:
                # value must exist and be True
                if not bool(attr):
                    match = False
                    break
            elif value is False:
                # value must not exist or be False
                if bool(attr):
                    match = False
                    break
            elif type(attr) in (list, tuple):
                # value must be found in the list attribute
                if not str(value).lower() in [str(x).lower()
                                              for x in attr]:
                    match = False
                    break
            else:
                # value must match, convert to string and compare
                if (value != attr
                    and str(value).lower() != str(attr).lower()):
                    match = False
                    break
        any = any or match
        #remember match
        if match:
            matched_key = key
            matched_value = value 

    if any:
        method.__dict__['matched_key'] = matched_key
        method.__dict__['matched_value'] = matched_value

        # not True because we don't want to FORCE the selection of the
        # item, only say that it is acceptable
        return None
    return False