0

I am building a rest api and I was making the routes for the api and stumbled upon a problem. I am using flask restplus to build the api and marshmallow to validate the json sent by the client.

My design: I use a decorator that is used to wrap every api route. this decorator validates the json sent by the client against a marshmallow schema and if the json validates, then the decorator lets the api route run. Otherwise, if the json invalidates when it's checked against the schema, it returns the errors that it got when it invalidated the json back to the client without running the route.

I really like this design as it significantly reduces code repetition and it can automatically validate and invalidate the data posted by the client without me doing pretty much the same thing in each api route -- checking the json sent by the client and then running the route.

My only issue is that I have no clue how to unit test this. I have written tests for the specific marshmallow json schemas to check if they raise the correct validation errors when invalid data is passed to them. However, now I need to test the api routes to check if they return the validation errors raised by the schemas. This seems like a lot of repetition of unit tests because I'm checking for the same errors when testing the schemas and again when I'm testing the api routes/decorator.

Therefore, do you guys have any recommendation of how I should unit test this. Should I test the api routes specifically, the decorator separately, and the schemas separately? Or should I test just the api routes to check they return the correct errors that the schemas raise?

Thanks in advance.

Yang K
  • 407
  • 4
  • 13

1 Answers1

0

This could be a matter of opinion.

I don't think it is useful to test all schemas, unless they include some custom code like custom fields, validators, etc. There is no need to test marshmallow itself, as it is already extensively covered by its own tests. So testing a marshmallow schema would only serve the purpose of checking you entered the correct field names and validators. That would be quite verbose for low benefits, IMHO.

I would test the decorator: create a dummy test route, decorate it, check the correct error is returned.

Deciding whether it is worth testing all routes for all kinds of errors is up to you. As long as your decorator is tested, testing the routes just checks that the decorator is applied to the route with the correct schema.

Some would argue that, on the contrary, they only care about the API itself and they only need to test the routes. But this means testing all cases on all routes, so that's a lot of duplication. And I don't think this qualify as unit testing, rather integration testing.

Note you could try to use webargs (maintained by marshmallow team) to replace your decorator, but I don't know how easy it is to integrate to flask-restplus.

Jérôme
  • 13,328
  • 7
  • 56
  • 106
  • What if you have custom validators for the marshmallow schemas. Is it still not worth unit testing each schema or at least each validator? – Yang K Feb 15 '19 at 19:33
  • Yes, of course. Test code that is yours and assume the library is covered by its own tests. (Marshmallow has extensive tests.) – Jérôme Feb 15 '19 at 19:34
  • How do i make sure that the custom validators that i created for the marshmallow schemas actually work without creating unit tests for them? – Yang K Feb 15 '19 at 19:35
  • So what you're saying is that I should test the custom validators that I created for marshmallow because its my code but not to worry about testing built in validators? Additionally, I should test the decorator to make sure it works properly and whether I test each api route is purely opinion based? That's what you're saying correct? @Jérôme – Yang K Feb 15 '19 at 19:39
  • Yes. Exactly. I edited my answer to hopefully clarify that. – Jérôme Feb 15 '19 at 20:04