本系列文章是针对Android手机自动化测试做的分享总结,方便以后工作中遇到可以随时查阅,首先搭建环境Python36,安装项目所用工具Pycharm等。
工具下载安装
Pycharm:https://ptest1234.ctfile.com/fs/16324556-289107881 (破解码地址:http://idea.lanyus.com/)
Python36:https://ptest1234.ctfile.com/fs/16324556-289108039
HTMLTestRunner:地址同上,存放位置在Python36安装目录,如:E:\Python36\Lib
以上工具安装完成以后,需要导入所对应到资源文件:urllib3、uiautomator,安装方式有两种:
1.使用工具Pycharm:打开工具-》File-》Settings-》project:项目名-》project Interpreter,如果没有的点击添加按钮,获取
2.使用CMD命令方式安装,开始-》运行-》输入CMD,回车,命令窗口分别输入,等待下载安装:
pip install uiautomator
pip install urllib3
Unittest案例事例
# -*- coding:utf-8 -*-import sysimport importlibimportlib.reload(sys)import unittestimport HTMLTestRunner,sys,StringIOimport timeimport osfrom uiautomator import device as dclass MyTestCase(unittest.TestCase): def setUp(self): #print 'aaa' 运行前先运行此函数 pass def tearDown(self): #所有用例运行完后,运行此函数 pass def testCase1(self): self.assertEqual(2,2,"testError") def testCase2(self): self.assertEqual(2,2,"testError")#添加Suitedef Suite(): suiteTest = unittest.TestSuite() suiteTest.addTest(MyTestCase("testCase1")) suiteTest.addTest(MyTestCase("testCase2")) return suiteTestif __name__ == '__main__': # 确定生成报告的路径 timer = time.strftime('%Y-%m-%d %H_%M_%S ', time.localtime(time.time())) filePath = timer+"pyResult.html" fp = open(filePath,'wb') # 生成报告的Title,描述 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title='Python Test Report',description='This is Python Report') runner.run(Suite(1)) fp.close()