????4. ??Mock????????exception
def is_error(self):
try:
os.mkdir("11")
return False
except Exception as e:
return True
class TestProducer(unittest.TestCase):
@mock.patch('os.mkdir')
def test_exception(self?? mock_mkdir):
mock_mkdir.side_effect = Exception
self.assertEqual(self.calculator.is_error()?? True)
????5. Mock????????????????????
@mock.patch.object(Calculator?? 'add')
@mock.patch('test_unit.multiple')
def test_both(self?? mock_multiple?? mock_add):
mock_add.return_value = 1
mock_multiple.return_value = 2
self.assertEqual(self.calculator.add(8?? 14)?? 1)
self.assertEqual(multiple(8?? 14)?? 2)

????Coverage
??????????coverage???????????????????????????????html???????棬????????????????????????.coverage??????????combine???н???????????????????檔
?????????????????ο???http://nedbatchelder.com/code/coverage/cmd.html
?????????????????????????ο???http://nedbatchelder.com/code/coverage/config.html
??????????????????????????????Щ?е??????????磺

 

[report]
exclude_lines =
# ???????м??????“# pragma: no cover”????л?????
pragma: no cover
# ?????main????
if __name__ == .__main__.:

????Nose
????Nose????????е??????????????????У?????????coverage???????????????????????
????Nose?????????????????????????“test”????“Test”??????????????裬????????????з??????“test”????“Test”?????????????
????????Nose????????????setup??teardown????軔??????__init__.py????м??ɡ?
????Nose???????У???????nosetest?????????????в??????????????????????????Щ???в????ο???http://nose.readthedocs.org/en/latest/usage.html
????Nose???????????"--cover"????????coverage?????????????????а?????coverage???????????????????????nose-cov???????“--cov-config”???????????????????????ο???https://pypi.python.org/pypi/nose-cov
????????????????????????????????Щ???????test????????????鷳?????д??????????????Щ??????????

 

import os
import subprocess
######################################################################
# ??????????????????????
cover_list = [
'src/sample/analyzer/unpacker/src/emulator.py'??
'src/sample/analyzer/unpacker/src/emulator_manager.py'??
'src/sample/analyzer/unpacker/src/unpacker_analyzer.py'??
'src/sample/analyzer/bitvalue/src/confparser.py'??
'src/sample/analyzer/bitvalue/src/trunk.py'??
]
# ???????????????????????????????????????test??????????????????
ut_list = [
'src/sample/analyzer/unpacker/ut'??
'src/sample/analyzer/bitvalue/ut/ut_main.py'
]
######################################################################
PRODUCTION_HOME = os.environ.get("PRODUCTION_HOME"?? "../..")
def get_command():
command = [
'nosetests'??
'--with-cov'??
'--cover-erase'??
'--cov-report'?? 'html'??
'--cov-config'?? 'cover.config'??
]
for cover in cover_list:
command.append('--cov')
command.append(os.path.join(PRODUCTION_HOME?? cover))
for ut in ut_list:
command.append(os.path.join(PRODUCTION_HOME?? ut))
return command
if __name__ == '__main__':
command = get_command()
print command
os.chdir(PRODUCTION_HOME)
proc = subprocess.Popen(command?? shell=False?? stdout=subprocess.PIPE?? stderr=subprocess.PIPE)
output?? error = proc.communicate()
return_code = proc.poll()
print output
print error
print return_code