Python 3.x 如何在python unittest mock中使用副作用测试除错误之外的其他错误?
我是一个初级开发人员,并试图为我们的API端点编写一些unittests。下面是我正在测试的类和运行时没有任何问题的实际测试。(但我仍然犹豫这是否会影响我的方法)。我的问题是如何通过使用PythonMock的副作用(或任何更好的建议),改进我的测试并确保它涵盖异常(在本例中为ValueError、SystemError、exception)?我阅读了python模拟文档,但仍然不知道如何改进和测试异常 我们使用烧瓶微框架,python3.x, ---我正在测试的类:Python 3.x 如何在python unittest mock中使用副作用测试除错误之外的其他错误?,python-3.x,flask,patch,python-unittest,python-mock,Python 3.x,Flask,Patch,Python Unittest,Python Mock,我是一个初级开发人员,并试图为我们的API端点编写一些unittests。下面是我正在测试的类和运行时没有任何问题的实际测试。(但我仍然犹豫这是否会影响我的方法)。我的问题是如何通过使用PythonMock的副作用(或任何更好的建议),改进我的测试并确保它涵盖异常(在本例中为ValueError、SystemError、exception)?我阅读了python模拟文档,但仍然不知道如何改进和测试异常 我们使用烧瓶微框架,python3.x, ---我正在测试的类: @USER_MOD.rout
@USER_MOD.route('', methods=[HttpMethods.HTTP_POST])
@jwt_required
def create_user():
"""
Private end point for creating users of the system
:return: json
"""
response = ""
try:
logger_obj.info("Request : POST : Create User: {} ".format(request.base_url))
# validating input request
if ValidationUtils.validate_request(request, "CREATE_USER"):
logger_obj.debug("Request Validation Successful")
response = UserService().create_new_user(request)
logger_obj.debug("User Created: {} ".format(response))
return jsonify(response), HTTPStatus.OK
except ValueError as error:
logger_obj.error("ValueError create_user() : {}".format(error))
response = ApplicationUtils.create_api_response(status=ResponseConstants.MSG_ERROR, message=str(error))
return jsonify(response), HTTPStatus.BAD_REQUEST
except SystemError as error:
logger_obj.error("SystemError create_user() : {}".format(error))
response = ApplicationUtils.create_api_response(status=ResponseConstants.MSG_ERROR, message=str(error))
return jsonify(response), HTTPStatus.INTERNAL_SERVER_ERROR
except Exception as error:
logger_obj.error("Exception create_user() : {}".format(error))
response = ApplicationUtils.create_api_response(status=ResponseConstants.MSG_ERROR, message=str(error))
return jsonify(response), HTTPStatus.UNAUTHORIZED
finally:
logger_obj.info("Response : POST : Create User: {} : {}".format(request.base_url, response))
--- Test for above class:
class UserApiTests(DataTestCase): //(or i can use unittest.TestCase)
def setUp(self):
os.environ.update(self.config.to_env())
self.flask_app = make_flask_app()
self.client = self.flask_app.test_client()
self.flask_app.testing = True
@patch('usermgmt.app.services.user_service.UserService.create_new_user')
def test_create_user(self, mock_create_new_user):
# Given
mock_create_new_user.return_value.status_code = 200
mock_create_new_user.return_value = {
"status": "SUCCESS"
}
data_to_post = {
"name": "Test2",
"email": "new-user2@entity.com",
"entity_id": 1,
"is_active": True,
"product_roles": [
{"product_id": 1, "role_id": 4},
{"product_id": 2, "role_id": 4}
],
}
# When
response = self.client.post('/api/usermgmt/users', data=json.dumps(data_to_post), headers={
"Authorization": "Bearer {}".format(get_jwt(identity=self), "Content-Type: application/json")
})
data = response.data
json_data = json.loads(data)
# Then
self.assertEqual(response.status_code, 200)
self.assertEqual(json_data['status'], "SUCCESS")
在我的实践中,我发现这种方法非常好
class TestCreateUser(TestCase):
def test_works_in_correct_case(self):
# equal to your test_create_user
@patch("usermgmt.app.services.user_service.UserService.create_new_user")
def test_handles_value_error_in_create_new_user(self, mock_create_new_user):
mock_create_new_user.side_effect = ValueError
# Your preparation flow here
response = self.client.post('/api/usermgmt/users') # simplified call here is just an example
self.assertEqual(response.status_code, 400)
# check if response body is what you've expected, etc.