Posts

Showing posts with the label Unittest

[ Level 3 ] Test your Python code.

Ref: https://python-guide.readthedocs.org/en/latest/writing/tests.html Mock: http://www.voidspace.org.uk/python/mock/getting-started.html# Wish this helps. regards, Stanley Huang

[ 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

[ Level 2 ] Coverage in Python.

$ sudo pip install coverage $ coverage run myApp.py arg1 arg2... $ coverage report -m $ coverage html $ coverage help (run) ## or coverage run --help Commands of coverage: run – Run a Python program and collect execution data. report – Report coverage results. html – Produce annotated HTML listings with coverage results. xml – Produce an XML report with coverage results. annotate – Annotate source files with coverage results. erase – Erase previously collected coverage data. combine – Combine together a number of data files. debug – Get diagnostic information. Ref: http://nedbatchelder.com/code/coverage/ http://nedbatchelder.com/code/coverage/cmd.html#cmd Wish this helps. regards, Stanley Huang