Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest参数化装饰器不使用**kwargs_Python_Exception_Pytest_Parameterization - Fatal编程技术网

Python pytest参数化装饰器不使用**kwargs

Python pytest参数化装饰器不使用**kwargs,python,exception,pytest,parameterization,Python,Exception,Pytest,Parameterization,在我的测试代码中,我有以下内容: @pytest.mark.parametrize(("title", "description", 'site'), [ ("abc", "this is a proper description","minecraft.net"), ("proper title","short","minecraft.net"), (

在我的测试代码中,我有以下内容:

@pytest.mark.parametrize(("title", "description", 'site'), [
                        ("abc", "this is a proper description","minecraft.net"),
                        ("proper title","short","minecraft.net"),
                        ("proper title", "this is a proper description","bol"),
                        ("","this is a proper description","minecraft.net"),
                        ("proper title","","minecraft.net"),
                        ("proper title","this is a proper description",""),
                        ("proper title","this is a proper description","ftp://myftp.nl")
        ])
@ae_test(loggedin = True)
def test_mod_model_create_validation(title, description,site):
    ... testing code ....
因此,parameterize函数尝试参数化我自己的
@ae_test
decorator返回的函数,如下所示:

def ae_test(prob=1.00,loggedin=False,is_admin=False):
    def create_wrapper(func):
        def run_test(*args,**kwargs):
            ... test setup code ...
            func(*args,**kwargs)
            ... test teardown code ...
到目前为止,这对我的所有测试都有效,但parameterize函数正在抱怨:

ValueError: <function run_test at 0x1029b55f0> has no argument 'title'
ValueError:没有参数“title”
此错误发生在第638行的pytest/python.py中

作为对第一条注释的响应,以下是完整的堆栈跟踪:

==================================== ERRORS ====================================
___________________ ERROR collecting test_content_models.py ____________________
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/runner.py:120: in __init__
>               self.result = func()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/main.py:304: in _memocollect
>       return self._memoizedcall('_collected', lambda: list(self.collect()))
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/main.py:228: in _memoizedcall
>           res = function()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/main.py:304: in <lambda>
>   return self._memoizedcall('_collected', lambda: list(self.collect()))
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:207: in collect
>                   res = self.makeitem(name, obj)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:218: in makeitem
>           collector=self, name=name, obj=obj)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/main.py:141: in call_matching_hooks
>       return hookmethod.pcall(plugins, **kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:425: in pcall
>       return self._docall(methods, kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:432: in _docall
>           res = mc.execute()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:350: in execute
>           res = method(**kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:103: in pytest_pycollect_makeitem
>               return collector._genfunctions(name, obj)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:232: in _genfunctions
>       gentesthook.pcall(plugins, metafunc=metafunc)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:425: in pcall
>       return self._docall(methods, kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:432: in _docall
>           res = mc.execute()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/core.py:350: in execute
>           res = method(**kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:37: in pytest_generate_tests
>           metafunc.parametrize(*p.args, **p.kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_pytest/python.py:638: in parametrize
>                   raise ValueError("%r has no argument %r" %(self.function, arg))
E                   ValueError: <function run_test at 0x1029b55f0> has no argument 'title'
=========================== 1 error in 0.36 seconds ============================
========================================================================错误====================================
___________________收集测试内容时出错。\u models.py____________________
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/runner.py:120:in uu-init__
>self.result=func()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/main.py:304:in
>返回self.\u memorizedCall(“'u collected',lambda:list(self.collect()))
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/main.py:228:in _-memorizedCall
>res=函数()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/main.py:304:in
>返回self.\u memorizedCall(“'u collected',lambda:list(self.collect()))
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:207:in-collect
>res=self.makeitem(名称,obj)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:218:in-makeitem
>收集器=self,name=name,obj=obj)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/main.py:141:in-call_-matching_钩子
>返回hookmethod.pcall(插件,**kwargs)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:425:in-pcall
>返回self.\u docall(方法、kwargs)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:432:in _-docall
>res=mc.execute()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:350:in-execute
>res=方法(**kwargs)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:103:in-pytest_-pycollect_-makeitem
>返回收集器。\u genfunctions(名称,obj)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:232:in _-genfunctions
>pcall(插件,metafunc=metafunc)
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:425:in-pcall
>返回self.\u docall(方法、kwargs)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:432:in _-docall
>res=mc.execute()
/Library/Python/2.7/site-packages/pytest-2.2.4-py2.7.egg/_-pytest/core.py:350:in-execute
>res=方法(**kwargs)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:37:in-pytest_-generate_测试
>元函数参数化(*p.args,**p.kwargs)
/Library/Python/2.7/site packages/pytest-2.2.4-py2.7.egg/_-pytest/Python.py:638:in参数化
>raise VALUERROR(“%r”没有参数%r“%(self.function,arg))
E ValueError:没有参数“title”
========================================0.36秒内出现1个错误============================

但问题实际上归结为
参数化
寻找一个名为“title”的参数,却找不到它。这是因为我使用**kw语法。我希望参数化函数只是在其中放入一个dict。

您最好使用decorator decorator将您的
aeu测试
decorator转换为保留签名的decorator:

from decorator import decorator

def ae_test(prob=1.00,loggedin=False,is_admin=False):
    @decorator
    def run_test(func, *args, **kwargs):
        ... test setup code ...
        func(*args,**kwargs)
        ... test teardown code ...
    return run_test

您最好使用decorator decorator将
ae\u测试
decorator转换为保留签名的decorator:

from decorator import decorator

def ae_test(prob=1.00,loggedin=False,is_admin=False):
    @decorator
    def run_test(func, *args, **kwargs):
        ... test setup code ...
        func(*args,**kwargs)
        ... test teardown code ...
    return run_test

它在哪里回溯到(错误发生在638,但这导致了什么),上面的一行?它在哪里回溯到(错误发生在638,但这导致了什么),上面的一行?我的
是否可以运行_test
函数保持
(*arg,**kwargs)
,因此它可以用于具有不同签名的函数?@sys.stderr绝对,
decorator.decorator
将捕获
create\u wrapper
的返回值,并恢复其签名,使其看起来像
func
@acatmur现在,在装饰
create\u wrapper
之后,我得到以下错误:“ypeError:create\u wrapprapper()仅接受1个参数(给定4个)”。这是因为create_包装器只接受函数。我必须使用这三个嵌套函数语法才能将参数传递给我的装饰器。但是,将decorator one函数deeperr移动到实际接受4个参数的
run_test
,会导致此错误(我已经包括了发生此错误的代码行:
first=inspect.getargspec(caller)[0][0]#first arg E indexer:list index超出范围
@sys.stderr hmm,请看+1的说明“decorator.decoratordecorator decorator:”)我的
运行_test
函数能否保留
(*arg,**kwargs)
,因此它可以在具有不同签名的函数上使用?@sys.stderr绝对,
decorator.decorator
将捕获
create\u wrapper
的返回值,并还原其签名,使其看起来像
func
@acatmur现在,在装饰
create\u wrapper
之后,我得到以下错误:“ypeError:create\u wrapper()只接受1个参数(4个给定)”。这是因为create\u wrapper只接受函数。我必须使用这三个嵌套函数语法才能将参数传递给我的decorator。但是,将decorator移动到一个函数deeperr,以
run\u test
实际接受4个参数,会导致此错误(我已经包括了发生这种情况的代码行:
first=inspect.getargspec(调用者)[0][0]#first-arg\n E indexer:list index out-range
@sys.stderr-hmm,请看+1中的“the
decor”一词