2

I am learning Drupal and come across the testing part. What I don't understand is why we do testing. I have read up on some links like this one What are unit tests and why should I care?. I understand such tests are to ensure that we get what we want to get. My problem is that when we write the code, we already have tested the code to make sure it parse correctly, not producing error messages, and also producing the correct results. We can see the correct results with our eyes, and we can try some inputs to make sure the extreme data are tested. So what is the difference between these (normal) testings and unit tests?


Let me rephrased my question. I know it is important to do unit testing because it makes the program reliable etc.. What I don't understand is that when we do a run a program, we already ensure it produces the result we are expecting. So what improvements does unit test brings in? To test things that are already right?
I know I am missing something. But I can't figure out what I am missing.
Community
  • 1
  • 1
Standstill
  • 399
  • 4
  • 17

4 Answers4

0

"Unit Tests" are tests, the same as normal tests. but they are a subset.

That means Unit Tests test just a small unit, and when a Unit Test fails you know EXACTLY what went wrong.

Quick example: If you use a function to rename uploads and the Database Query Fails, you can trace it back to being a problem with the filename, alas noticing it was the function. If you had unit tests, you would be advised earlier that the output for the renaming function was wrong. That's the idea.

This case was simple, but when you deal with TONS of functions, some function that produces the wrong output can have reflexes after 20 other functions have been successfully executed. And then you'll have to debug every one of those to find what happened. TDD exists to lessen those kinds of risks.

It's more like a concept of "Thorougly testing any and everything".

0

Computer programs are very complex systems. Code is (usually) very context sensitive: so a particular bit of code can behave very differently if it is executed in a different context. In other words, changes over here can have unintended consequences over there.

Unit tests are designed to test a small, logically self-contained chunk of code as thoroughly as possible. There are two distinct advantages to using them:

  • they help you to break assumptions about the context of the code you are writing, which helps you write more robust code. You write unit tests that try running the code with different contexts (environment variables, etc.)
  • they help you figure out later on whether the code still performs as you expect it to, when you have made substantial changes elsewhere.
jmtd
  • 1,223
  • 9
  • 11
  • But what is the difference between the normal test I do and unit tests? For example, when I was taught programming many years back, I was told to try some inputs as testing for unexpected results ie. division by zero in a calculation program. Are those tests not useful anymore? – Standstill Mar 05 '12 at 15:04
0

Perfect repetition and meticulous attention to detail are notoriously bad tasks for a human to perform. You won't perform the same test twice, much less 100 times, so you can never produce the confidence in a stable system than an automated suite can.

And even if that enormous benefit weren't enough, it will also make you a better developer. Testing parts of your software independently of one another will sharpen your skills, and lead you to optimizations and improvements you would very likely have overlooked otherwise.

It's a question you don't see a lot of experienced developers ask, because we're intuitively terrified of the enormous, brittle systems that are the very likely result of untested software.

menacingly
  • 728
  • 4
  • 11
0

When you refactor code, you don't have to test every code path manually. That's a huge time saving.

Also see http://en.wikipedia.org/wiki/Regression_testing.

Wim Leers
  • 1,631
  • 1
  • 11
  • 10