Posts

Showing posts from April, 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

[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): ...