Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing best practice: For CRUD api

Similar questions have been answered before, but they didn't solve my problem.

I am testing an api with create, read, update and delete methods. It is considered best to test each function separately. But,

To test create, I need to read. To test read, I need to create. To test update, I need to create and read. To test delete. I need to create!

I have no other (for example, lower layer api) mechanism of verifying other than this api itself.

In this case, should I write one long test with everything? Or 4 different tests; each with special setup and teardown logic.

Thanks

like image 496
Asad Iqbal Avatar asked Jul 30 '26 14:07

Asad Iqbal


1 Answers

This is very subjective but I would write four different tests as it would

  • be easier to maintain
  • be easier to locate a possible error
  • improve readability

Readability

What would you name your testmethod if it tests everything? I find it easier to read testmethods in a form of CreateShouldCreateARecord instead of TestCRUD

Locate an error

With readability improved, it is easier to know what went wrong. Again, for one monolithic method, all you get in a report is that the TestCRUD method failed and you will have to drill down to find that it was a wrong implemented read.

like image 133
Lieven Keersmaekers Avatar answered Aug 02 '26 08:08

Lieven Keersmaekers