[Tutor] unittests

Sydney Shall s.shall at virginmedia.com
Tue Apr 1 13:30:46 CEST 2014


Another debutant!
I am having trouble learning to use unittests.
My question is;
     In the example below, did you write the class 
"SquareRootTests(unittest.TestCase):" ?
    Or do I find a set of them in the library?
    And what is the significance of the name chosen 
"self.assertAlmostEqual(square_root(3), 2.0) "?
   I take it the first argument is calling my function with its argument 
and the second argument is the correct answer?

I am quite unclear how one proceeds to set up unittests.

With many thanks in advance,

Sydney


On 01/04/2014 03:10, Danny Yoo wrote:
>> I tweaked it to what I thought was correct but when I test it I get nothing back.
>>
>> def square_root(a, eps=1e-6):
>>     x = a/2.0
>>     while True:
>>         y = (x + a/x)/2.0
>>         if abs(x - y) < eps:
>>             return y
>>         x = y
>>
>> round(square_root(9))
>>
>> The way I tweaked it seems to work, I’m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python.
>
> I didn't want to keep you waiting, so I'll cut to the chase.  This
> line here in your program:
>
>      round(square_root(9))
>
> computes a value... But it doesn't do anything with that value.
>
> Try printing the value.
>
>
> You may also try to see that your program is doing something effective
> by "unit testing" it.  This is often a lot better than just printing
> values and looking at them, because the test case will say what the
> _expected_ value is, so it's more informative.
>
>
>
> For this example, the following is a start at unit testing the above
> function.  Add the following to the bottom of your program's source.
>
>
> ###############################################
> ## See:  http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html
> import unittest
> class SquareRootTests(unittest.TestCase):
>      def testSimpleCases(self):
>          self.assertAlmostEqual(square_root(1), 1.0)
>          self.assertAlmostEqual(square_root(4), 2.0)
>
> if __name__ == '__main__':
>      unittest.main()
> ###############################################
>
>
> Here's what it looks like when I run this:
>
> ##############################################
> $ python sqrt.py
> 4.472135955
> .
> ----------------------------------------------------------------------
> Ran 1 test in 0.000s
>
> OK
> ##############################################
>
>
> You can then start adding more and more to tests to gain confidence
> that the code is doing something reasonable.
>
>
>
> If we try to put in an intentionally broken test, like:
>
>          self.assertAlmostEqual(square_root(3), 2.0)
>
> in the body of testSimpleCases(), then we'll see the following error
> when running the program:
>
>
> ##############################################
> $ python sqrt.py
> 4.472135955
> F
> ======================================================================
> FAIL: testSimpleCases (__main__.SquareRootTests)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>    File "sq.py", line 20, in testSimpleCases
>      self.assertAlmostEqual(square_root(3), 2.0)
> AssertionError: 1.7320508075688772 != 2.0 within 7 places
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.000s
>
> FAILED (failures=1)
> ##############################################
>
>
> And that's what you want to see.  If either the test or the code is
> bad, it'll say something about it.
>
>
> One other thing: you will want to check a particularly insidious case
> that will cause the program here to behave badly.  Consider the zero
> case: square_root(0).  Write the test case.  Run it.  You'll see
> something interesting.
>
>
>
> Good luck!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


-- 
Sydney Shall


More information about the Tutor mailing list