Sunday, May 5, 2013

[ Level 2 ] Tips of unittest for Python.

There are tips for Python:
1. Use mock for replace method.
ex.
myMock=mox.Mox()
myMock.StubOutWithMock(myModule, 'replaceMethod')
myModule.replaceMethod(input).AndReturn('Hello World!')
myMock.ReplayAll()
expected = 'Hello World!'
self.assertEqual(module.replaceMethod(input), expected)
myMock.VerifyAll()  #verify if all mocks be executed.
myMock.UnsetStubs() #release all mocks

2. Use -m to test one method only.
ex.
$ python -m unittest myApp.TestClass.testMethod

Wish this helps.

regards,
Stanley Huang