I'm writing a CRUD app in Python that exposes web API. At first I wrote functions for communicating with DB and wrote tests for these functions.
def crud(): # do something with db def test_crud(): crud() # check crud tables now have all required rows
Then I wrote functions for API routes, which do some input validation and then just call corresponding crud function. All API functions do is basically call
crud functions inside them and return result. Tests for them are like:
def api(): # do some api stuff return crud() def test_api(): resp = api() # check response body has what we expect # check crud tables now have all required rows (1)
(1) it was already ringing a bell to me, but whatever.
Then I had to add a new
module that does something with DB, calls some
crud function and has to be used in
api functions instead of
def module(): # do some stuff crud() # do some stuff def api(): # do some api stuff return module()
So, now tests look like this:
def test_crud(): crud() # check crud tables now have all required rows def test_module(): module() # check module tables now have all required rows # check crud tables now have all required rows def test_api(): resp = api() # check response body has what we expect # check module tables now have all required rows # check crud tables now have all required rows (1)
What's bothering me is that I check the same thing in three places and in case of some changes I'll have to edit basically one test three times. I don't think that I should remove, for example, checks from
test_api() that are related to side effects of internally called functions, because I care both about API response and these effects.
I was reading similar questions like "should I test only public methods or private too?" where, in general, answer was "test public methods, but not private ones". Does it apply to my situation, do I need to remove tests for
module and leave only
test_api which tests all the things that the first two do?
moduleare test methods themselves, so why not directly implement them in the
moduleare separate functions (actually, separate sets of functions) used in application, and
test_xxxare test methods for these functions. I've added more pseudoexamples to make it clearer