这次只测试登录,使用ddt的数据驱动
其实我在uiautomator +python 实现安卓自动化 一文当中我已经使用过ddt数据驱动进行编写测试用例,那么我们来看下测试代码
import unittest from macaca import WebDriver import time import BSTestRunner import ddt login_data=[ {'username':'北漂的','password':'li1213456','id':'tip_btn','assert':'该用户不存在'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'','password':'li123456','id':'tip_input1','assert':'请输入登录用户名'}, {'username':'北漂的雷子','password':'li123456','id':'tip_btn','assert':'用户名或密码错误'}, {'username':'北漂的雷子','password':'','id':'tip_input2','assert':'请输入密码'}, {'username':'北漂的雷子','password':'####','id':'feed_recent','assert':'最新动态'},] die_arp={ 'platformName':'Desktop', 'browserName':'electron' } server_url={ 'hostname':'localhost', 'post':3456 } @ddt.ddt class BokeyuanTest(unittest.TestCase): def setUp(self): self.deriver=WebDriver(die_arp,server_url) self.deriver.init() self.deriver.get('https://passport.cnblogs.com/user/signin') def tearDown(self): self.deriver.quit() @ddt.data(*login_data) def test_login(self,login_data): self.deriver.element_by_id('input1').send_keys(login_data['username']) self.deriver.element_by_id('input2').send_keys(login_data['password']) self.deriver.element_by_id('signin').click() time.sleep(1) self.assertTrue(self.deriver.element_by_id(login_data['id']).text,login_data['assert']) if __name__ == '__main__': suite = unittest.TestSuite() now = time.strftime('%Y-%m%d', time.localtime(time.time())) report_dir = r'%s.html' % now suite.addTests(unittest.TestLoader().loadTestsFromTestCase(BokeyuanTest)) re_open = open(report_dir, 'wb') runner = BSTestRunner.BSTestRunner(stream=re_open, title='demo by macaca', description='测试结果') runner.run(suite)
由于牵涉到个人博客的内容,部分已经屏蔽,那么我来看卡执行结果
看下测试报告
实践感觉:Macaca 运行速度就是快,不到一分钟,7个测试用例,不是一般的快,目前定位的地方还是写死的需要在后期修改,在之后的篇章当中会有介绍。