Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Ideal world</strong> answer would say that <em>every line</em> of code you wrote should be unit tested.</p> <p>But let's forget about that for a moment and move back to <strong>real world</strong>. Write tests for code that is important and having <em>another line of defense</em> is worth it. In other words, does it make much sense testing constructor that simply assigns value to one field? Most likely not. Is it worth to unit test parser extracting account data from complex XML your client provides? Probably yes.</p> <p>Where does this difference comes from? Two major reasons:</p> <ul> <li>it's less likely that constructor code will suffer from unpredictable changes (vs. <em>much-evolving</em> parser code to meet up with changing requirements/optimizations/refactorings)</li> <li>constructor code is fairly simple and you've wrote such code many times already, tests might not offer you huge advantage in spotting issues; quick glance at such code and you'll be most likely able to tell what's going on (vs. complex XML parser code)</li> </ul> <p>Why making the distinction? Why testing this and not that? Wouldn't it be easier to simply test everything (as <em>ideal world</em> answer would suggest)?</p> <p>No. Because of <strong>time and money</strong> constraints. Writing code takes <em>both</em>. And there's only certain amount of money somebody is willing to pay for your product just as there's only certain amount of time he's going for wait for it to be delivered. Some tests are simply not worth it (again, constructor code example). Remember that unit tests are not immune to <a href="http://en.wikipedia.org/wiki/Diminishing_returns" rel="nofollow noreferrer">diminishing returns</a> (having 80% code base covered with tests might take extra 20% development time and later save 20% time spent on debugging/maintenance, while going for another 10% might be twice as time consuming yet yield much lesser gains).</p> <p>Again, you probably want to ask <em>"Where's the line?"</em> When do you decide <em>"Ok, unit tests for this piece of code are not really needed"</em>? Unfortunately, this kind of judgement comes with experience. Write code, read code, see what others (possibly more experienced developers) do and learn.</p> <p>If I were to give couple of generic advises (<em>what to unit test</em>), those would be:</p> <ul> <li>start with business/domain logic code</li> <li>make sure to test all kind of converters/parsers/calculators (those are fairly easy to test, tend to change often [either due to changing requirements or refactorings] and by their nature are error prone)</li> <li>avoid testing simple one-liner methods, unless that one line is crucial in some way</li> <li>write tests for bugs that you discover (and keep them!)</li> <li>don't follow magic fairy-tales of <em>"good code must have 99.99% test coverage"</em> blindly</li> <li>reading <a href="https://softwareengineering.stackexchange.com/questions/159964/how-big-does-my-project-need-to-be-for-me-to-unit-test-it">questions</a> <a href="https://softwareengineering.stackexchange.com/questions/158052/are-unit-tests-really-that-useful">on topic</a> at <a href="http://programmers.stackexchange.com">programmers.stackexchange.com</a> can often give you different perspective to approach problems</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload