1

I have a unittest that test that a custom exception is raised properly. But I got get a AssertionError: InvalidLength not raised

Below is my 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 to popular"
    mock_jsonify.return_value = {
        "success": False,
        "results": {
            "message": "Invalid value for traditional_desc"
        }
    }

    with self.assertRaises(InvalidLength) as cm:
        BenefitTemplateService.create(data)

And this is the 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 for traditional_desc")

            # Call create here
            response = BenefitTemplateEntityManager.create_template(params)
            return response

        except InvalidLength as e:
            response = {
                "success": False,
                "results": {
                    "message": e.message
                }
            }

            return jsonify(response), e.code

The except InvalidLength is working properly because if I try to do a print it execute that line of code. So I assumed that InvalidLength Exception is being called but I'm not sure on the result of my unittest it fails. Can you help please

MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76

2 Answers2

1

create raises InvalidLength exception but then catches it and handles it silently, where your test expects it to actually raise it.

Use a different assert than assertRaises. The except block returns a json, so your test can check the json's content.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • How do I go around this? I need my test to tell me that an exception really was raised. I can fix this if I remove the except code but I will loose the flexibility of my response – MadzQuestioning Aug 15 '17 at 10:54
  • @MadzmarUllang Then use a different assert than `assertRaises`. You return a json, so your test can check the json's content. – DeepSpace Aug 15 '17 at 10:56
0

you are raising the exception properly

if not validate_string(params['traditional_desc'], 0, 1000, characters_supported="ascii"):
    raise InvalidLength(400, "Invalid value for traditional_desc")

Then you catch it and return a json

except InvalidLength as e:
    response = {
        "success": False,
        "results": {
            "message": e.message
        }
    }

    return jsonify(response), e.code

hence exception does not propagate to the test.

2 ways to solve this:

  • In your test, check if the json response was correct. "Invalid value for traditional_desc"
  • OR Don't catch InvalidLength exception in your code.

I think considering your use case, you should update your test to check if the response message is correct or not.

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70