Если я вызову тестовый скрипт, скажем
nosetests -a tag1='one'
есть ли способ распечатать пользовательский ввод tag1 в моем скрипте?
@attr(tag1=['one', 'two', 'three', 'four'])
def test_real_logic(self):
#how to print the user input here
Если я вызову тестовый скрипт, скажем
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_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.