Sachi
Sachi

Reputation: 163

Need to write unit test for compound functions and DB functions?

I need to clear my thoughts about unit testing.

Q1.) Ex: Function A ; Function B { do some other stuff; Function A;}

As per definition unit test should write for smallest (independent unit) of the application. In this case should i write unit test for Only for A , or need to write for both A and B ?

Q2.) 2/3 part of the functions in my application deal (get/update/insert) with local database (sqlite)

(When is a Test not a Unit-test?) is mentioned that unit test should not touch database. So how do i make sure the my database queries work well or not?

Upvotes: 0

Views: 51

Answers (1)

ironcev
ironcev

Reputation: 394

It would be helpful if you provide some more concrete examples. Then the answers could be more concrete. Out of this what you have mentioned, here is the general approach:

Ad 1) You should test both the Function A and the Function B (assuming that they are both public functions). Why? Firstly, as you wrote, Function B is doing "some other stuff" as well. So, there is surely something additional to be tested there. Secondly, the fact that Function B calls Function A to implement its semantics is an implementation detail that could change over time, although the semantics of the Function B will stay the same. The point is, you have to tests what Function B should be doing, regardless if it calls A or not.

Ad 2) Unit tests are not the only kind of automated tests. Yes, unit test shouldn't touch database, but there are automated tests that touch database. Depending on the terminology, they could have different names. Integration tests could be one of them. Such tests can also be run using unit tests runners like e.g. NUnit, but that does not make them unit tests. Using the unit test runner in such cases is just a matter of convenience.

If you provide more concrete examples, we can sketch how the tests (unit and "integration") could look like.

To really "clear your thoughts" and gain a deeper understanding on the topic of automated testing I would highly recommend you excellent book from Roy Osherove The Art of Unit Testing, With Exampels in CSharp, 2nd Edition.

Upvotes: 1

Related Questions