DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
self.assertAlmostEqual(area_of_circle(1),
pi)
self.assertAlmostEqual(area_of_circle(0),
0)
self.assertAlmostEqual(area_of_circle(3.5),
pi * 3.5**2)

You can go to the directory where these two scripts
reside and enter python -m unittest
test_areacircle.py to run the test. If you don’t want to
type all that, you can add the following to the bottom of
the test_areacircle.py script to allow the unittest
module to be launched when you run the test script:


if __name__ == '__main__':
unittest.main()

All this does is check to see if the script is being run
directly (because the main special case is an
attribute for all Python scripts run from the command
line) and call the unittest.main() function. After
executing the function, you should see the following
results:


Click here to view code image


.
--------------------------------------------------
-----------------
Ran 1 test in 0.000s

OK

The dot at the top shows that 1 test ran (even though you
had multiple checks in the same function) to determine
whether the values submitted produced an error. Since
all are valid for the function, the unit test came back
successfully.

Free download pdf