目前 uiautomator2 好像不支持测试报告的输出,所以考虑用 unittest 测试框架里的 HTMLTestRunner(该修改的要修改好)实现测试报告的输出。给需要的人参考,新手上路,请多指教。

代码如下:
import unittest
import uiautomator2 as u2
import time
from Lib import HTMLTestRunner
import uiautomator2.ext.htmlreport as htmlreport

class xxx(unittest.TestCase):
@classmethod
def setUpClass(c):
c.u = u2.connect_usb('S9B4C17415007705')
c.u.healthcheck()
# 输出截图
hrp = htmlreport.HTMLReport(c.u,'report1')
hrp.patch_click()
c.u.toast.show("test begin", 1)

@classmethod
def tearDownClass(c):
c.u.toast.show("test over", 1)
c.u.app_stop_all()
c.u.service("uiautomator").stop()

def setUp(self):
self.d = self.u.session("测试的包名称")

def tearDown(self):
pass

def testOne(self):
self.d(resourceId="uid").set_text("lp")
self.d(resourceId="upwd").set_text("888888")
self.d.click(0.774, 0.628)
self.d.toast.show("testOne", 1)

def testTwo(self):
self.d(resourceId="uid").set_text("lh")
self.d(resourceId="upwd").set_text("888888")
self.d.click(0.774, 0.628)
self.d.toast.show("testTwo", 1)

def testThree(self):
self.d(resourceId="uid").set_text("dx")
self.d(resourceId="upwd").set_text("888888")
self.d.click(0.774, 0.628)
self.d.toast.show("testThree", 1)

if name == 'main':
suite = unittest.TestSuite()
suite.addTest(xxx('testOne'))
time.sleep(3)
suite.addTest(xxx('testTwo'))
time.sleep(3)
suite.addTest(xxx('testThree'))
time.sleep(3)
# 输出测试报告
filename = 'D:\python\atx.html'
fb = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fb, title='ATX', description='Atx Report')
runner.run(suite)
fb.close()


↙↙↙阅读原文可查看相关链接,并与作者交流