Friday, April 20, 2012

[Level 2] How to fork in Python.

#!/usr/bin/env python
import os
import time

def my_fork():
    child_pid = os.fork()
    if child_pid == 0:
        print "Child Process: PID# %s" % os.getpid()
        time.sleep(10) 
    else:
        print "Parent Process: PID# %s" % os.getpid()
        time.sleep(1) 

if __name__ == "__main__":
    my_fork()
$ python /testFork.py
Parent Process: PID# 2292
Child Process: PID# 2293
$
Wish this helps.

regards,
Stanley Huang

Sunday, April 8, 2012

[Level 2] Python unit test samples.

#!/usr/bin/env python
import os,sys
import unittest

class C(object):
    def __init__(self):
        self.name = 'stanley'
        self.age = 24

class TS(unittest.TestCase):
    ## set up
    def setUp(self):
        self.c = C()
        pass

    ## clean up
    def tearDown(self):
        pass

    def test_getName(self):
        print self.c.name

    def test_getAge(self):
        print self.c.age

    def test_getNameAge(self):
        c = C()
        print c.name
        print c.age 

    def runTest(self):
        print self.test_getName()
        print self.test_getAge()

    #def myTestModule():
    #    tests = ['test_getName', 'test_getAge']
    #    suite = unittest.TestSuite(map(TS, tests))
    #    unittest.TextTestRunner(verbosity=2).run(suite)

class TS1(unittest.TestCase):
    def setUp(self):
        self.c = C()
        pass

    def tearDown(self):
        pass

    def test_name1(self):
        print self.c.name

    def test_age1(self):
        print self.c.age

    def runTest(self):
        self.test_name1()
        self.test_age1()

class TS2(unittest.TestCase):
    def setUp(self):
        self.c = C()
        pass

    def tearDown(self):
        pass

    def test_name2(self):
        print self.c.name

    def test_age2(self):
        print self.c.age

    def runTest(self):
        self.test_name2()
        self.test_age2()

def getSuite1():
    suite = unittest.TestSuite()
    ## use suite.addTest()
    ## 1. only could pass instance
    ## 2. must have method named 'runTest()'
    ## 3. could also pass test method when instanize

    suite.addTest(TS('test_getName'))
    suite.addTest(TS('test_getAge'))
    ts1 = TS1()
    ts2 = TS2('test_age2')
    suite.addTest(ts1)
    suite.addTest(ts2)
    return suite

def getSuite2():
    tests = ['test_getName', 'test_getAge']
    return unittest.TestSuite(map(TS, tests))

def getSuite3():
    return unittest.TestSuite(map(TS, ['test_getName']))

def getSuite4():
    return unittest.TestSuite(map(TS, ['test_getAge']))

def getCompositeSuite34():
    return unittest.TestSuite([getSuite3(), getSuite4()])
    
if __name__ == '__main__':
    ## uncomment when want to test. ##
    ## way 1:
    #unittest.main()
    ## way 2:
    #suite = unittest.TestLoader().loadTestsFromTestCase(TS)
    #unittest.TextTestRunner(verbosity=2).run(suite)
    ## way 3:
    #suite = unittest.TestLoader().loadTestsFromTestCase(TS)
    #suite1 = getSuite1()
    #suite2 = getSuite2()
    #allsuites = unittest.TestSuite((suite1, suite2))
    #unittest.TextTestRunner(verbosity=2).run(suite1)
    #unittest.TextTestRunner(verbosity=2).run(suite2)
    #unittest.TextTestRunner(verbosity=2).run(allsuites)
    ## way 4:
    #suite = getCompositeSuite34()
    #unittest.TextTestRunner(verbosity=2).run(suite)

    ## main ##
    #c = C()
    #print 'hello %s(%s)' % (c.name, c.age)
    pass
Wish this helps.  regards, Stanley Huang