Posted by Derek@TheDailyLinux »
Add Comment »
There was a major hardware failure at the hosting facilities over Thanksgiving weekend which took down (and wiped clean) everything on this site. Luckily, the staff were able to salvage the hard drive from the old server and transfer everything to the new hardware. This does take some time to transfer. We’re hoping that by tomorrow, things will be fully transferred and back to normal. In the meantime, please forgive any oddities or speed issues as you browse. They should be cleared up soon. If you do find something that you need immediately, please feel free to use the comment form below.
Best Regards,
Derek
Posted by Derek@TheDailyLinux »
Add Comment »
Posted by Derek@TheDailyLinux »
Add Comment »
The following sections give quick, heavily commented, and ready-to-run examples that will give you a head start in creating a unit test framework in Python (pyUnit). First, we’ll take a look at a standalone module with several simple examples of unit tests. Then, we’ll take a look at a more complicated version that uses modules and test suites.
Here’s the first example. As long as you are running Python version 2.1 or greater, you can copy/paste this code, save it to unittest.py, and run it using python unittest.py.
'''This is a simple program to demonstrate how to create a unittest in
Python. For more information and documentation, please see the official
documentation page here: http://docs.python.org/library/unittest.html'''
import unittest #Include the pyUnit unittest framework
def add(a,b,c=0):
'''A simple adding function to demo unittest'''
return a+b+c
# The following is the class in which all functions will be ran by unittest
class AddTest(unittest.TestCase):
''' Main class for add testing; Can be added to a suite'''
# The function "setUp" will always be ran in order to setup the
# test environment before all the tests have run.
def setUp(self):
'''Verify environment is setup properly''' # Printed if test fails
pass
# The function "tearDown" will always be ran in order to cleanup the
# test environment after all the tests have run.
def tearDown(self):
'''Verify environment is tore down properly''' # Printed if test fails
pass
# Functions beginning with "test" will be ran as a unit test.
def test_positive_add(self):
'''Verify that adding positive numbers works''' # Printed if test fails
self.assertEqual(add(10,23,22), 55) # Test will fail if "false" (boolean)
self.assertEqual(add(11,23), 34)
self.assertEqual(add(1,1,17), 19)
# Functions beginning with "test" will be ran as a unit test.
# @unittest.skip("demonstrating skipping") # Skip this test (Python >= 2.7)
def test_negative_add(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
# Functions beginning with "test" will be ran as a unit test.
# In this case, this test will be skipped.
#@unittest.skip("demonstrating skipping")
def test_negative_add_skip(self):
'''Verify that adding negative numbers works''' # Printed if test fails
self.assertEqual(add(-12,23), 11)
if __name__=='__main__':
unittest.main()
For more complicated test suites and modularity, please take a look at the git repository I’ve setup on bitbucket.org. Download the source, read the README file, and you’ll be able to have a working example of a testsuite (run with python pyunit_intro/suite/test/unittest_run_all.py).
https://bitbucket.org/dhildreth/pyunit_intro
Source: Much of the code from above was done first in this great introductory presentation given by Chander K. Ganesan at PyCon 2010.
Posted by Derek@TheDailyLinux »
Add Comment »
Here’s a quickie that will help you find lines longer than 80 characters (or any number of characters) within VIM. Open vim, and copy/paste the following:
:/.{80,}
You can use the regex anywhere, actually, but this is a good one to store in the useful command list. Or, create an alias for it in your .vimrc file for quick access.