Why is it often said that the test cases need to be made before we start coding?
What are its pros and what the cons if we don't listen to this advice?
Moreover, does that advice refer to black box testing or White Box testing or both?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityWhy is it often said that the test cases need to be made before we start coding?
What are its pros and what the cons if we don't listen to this advice?
Moreover, does that advice refer to black box testing or White Box testing or both?
Writing tests before implementations is one of the core ideas behind Test Driven Development (TDD). The procedure is:
This procedure is the same whether you implement a feature or fix a bug; it is often referred to as "Red-Green-Refactor" (red = test fails, green = test passes). The advantages of this method are numerous:
Now for the downsides:
Why is it often said that the test cases need to be made before we start coding?
It's a basic principle of test-driven development, but not a general "best practice". Not everyone wants or needs to do TDD, even though its proponents often claim that everyone would benefit from it.
What are its pros and what the cons if we don't listen to this advice?
The advantage is that it forces you to think about your code from a perspective of testing before you write it, which has two main advantages: you're less likely to forget about special cases and boundary conditions, and your code has to be testable and thus modular. Another important benefit: at any time you know that your code is working as you thought it should.
The disadvantage is that it may encourage thinking about low-level details and overlook important design issues (but the same can happen if you just start coding). Also, the most easily testable design is not necessarily the best (but probably much better than one grown out of ad hoc coding).
Of course there's the question whether TDD saves more time than writing all those unit tests costs. Its proponents certainly claim so.
Moreover, does that advice refer to black box testing or White Box testing or both?
Usually, TDD deals exclusively with unit tests, which are white box tests. Integration tests have to be done additionally.
When I am writing test first, it isn't much a matter of verifying correct functioning of my code, not really a matter of black box or white box or whatever related to quality assurance, and here is why...
Over the years I practiced and polished a skill to convince management that I need good testers 1, 2, 3 and this suffices to ensure that my code will work as intended without me having to think much about it.
Main reason why I prefer test first is it helps me understand how to design my code so that it's convenient to use. I am not good at imagining in my head how my module will be used by other modules so I can do it overly complicated and that will bite me later and possibly even require redesign.
This is a problem no matter whether I am coding a module for myself or for someone else, it is equally painful using inconvenient interface myself as well as having to explain its usage to other programmers.
"Test-first" approach saves me from that pain; it doesn't require imagination to immediately see how my module will be used if designed this or that way and makes it easy to choose the more convenient one.
When coding module for someone else, it also saves me from the need to painfully explain how to use it.
Look, code in
this test
shows how you should use that module and if you run it, you'll also see what results you should expect. Using any other way than shown inthis test
would be a mistake. Now go away and let me do something interesting.
It is said in the context of practising TDD/BDD, which are design methodologies, not test methodologies.
With TDD/BDD, the idea is to drive out the API usage before writing the API, evolving it with tests.
Seen in this light, the point is to write a test for a non-existing API call or a test for a non-existing API behaviour and then implementing it.
As for blackbox/whitebox testing - again, this isn't about testing - it is about design.
Why is it often said that the test cases need to be made before we start coding?
There are a couple reasons why this is said.
What are its pros and what the cons if we don't listen to this advice?
Pros
Cons
Moreover, does that advice refer to black box testing or White Box testing or both?
It depends. Most unit tests tend to be more blackbox in that they will examine input and output of the method or function. However, there are times when mocks or stubs are needed making it more of a whitebox test as it now requires information about the implementation. It may be a moot point for the purposes of TDD because the developer is typically the one writing the tests.