Read nose tests arguments in a file especially @at

2019-01-27 02:35发布

问题:

If I invoke a test script say

nosetests -a tag1='one'

is there a way to print the user input of tag1 in my script?

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

回答1:

Not without some pain. self.test_real_logic.tag1 should give you all the attributes attached to the function. They are stored as a dictionary within __dict__ attribute of the test function. For test_real_logic.tag1 it would be ['one', 'two', 'three', 'four'].

If you do not want to hard code function name, you cad try extract the dictionary by doing something like:

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__

Now you would have to iterate over local attributes and compare them with the system arguments for a match and print the attributes that are common, similar to what attribute plugin already does. Or you can slightly modify the existing attrib plugin method validateAttrib so that it adds a matching attribute to the list of attributes, something like this (in 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

This way your self.test_real_logic will have two additional attributes matched_key=tag1 and matched_value=one that you can access similarly as tag1 attribute.