Reputation: 2133
When doing TDD on a Django app API, I write tests demonstrating that when proper constraints are provided, expected results are achieved. How do I write tests for the infinite permutations of cases where inputs are 'wrong'.
Two things come to mind:
1) I shouldn't break the dynamic aspect of python by type-checking every input, and
2) I should be testing "interesting" functions or methods (not getters and setters), though I'm not sure how this applies to an API
Upvotes: 0
Views: 286
Reputation: 12195
With TDD, a good way to stay sane is to write tests that cover the required number of valid inputs, have the tests fail because there's no code yet, write as little code as possible to make them pass, then refactor that code into something production-ready, while ensuring your tests still pass. Sounds like you've done that bit.
Then, in terms in invalid input-testing, I doubt that the choices really need to be infinite, and will fall into several cases that you can test reasonably well with just a few alternative inputs/arguments (eg: for an integer input, test passing a zero, negative integers, masssive/overflowing integers, characters).
Of course, that still takes time to work out and code up (even if you automate it by looping over a lists of variables and call the same test repeatedly with different inputs), and while it's definitely worth doing, if you want more, you can always look at something like fuzz testing for extra piece of mind.
PS. In terms of testing the API, I would focus on creating test queries against the API's exposed collections/resources (easy to do as unit tests with the Django test Client), and also directly unit test any non-trivial methods that the API uses under the covers. Getters and setters, etc, should then be implicitly tested by higher-level functions. In theory :o)
Upvotes: 3