???????
?????ο?????????????
????????
????????ж?????д???????????????????python??????????£??????????Android??á?????PyUnit??????????????????????????????????????????????????????
import unittest
class WidgetTestCase(unittest.TestCase):
def setUp(self):
self.widget = Widget("The widget")
def tearDown(self):
self.widget.dispose()
self.widget = None
def testDefaultSize(self):
assert self.widget.size() == (50??50)?? 'incorrect default size'
def testResize(self):
self.widget.resize(100??150)
assert self.widget.size() == (100??150)??
'wrong size after resize'
?????????HTMLTestRunner??飬????????????????????????????棬?????(????????)??

????????????и??????????????????е??????????????????????棬????????????????????(???????????????????????е??????????????)
?????·
????????????????html??????????????????????????????????????????????????????????????????????±???????
????1??????????е??????棬??????????????????????
????2?????????е?????????????????????????????????? + ???? + ????????????????????
????3??????HTMLTestRunner.HTMLTestRunner????run???????
????????
??????????
???????????????????????HTMLTestRunner._generate_report_test()???????????????????????
import CommonLib
print CommonLib.screenshotPath
row = tmpl % dict(
tid = tid??
Class = (n == 0 and 'hiddenRow' or 'none')??
style = n == 2 and 'errorCase' or (n == 1 and 'failCase' or 'none')??
desc = desc??
'''???????'''
screenshot = str(CommonLib.screenshotPath)??
script = script??
status = self.STATUS[n]??
)
rows.append(row)
if not has_output:
return
??????HTMLTestRunner._generate_report_test()????????html???REPORT_TEST_WITH_OUTPUT_TMPL??????????????????????????????????????и?error??????????????????????????????
????<a href="%(screenshot)s">screenshot</a>
????<a href="%(logcat)s">logdetail</a>
????Ч???????????????????????????????????ò???
??????????????????????????
???????????????
????1?????importlib??????????????????????з????????????????????δ???????????????????????pass??
????2????????????????????????????????????? + ???? + ?????????????????????????????????????????????????н??????????????teststep()
???????裺
????1???????????????·???????н??????????????
def get_file_name(path):
'''
@see: ?????????????????μ?.py????????.py?????
@param path: ????????????·??
'''
fileNameList = []
for root?? dirs?? files in os.walk(path):
if files:
for fi in files:
if 'init' in fi or '.pyc' in fi:
continue
if '.py' in fi:
fileNameList.append(fi.split('.')[0])
return fileNameList
????2??????д????????????unitTestDemo.py????????????е?testcase??testsuite??
if __name__ == "__main__":
testsuite = unittest.TestSuite()
path = CommonLib.unittestcasePath
'''???????????????????????:'''
TestDemo = add_testcase_to_suite(path)
testsuite.addTests(TestDemo)
def add_testcase_to_suite(path):
'''
@see: ??????????????
'''
fl = Util.get_file_name(path)
TestDemo = []
for f in fl:
ef = f + '.' + f + '("teststep")'
TestDemo.append(eval(ef))
return TestDemo
????3??????unitTestDemo.py?ж??????????л???????????? (???????????????)
def config_unittest_demo_read(path?? moudleName):
'''
@see: ????????????????鵽??????????????
@param path: ???????????????·??
@param moudleName: ??????????????????
'''
fl = []
fo = open(path??'r')
try:
for f in fo.readlines():
if 'from UnitTestDemo import' in f:
p = 'from.+? '
rc = re.compile(p)
fr = f.replace(rc.findall(f)[0]?? moudleName + ' ')
fl.append(fr)
continue
else:
fl.append(f)
return fl
except IOError??e:
print e
return False
finally:
fo.close()
????4?????HTMLTestRunner.HTMLTestRunner??run??????????е???
'''?????????????'''
file_name = CommonLib.unittestresultPath
fp = file(file_name?? 'wb')
'''??е??????'''
renner = HTMLTestRunner.HTMLTestRunner(
stream=fp??
title='??????'??
description='???????'
)
renner.run(testsuite)
?????????з??
?????????????з?????????????????з??????????????????????????з?????????????bat??????С????????????????е??????????????????????
unitTestDemo.py
configUTDemo.bat
cd E:Python27
python E:PythonProjectconifgUTDemo.py
ping 127.0.0.1 -n 11>nul
startUTTest.bat
cd E:Python27
python E:PythonProjectunitTestDemo.py
ping 127.0.0.1 -n 11>nul
????????б???????????????????????