安装
pip install airtest
pip install opencv-python
复制
import HTMLTestRunner
import unittest
import time
from airtest.core.api import *
from airtest.report.report import simple_report
import os
# 初始化
auto_setup(__file__)
# 连接到指定设备
connect_device("Android:///10AD9T0CS80028C")
os.system("adb shell am start -a android.settings.BLUETOOTH_SETTINGS")
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_error(self):
""" This test should be marked as error one. """
raise ValueError
def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)
@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass
def test_bluetooth_switch(self):
try:
# 打开蓝牙开关
touch(Template(r"tpl1711383764391.png", record_pos=(0.375, -0.71), resolution=(1260, 2800)))
sleep(5) # 等待蓝牙开关操作完成
# 检查蓝牙是否成功打开
if exists(Template(r"tpl1711383764391.png")):
print("蓝牙成功打开")
else:
raise AssertionError("蓝牙未成功打开")
# 关闭蓝牙开关
touch(Template(r"tpl1711383807120.png", record_pos=(0.369, -0.717), resolution=(1260, 2800)))
sleep(2) # 等待蓝牙开关操作完成
# 检查蓝牙是否成功关闭
if exists(Template(r"tpl1711383807120.png")):
print("蓝牙成功关闭")
else:
raise AssertionError("蓝牙未成功关闭")
except Exception as e:
print("测试用例执行出现异常:", e)
def Suite():
suite = unittest.TestSuite()
# 添加测试用例到测试套件
suite.addTest(TestStringMethods('test_upper'))
suite.addTest(TestStringMethods('test_error'))
suite.addTest(TestStringMethods('test_fail'))
suite.addTest(TestStringMethods('test_skip'))
suite.addTest(TestStringMethods('test_bluetooth_switch'))
return suite
if __name__=="__main__":
for i in range(10):
print(f"执行第 {i+1} 次测试")
testSuite = Suite()
now = time.strftime("%Y-%m-%d %H_%M_%S")
Html = '.\\' + now + f'_result_{i+1}.htm'
fp = open(Html, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'百度搜索测试报告', description=u'用例执行情况:')
runner.run(testSuite)
fp.close()
# 生成简单测试报告
# simple_report(__file__)