I want to know the best/different ways to test a REST API which uses a database backend. I've developed my API with Flask in Python and want to use unittest or nose.
But my problem, is that some resources require another resource to create them in the first place. Is there a way to say that to test the creation of a blog post requires that another test involving the creation of the author was successful?
In e2e tests or acceptance tests you usually only check the observable behavior, so the behavior the user would be able to see, in your case the user of the API, so in theory you should not need to check the database.
REST means representational state transfer, and it's an architecture used to design client-server applications. With a Rest API, you're getting a representation of the requested data stored in a database. A REST API is also stateless, which means that the server doesn't store any data between requests from clients.
Backend Testing is a testing method that checks the database or server-side of the web application. The main purpose of backend testing is to check the application layer and the database layer. It will find an error or bug in the database or server-side.
There are 2 standard ways of approaching a test that depends on something else (object, function call, etc).
Some people like "classical" unit tests where only the "unit" of code is tested. In these cases you typically use mocks and stubs to replace the dependencies.
Other like more integrative tests where most or all of the call stack is tested. In these cases you use a fixture, or possibly even do calls/creations in a setup function.
Generally you would not make one test depend on another. All tests should:
If you make one test dependent on another they cannot be run in isolation and you are also forcing an order to the tests run. Enforcing order in tests isn't good, in fact many people feel you should randomize the order in which your tests are run.
The unit test should work in isolated mode so you have to isolate your dependent resources and this done using isolation an framework (mocking framework). Common frameworks for legacy, Windows systems are DevMagicFake, MOQ, Rhino Mocks, TypeMock.
DevMagicFake will make you able to fake the DB so you will not need to create DB or even any code to save your data because it save your data in memory and you can retrieve it anytime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With