Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing REST API with database backend

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?

like image 474
Jarus Avatar asked Sep 07 '11 15:09

Jarus


People also ask

Should API tests check database?

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.

Does REST API use 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.

What is backend database testing?

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.


2 Answers

There are 2 standard ways of approaching a test that depends on something else (object, function call, etc).

  • You can use mocks in place of the objects the code you are testing depends on.
  • You can load a fixture or do the creation/call in the test setup.

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:

  • clean up after themselves
  • be runnable in isolation
  • be runnable as part of a suite
  • be consistent and repeatable

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.

like image 190
dietbuddha Avatar answered Oct 11 '22 06:10

dietbuddha


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.

like image 37
Mohamed.Radwan -MVP Avatar answered Oct 11 '22 08:10

Mohamed.Radwan -MVP