Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
Thread: Unit testing an API
08-27-2013, 02:45 PM #1
Unit testing an API
I've built a crud-based PHP API and I'd like to do some unit testing with it but I'm not sure exactly the best way to implement it.
The API has a few layers:
index.php You POST your API call to this page api index.php sets up the autoloader/error handlers and sends the request here database Class that interacts with a mysql database crud Class that provides a few functions like create, read, update, and delete myclass extends crud Just a sample class
- Test my API by using the api entry point at index.php. This means I would test the entire API by just sending requests to index.php and looking at the response I get back and checking to see if it's what I expect. This means I would never instantiate crud or database or anything on my own.
- Test my API class by class. If I want to test and see how my crud class is working, I would call methods on crud directly.
I can see pros and cons to each method so I'm not sure what's best. I feel like the second option is better because my tests are more isolated and thus a bug in the way I'm returning data from the API won't break a test on a function that fetches something from the database. The only issue I can see is that I will lose stuff like my autoloader so I feel like I'll be rigging my tests to make them work because they are connecting to classes in a way that the API wasn't designed to allow.