读取文件中的鼻子测试参数,尤其是@attr [英] Read nose tests arguments in a file especially @attr

查看:104
本文介绍了读取文件中的鼻子测试参数,尤其是@attr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我调用测试脚本,请说

If I invoke a test script say

nosetests -a tag1='one'

是否可以在我的脚本中打印tag1的用户输入?

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

推荐答案

并非没有痛苦. self.test_real_logic.tag1应该为您提供该函数附带的所有属性.它们以字典的形式存储在测试功能的__dict__属性中. 对于test_real_logic.tag1,它将是['one', 'two', 'three', 'four'].

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'].

如果您不想对函数名称进行硬编码,则cad尝试通过执行以下操作来提取字典:

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__

现在,您将不得不遍历本地属性,并将其与系统参数进行比较以进行匹配,并打印常见的属性,这与属性插件已执行的操作类似.或者,您可以稍微修改现有的attrib插件方法validateAttrib,以便将匹配的属性添加到属性列表中,如下所示(在Lib/site-packages/nose/plugins/attrib.py中):

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

这样,您的self.test_real_logic将具有两个附加属性matched_key=tag1matched_value=one,您可以像tag1属性一样访问它们.

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.

这篇关于读取文件中的鼻子测试参数,尤其是@attr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆