I am a php programmer converting to python. Please explain the importance (or non importance) of unit testing. I never unit tested with php! [closed]

StackOverflow https://stackoverflow.com/questions/5864019

Question

I am at a point in my learning about solid software development where I realize that the "pedal-to-the-medal code at light speed and pray for good results with no real plan" approach does not work.

I am seeking to replace my old (and admittedly not exactly great) ways with solid practices. I have never used unit testing before, and now on top of trying to learn pyramid (which is going well thanks to the thorough documentation) I am also trying to see how unit testing in general can benefit me (also in the future add integration testing, regression testing, etc.). I read this intro to nosetest http://ivory.idyll.org/articles/nose-intro.html, and (not to be flippant) I see that you can use unit tests to test regular expressions, and see if a=1; b=1; now does a == b. This is all I can see at this point. I do not need a framework for this. I can use a regular expression tester, and common sense to 'test'. If I'm going to use unit testing to see if two variables are equal, then wouldn't i have to create a test every time I create a variable and set it equal to something? The examples from the article just seem kind of arbitrary.

My mind is way open to learning the benefits of unit testing, but I want to know What are some practical applications of unit testing.

Bottom line: I have never used unit testing before, so please tell me why I am wrong, and should start using testing.

Cheers!!

Was it helpful?

Solution

Unit testing gives you some certainty that you code meets your expectations. Unit testing is not a holy grail, it is simply one of the tools you can use to attain a good level of accuracy and modularity with your code.

The unit test is your expectation. In other words, your test will call or invoke the piece of code that is under test, and then measure the results (return values, etc) against what you expected to happen (or be returned). If the results don't match what you expected, then you have either got a faulty test (your expectation was wrong), or the code under test is not operating as you thought.

Unit testing is especially useful when it comes time for refactoring or maintaining your code. You can change code, run the unit tests, and check to see if your changes have broken the tests.

To keep it brief, unit tests do have other benefits as well. Normally you should be able to run them in an automated way, so you can have them run if you do a nightly build, then you can check the results in the morning. This is especially useful in larger teams when multiple people can be changing or adding code. Constructing your code in a way that is suitable for unit testing also leads to more of a decoupled approach when building your app, this reduces code smell and helps with the future maintainability and extensibility of it.

Edit:

With testing did_earn, do I hard code in test cases. For example, would I manually pass user5 as a param (user5 has logged in 400 times) and make sure that they do not get the badge, but user6 (500 logins) does get the badge?

Yes, that is exactly what you will do. This means that if you change the internal implementation of did_earn then your tests can tell you immediately whether it still meets your expectation (that it only returns true from 500 logins onwards). If you are testing code that is not strongly typed (like a scripting language) then you should also consider testing for what happens if you pass a string to the function instead of an integer, etc.

While it may not apply to you at the moment, you should also check out the concept of mocking, IoC and Dependency Injection, which are all techniques used heavily in unit testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top