-1

It is quite easy to cover your code by writing tests first using TDD and you know that when to stop once you implemented a feature.

For me it is trickier to decide how many functional tests to write for say a REST service.

Currently I am in a situation where a service returns few fields in JSON and I can create/updade/retrieve/delete an entry. There are cases where errors are thrown if for example for one or more fields wrong value has been assigned. Also there are dependencies and given a value for one field would make another invalid with certain value. So service is doing some validation.

I have few options(maybe you see and suggest more):

  • Write functional tests for each case to verify that system behaves as expected and of course write unit tests together with it. And integration tests if you need it.
  • Write some functional tests to verify that the response format/status code/headers are valid but not cover each case as you can cover these with integration/unit tests say testing at the service boundary together with persistence layer(repository) without controller.So I could loose some confidence in actually guaranteeing that my REST service will marshal results back as expected.

Obviously there are trade offs. In first scenario - higher confidence, more time writing tests, takes longer to build an artifact because I have more tests that run slower. In second scenario - faster build, quite high confidence but not as high as with the scenario 1, less time spent writing functional tests.

What is your experience/view on this and what do you think is best option?

  • 4
    Write enough tests so that you have confidence that the code works. – Robert Harvey Jul 7 '16 at 21:17
  • 1
    And no more. (Subjective, I know. No one said this was easy.) – RubberDuck Jul 7 '16 at 22:02
0

This is too broad of a question to answer specifically. There are a number of textbook approaches to this and no one will be able to tell you exactly how many number of tests to run without analyzing the details of your project. The main thing is that both you and the customer have to be satisfied.

Here is a common strategy that you didn't mention in your post:

  1. Create a list of all the tests you can think of without regard to need, cost or difficulty.
  2. Ask the product owner to rank them in terms of desirability. The customer might think of other tests too.
  3. Rank the tests yourself in terms of which one you think is most important to mitigate risks of defects. For instance to address parts of the project that especially tricky.
  4. Combine your ranking and and the customer's to prioritize all of the tests.
  5. Starting with the highest priority tests, tell the customer which of the tests can be done within your time/cost constraints.
  6. Focus your testing on high priority tests.

As one of the commenters alluded to, figuring all of this out is not easy. Assuming you aren't doing this as a hobby, you will actually have to talk to the customer or product owner to figure out the right number of tests.

  • 1
    you will actually have to talk to the customer or product owner to figure out the right number of tests -- Why? Unless this is part of your requirements-gathering process or you're building a User Acceptance Test Suite, you should be figuring out stuff like this yourself. – Robert Harvey Nov 14 '16 at 22:59

protected by gnat Nov 14 '16 at 23:01

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.