python - AssertionError: Exception not raised -
i have unittest test custom exception raised properly. got assertionerror: invalidlength not raised
below unit test
@patch('services.class_entity.validate') @patch('services.class_entity.jsonify') def test_should_raise_invalid_length_exception(self, mock_jsonify, mock_validate): mock_validate.return_value = true data = self.data data['traditional_desc'] = "contrary popular" mock_jsonify.return_value = { "success": false, "results": { "message": "invalid value traditional_desc" } } self.assertraises(invalidlength) cm: benefittemplateservice.create(data)
and function i'm testing
class benefittemplateservice(object): @staticmethod def create(params): try: required_fields = ['company_id', 'name', 'behavior', 'benefit_type'] valid = is_subset(params, required_fields) if not valid: raise missingparameter if not validate_string(params['traditional_desc'], 0, 1000, characters_supported="ascii"): raise invalidlength(400, "invalid value traditional_desc") # call create here response = benefittemplateentitymanager.create_template(params) return response except invalidlength e: response = { "success": false, "results": { "message": e.message } } return jsonify(response), e.code
the except invalidlength working because if try print execute line of code. assumed invalidlength exception being called i'm not sure on result of unittest fails. can please
create
raises invalidlength
exception catches , handles silently, test expects raise it.
use different assert assertraises
. except
block returns json, test can check json's content.
Comments
Post a Comment