I know that it is recommended to write unit test before coding.

But my question is related to test cases.

When should I write test cases? When I already have written code or before it.

  • In spite of user246's answer, I feel this question is not a good fit for our format. It lacks the specificity required to solve an actual problem. It also lacks evidence of prior effort. – corsiKa May 28 '13 at 15:15
  • So in which SE site I should post this question? – TIKSN May 29 '13 at 14:56
  • This is the right SE for the -topic-. The problem lies in that the problem is overly broad. To put this into context, the question isn't all that different from "My wife says I should go for a walk during my lunch break. Should I do it before I eat or after?" The question in its present form doesn't give much information about context or about what I've done already to try to solve my problem. See what I mean? – corsiKa May 29 '13 at 16:17

Welcome to SQA, TIKSN. Since your question distinguishes between unit tests and test cases, I assume you are asking about test cases written in English (or Armenian, or whatever) rather than automated tests.

You will hear a variety of opinions about this. Some people recommend writing the test cases before writing any code because the test cases help clarify details about the requirements. Other people recommend writing the test cases after the code is finished (or at least functionally complete) because the requirements (and therefore the test cases) are likely to evolve as the code is written.

The form of a written test case can vary from one person to the next, too. Some people prefer test cases with detailed, step-by-step instructions. Others prefer higher-level test cases that get the point across but stay out of the details. I prefer to write higher-level test cases before writing code. However, I also expect to revise my test cases as the code progresses.

Finally, it is interesting that you asked whether you should write the test cases. If you work with a tester, they may prefer to write the test cases for you, or at least collaborate with you on the test cases. There are several reasons why a tester might want to be involved; for example, if the tester knows about the test cases before the code is finished, they can plan for how much time and effort they will need to test your code.

| improve this answer | |
  • Dear @user246 thank you for your sophisticated answer. I think I will write high level test cases before coding and will review after it. – TIKSN May 28 '13 at 1:32

Typically, you can do testing at any point in the development cycle; therefore you can write test cases at any point.

If you have requirements to build from, you have the building blocks for test cases in the future. If you have code, you can write test cases while the code is being built. If you already have the code, you can write tests too. Do you have data? You can write test cases. Do you have configurations? You can write test cases.

As per your request, "when should I write test cases?". It depends. It depends on:

  • your project?
  • what your role in the project is?
  • who's testing?
  • when does it need to be tested by?
  • regulated environment?
  • target installation environment?
  • etc, etc.

At the end of the day, you need to take a look at your own risks and apply your own heuristics as to when your write test cases.

| improve this answer | |
  • Your answer is too generic. I know that you create test cases at any point. My question was which is the right way to do based on your experience. – TIKSN May 28 '13 at 1:26
  • In my experience, it depends a lot on the nature of the project. Some projects I was writing test cases at a high level before coding started. Others coding was finished before I could start writing test cases. – Kate Paulk May 28 '13 at 11:43
  • Ultimately, you need to take a look at the risks of your project (is it new? is there things changing? are there upstream/downstream dependencies?, etc) and see where you problems might arise. From that, you can start to determine when/where you can write your test cases. – Ken De Souza May 28 '13 at 11:46

According to your question, you can write test cases after the partial completion of code because once the code is ready then you will be able to decide the expected result.

Once the expected result is known then it will help you a lot in writing test cases and to decide the Pass/Fail criteria. If more than 60 % code is completed then it will help you in identifying :

  • What to verify
  • Various combinations to be covered
  • Exact features to be tested
  • Functions/areas where there is possibility of bugs
| improve this answer | |

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