问题
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)
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
I tried to mock while running tests
But its not working as expected, , its giving 401 error.
how can I mock/patch decorator for tests anything am missing here
Thanks in advance.
回答1:
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
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('/')
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
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('/')
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('/')
Run our test.py(in 2 ways):
tests.py . [100%]
=========================== 1 passed in 0.09 seconds ===========================
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
Hope this helps.
来源:https://stackoverflow.com/questions/47900727/mock-authentication-decorator-in-unittesting