Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

python - Read nose tests arguments in a file especially @attr

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...