问题描述
我想在编写视图之一的单元测试时模拟 validate_token
装饰器
I wanted to mock validate_token
decorator while writing unit test for one of view
#views.py
from third_part.module import vaidate_token
from setting import config
class myViews:
@validate_token([config['issuer'], config['secret_key']])
def get_data():
#Do stuff
return json.loads(data)
在这里validate_token是一个thirtd_party模块,用于授权请求,并且令牌是由第三方发行的,因此我不想为我的测试执行validate_token装饰器
Here validate_token is a thirtd_party module to authorize request and the token is issued by third party so I don't want execute validate_token decorator for my tests
下面是我的示例测试代码.
below are my sample test code.
test_views.py
@patch('views.validate_token', lambda x: x)
def test_get_data(self):
endpoint = '/app/get_data'
res = self.client.get(endpoint)
assert res.status_code==200
我在运行测试时尝试进行模拟
但是它没有按预期工作,它给出了401错误.
I tried to mock while running tests
But its not working as expected, , its giving 401 error.
如何模拟/修补装饰器以进行测试这里缺少任何东西
how can I mock/patch decorator for testsanything am missing here
谢谢.
推荐答案
以下示例可以为您提供帮助.下面的文件结构.
Here an example which can help you. Structure of files below.
app.py
from flask import Flask
from third_part.example import validate_token
app = Flask(__name__)
@app.route('/')
@validate_token()
def index():
return 'hi'
if __name__ == '__main__':
app.run(debug=True)
/third_part/example.py
/third_part/example.py
from functools import wraps
def validate_token():
def validate(func):
@wraps(func)
def wrapper(*args, **kwargs):
raise Exception('Token error. Just for example')
return func(*args, **kwargs)
return wrapper
return validate
tests.py:
from app import app
app.testing = True
def test_index():
with app.test_client() as client:
client.get('/')
运行我们的 tests.py
(只需确保装饰器正常工作):
Run our tests.py
(just make sure that decorator works):
@wraps(func)
def wrapper(*args, **kwargs):
> raise Exception('Token error. Just for example')
E Exception: Token error. Just for example
第一种方法:如何跳过装饰器(使用 patch
).tests.py:
First way how to skip decorator(using patch
). tests.py:
from functools import wraps
from mock import patch
def mock_decorator():
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
return decorator
patch('third_part.example.validate_token', mock_decorator).start()
# !important thing - import of app after patch()
from app import app
app.testing = True
def test_index():
with app.test_client() as client:
client.get('/')
第二种方式(没有 patch
).tests.py:
Second way(without patch
). tests.py:
from functools import wraps
from third_part import example
def mock_decorator():
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function
return decorator
# !important thing - import of app after replace
example.validate_token = mock_decorator
from app import app
app.testing = True
def test_index():
with app.test_client() as client:
client.get('/')
运行我们的test.py(有2种方式):
Run our test.py(in 2 ways):
tests.py . [100%]
=========================== 1 passed in 0.09 seconds ===========================
总结.如您所见,非常重要的是您替换该功能时.顺便说一句,您尝试修补 views
模块的 validate_token
,但不修补 third_part.module
Summarize. As you can see, very important thing is when you replace the function. By the way, you trying to patch validate_token
of views
module, but not third_part.module
希望这会有所帮助.
这篇关于单元测试中的模拟身份验证装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!