在前段时间,分享了几篇关于appium基础的博文,再加上期间也有讲到unittest测试框架,所以今天就来一个专题,在appium+python实现的线性代码基础上,引入unittest框架,使代码更简洁。

之前的博客,可以见以下链接快速阅读:

基于Python的Appium环境搭建合集

Genymotion模拟器的安装及脚本制作

Appium Python API 中文版

Appium-Server与Appium-Desktop的区别

单元测试框架Uinttest一文详解

在以上博文中,代码示例,很多只是个线性脚本,没有太多的实用之处,用来写个demo还是可以,但实际运用到产品中,就不行了。脚本还是得引用框架,这样看起来,代码就不会那么乱,更有逻辑性,便于维护。

好了,进入正题,对unittest以及appium还不熟悉的,可以先阅读如上的博文了解,此篇文中,就不赘述了。

unittest代码优化一

优化逻辑:

①将启用服务字段放到初始化当中

②将输入账号、输入密码、获取当前activity的操作封装成一个个函数

③在用例中去调用需要执行的函数

示例代码如下:

import selenium
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest '''
@author: wenyihuqingjiu
@project: appiumcode
@file: test_login.py
@time: 2019-10-20 22:11
@desc:
''' print('selenium version = ', selenium.__version__) def input_account(self):
account = self.driver.find_element_by_id('id/rel_content')
account.find_element_by_id('id/edit_acount')
print("定位输入框")
self.driver.press_keycode(61)
print("换行操作")
account.send_keys('')
print("账号输入完毕")
time.sleep(2) def current(self):
ac = self.driver.current_activity
print('当前activity:' + ac) def input_password(self):
# genymotion只需要一次换行
self.driver.press_keycode(61)
print("换行操作")
password = self.driver.find_element_by_id('id/rel_content')
password.find_element_by_id('id/edit_pass')
print("定位输入框")
password.send_keys('')
print("输入密码")
self.driver.find_element_by_xpath('//android.widget.TextView[@text="登 录"]').click()
print("点击登录")
time.sleep(5)
print('登录成功') class case_01(unittest.TestCase):
@classmethod
def setUpClass(cls): desired_caps = { 'platformName': 'Android',
'platformVersion': '4.4.4',
'deviceName': 'S5',
'appPackage': '',
# 非首次登录的activity
'appActivity': '.ui.login.view.LoginActivity',
'udid': '192.168.66.101:5555',
'noReset': 'true', }
cls.driver = webdriver.Remote('http://192.168.1.101:4723/wd/hub', desired_caps)
print("服务启动成功") @classmethod
def tearDownClass(cls):
cls.driver.quit() def add_img(self):
self.imgs.append(self.driver.get_screenshot_as_base64())
return True def setUp(self):
# 在是python3.x 中,如果在这里初始化driver ,因为3.x版本 unittestbasic1 运行机制不同,会导致用力失败时截图失败
self.driver.implicitly_wait(30)
self.imgs = []
self.addCleanup(self.cleanup) def cleanup(self):
pass def test_login01(self):
input_account(self)
current(self)
input_password(self) if __name__ == "__main__":
suites = unittest.TestSuite()
suites.addTest(case_01("test_login01"))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suites)

unittest代码优化二

优化逻辑:

①将启用服务字段放到初始化当中

②将输入账号、输入密码、获取当前activity的操作封装成一个个函数,并将数据参数做成形参

③在用例中去调用需要执行的函数并入参

示例代码如下:

import selenium
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest '''
@author: wenyihuqingjiu
@project: appiumcode
@file: test_login.py
@time: 2019-10-20 22:11
@desc:
''' print('selenium version = ', selenium.__version__) def input_account(self, username):
account = self.driver.find_element_by_id('id/rel_content')
account.find_element_by_id('id/edit_acount')
print("定位输入框")
self.driver.press_keycode(61)
print("换行操作")
account.send_keys(username)
print("账号输入完毕")
time.sleep(2) def current(self):
ac = self.driver.current_activity
print('当前activity:' + ac) def input_password(self, send_password):
# genymotion只需要一次换行
self.driver.press_keycode(61)
print("换行操作")
password = self.driver.find_element_by_id('id/rel_content')
password.find_element_by_id('id/edit_pass')
print("定位输入框")
password.send_keys(send_password)
print("输入密码")
self.driver.find_element_by_xpath('//android.widget.TextView[@text="登 录"]').click()
print("点击登录")
time.sleep(5)
print('登录成功') class case_01(unittest.TestCase):
@classmethod
def setUpClass(cls): desired_caps = { 'platformName': 'Android',
'platformVersion': '4.4.4',
'deviceName': 'S5',
'appPackage': '',
# 非首次登录的activity
'appActivity': '.ui.login.view.LoginActivity',
'udid': '192.168.66.101:5555',
'noReset': 'true', }
cls.driver = webdriver.Remote('http://192.168.1.101:4723/wd/hub', desired_caps)
print("服务启动成功") @classmethod
def tearDownClass(cls):
cls.driver.quit() def add_img(self):
self.imgs.append(self.driver.get_screenshot_as_base64())
return True def setUp(self):
# 在是python3.x 中,如果在这里初始化driver ,因为3.x版本 unittestbasic1 运行机制不同,会导致用力失败时截图失败
self.driver.implicitly_wait(30)
self.imgs = []
self.addCleanup(self.cleanup) def cleanup(self):
pass def test_login01(self):
# 正常登录
input_account(self, "")
current(self)
input_password(self, "") def test_login02(self):
# 异常登录
input_account(self, "")
current(self)
input_password(self, "") if __name__ == "__main__":
suites = unittest.TestSuite()
suites.addTest(case_01("test_login01"))
suites.addTest(case_01("test_login02"))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suites)

通过上述代码可看出,使用更方便了,可以满足正向/反向的用例设计了,是不是很简单。

代码可扩展性还有很多,在unittest基础上,再引入PO模式,那代码的健壮性就更高了,以后有时间再出类似博文。

基于appium实现的线性代码引用unittest单元测试框架的更多相关文章

  1. Selenium基于Python web自动化基础二 -- 免登录、等待及unittest单元测试框架

    一.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...

  2. Selenium+Python ---- 免登录、等待、unittest单元测试框架、PO模型

    1.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...

  3. Selenium(十八):unittest单元测试框架(四) HTML测试报告

    1. HTML测试报告 对测试人员来而言,测试的产出很难衡量.换句话说,测试人员的价值比较难以量化和评估,相信这一点对软件测试人员来说深有体会.我们花费了很多时间与精力所做的自动化测试也是如此.所以, ...

  4. unittest单元测试框架前言

    一.在我们没有学习过python语言的时候领导让我们做接口测试 我们都使用工具来做测试,一般常用的如jemeter,postman这些个工具,我来推荐使用postman 这个工具来进行接口测试,有的小 ...

  5. Selenium实战(四)——unittest单元测试框架1

    Python中的单元测试框架包含:doctest.unittest.pyttest.nose等,使用unittest单元测试框架不需要自行定义断言失败的提示,并且当一个测试函数执行失败后,后面的测试函 ...

  6. unittest单元测试框架

    unittest单元测试框架 概述: 单元测试框架主要用来完成以下三件事: 提供用例组织与执行:当测试用例只有几条时,可以不必考虑用例的组织,但是当用例达到成百上千条时,大量的用例堆砌在一起,就产生了 ...

  7. unittest单元测试框架简单说明

    unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...

  8. Python+selenium之简单介绍unittest单元测试框架

    Python+selenium之简单介绍unittest单元测试框架 一.unittest简单介绍 unittest支持测试自动化,共享测试用例中的初始化和关闭退出代码,在unittest中最小单元是 ...

  9. unittest单元测试框架总结(转载)

    转载:https://www.cnblogs.com/yufeihlf/p/5707929.html unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该 ...

随机推荐

  1. Salem and Sticks-萨鲁曼的棍子 CodeForce#1105A 暴力

    题目链接:Salem and Sticks 题目原文 Salem gave you 

  2. WordCount程序【Spark Streaming版本】

    ~~ 前置 ~~ Spark Streaming 常常对接 :本地文件.HDFS.端口.flume.kafka package february.streaming import org.apache ...

  3. SpringCloud搭建注册中心与服务注册

    上一篇文章<微服务注册中心原理,看这篇就够了!>介绍了注册中心的概念和原理,本文将介绍下利用Eureka搭建中心并注册服务到注册中心的过程. 本文目录 一.Eureka介绍二.搭建注册中心 ...

  4. linux 装composer的出现的问题

    curl -sS https://getcomposer.org/installer | php php 值得是php的liux下的安装目录 php环境变量 当装compser 的时候,出现      ...

  5. windows下python和pycharm安装及其使用

    1.python安装及环境变量配置 1.1 python安装 1.1.1 python下载 官网下载:https://www.python.org/ Downloads-Windows(Mac os ...

  6. 移动端适配 rem 设置

    refresh();    window.onresize = function(){      setTimeout(function(){        refresh();      },10) ...

  7. ELK 学习笔记之 elasticsearch bool组合查询

    elasticsearch bool组合查询: 相当于sql:where _type = 'books' and (price = 500 or title = 'bigdata') Note: mu ...

  8. 自动下载zar配置管理

    maven 下载: https://maven.apache.org/ 官网 download 下载:Binary zip archive apache-maven-3.6.2-bin.zip 免安装 ...

  9. 分布式系统的延时和故障容错之Spring Cloud Hystrix

    本示例主要介绍 Spring Cloud 系列中的 Eureka,如何使用Hystrix熔断器容错保护我们的应用程序. 在微服务架构中,系统被拆分成很多个服务单元,各个服务单元的应用通过 HTTP 相 ...

  10. Cannot access a disposed object in ASP.NET Core

    Cannot access a disposed object in ASP.NET Core 楠木大叔     导航 常见原因 总结   对于.neter来说,在使用ASP.NET Core的过程中 ...