Page 424 -
P. 424

leftovers


          #4: Automated testing techniques


          And, no, before you ask, this has nothing to do with program-testing
           robots automatically testing your code for you (which, we agree,
           would be nice).
          Automated testing does have everything to do with trying to make
           sure, as far as possible, that your code is working properly.       Writing code to test
                                                                               code?!? Isn‛t that one
           But, how is this possible?                                          of those chicken and egg
           The truth is, it’s very hard to know for sure that your code is working   things?
           perfectly 100% of the time. Programs and software systems are
           complex beasts. Over time, they grow, and it gets increasingly hard
           to know for sure if they are working quite the way you want them to.
           To help, programmers test their code. When they are done, they
           test, test, then test some more. And just to be sure, they test again. In
           fact, programmers often test to destruction, which refers to attempting
           everything they know to try and break the program and make it
           misbehave. If testing finds a problem, the code is then fixed so that
           whatever it was isn’t a problem anymore.
           To help with the grunt work that testing sometimes is, Python comes
           with a handy built-in module called unittest. This module’s sole
           purpose in life is to allow you to write code that tests your code. Trust us,
           this isn’t as strange as it sounds. The unittest module provides
           a framework within which you can exercise your code to ensure it’s
           working the way you want it to.

           The idea is simple enough: as you write your code, you write a
           little extra bit of  code based on unittest to check that your new
           code is working properly. If the test works (that is, it successfully
           demonstrates that your code is OK), you are then in a position to
           automate the testing of your code by reusing your unittest code.
           For more details, check out the description of unittest in your
           favorite Python book or online in the official Python documentation.

          And now that you know about unittest, you have no excuse not
           to use it to test your code.














                                                                                      you are here 4    389
   419   420   421   422   423   424   425   426   427   428   429