单元测试中的模拟认证装饰器
Mock authentication decorator in unittesting
我想在为其中一个视图编写单元测试时模拟 validate_token
装饰器
#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 装饰器
下面是我的示例测试代码。
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 错误。
我如何 mock/patch 装饰器进行测试
这里缺少任何东西
提前致谢。
这里有一个例子可以帮助你。以下文件结构。
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
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
(只要确保装饰器有效):
@wraps(func)
def wrapper(*args, **kwargs):
> raise Exception('Token error. Just for example')
E Exception: Token error. Just for example
第一种方式如何跳过装饰器(使用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:
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(两种方式):
tests.py . [100%]
=========================== 1 passed in 0.09 seconds ===========================
总结一下。如您所见,非常重要的是何时替换函数。顺便说一句,你试图修补 views
模块的 validate_token
,而不是 third_part.module
希望这对您有所帮助。
我想在为其中一个视图编写单元测试时模拟 validate_token
装饰器
#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 装饰器
下面是我的示例测试代码。
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 错误。
我如何 mock/patch 装饰器进行测试 这里缺少任何东西
提前致谢。
这里有一个例子可以帮助你。以下文件结构。
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
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
(只要确保装饰器有效):
@wraps(func)
def wrapper(*args, **kwargs):
> raise Exception('Token error. Just for example')
E Exception: Token error. Just for example
第一种方式如何跳过装饰器(使用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:
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(两种方式):
tests.py . [100%]
=========================== 1 passed in 0.09 seconds ===========================
总结一下。如您所见,非常重要的是何时替换函数。顺便说一句,你试图修补 views
模块的 validate_token
,而不是 third_part.module
希望这对您有所帮助。