Search code examples
djangoapiresttddtastypie

Test Driven Development for REST API


I am writing a backend API in Django and using Tastypie, I am a beginner and want to clarify a basic question. I want to use Test Driven Development approach. What I know of it is, that test cases are supposed to be written before production code.

How am I supposed to write a test case that would not have production code? Like am I just supposed to make a mock of every step?

Does this approach mean that I first write almost similar code (logic) for test cases and then implement the same for production?

For example, I have an Object book, I want to write an API that can put,get,post or delete. How do I write test case for this without writing any of the production code?

Any help would be great! Thanks in advance.. :-)


Solution

  • The idea behind test driven development is that you write the tests first, but that they fail initially. Then, as you actually write correct implementations, the tests start succeeding.

    In this case, you'd write tests that would call put, get, post, and delete. Obviously none of them would work at first. Then, once you implemented, say, put correctly, that test would start succeeding, etc.

    Now, depending on what level you are writing the tests at, you may need to write enough of your API to get the tests to compile. However, it's okay if the API has no implementation and throws errors for everything at first.