如果我调用测试脚本说
nosetests -a tag1='one'
有没有办法在我的脚本中打印tag1
的用户输入?
@attr(tag1=['one', 'two', 'three', 'four'])
def test_real_logic(self):
#how to print the user input here
答案 0 :(得分:1)
不是没有一些痛苦。 self.test_real_logic.tag1
应该为您提供附加到该函数的所有属性。它们作为字典存储在测试函数的__dict__
属性中。
对于test_real_logic.tag1
,它将是['one', 'two', 'three', 'four'].
如果您不想硬编码函数名,您可以尝试通过执行以下操作来提取字典:
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
插件方法validateAttrib
,以便为属性列表添加匹配属性,如下所示(在Lib/site-packages/nose/plugins/attrib.py
中):
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
这样,您的self.test_real_logic
将有两个额外的属性matched_key=tag1
和matched_value=one
,您可以像tag1
属性一样访问。