(Appium + python) UI automation _08_unittest write test cases

Foreword

python unittest is carrying unit testing framework, similar to Junit (Java unit testing framework). Support for automated testing, you can write test pre & post-conditions, and can generate a batch run test cases and test reports.

Use unittest subject to the following points:

1, to be used when introducing unittest module

2, write a test class, and inherit unittest.TestCase

3, test case name begins with test (unittest will automatically be placed in the beginning of the test method test cases focus)

4, assertion assertion format required unittest

Usage Profile

A, front & rear

Front and rear of the front and rear class, a function of: before and after the two kinds of classification set unittest

Opposed front and rear class:

  • Perform tests at the beginning of class, app automation mainly used to start the app - class front; web browser automation is mainly used to start the drive, set up to maximize the browser window, open a default url: setUpClass.
  • tearDownClass: Rear class - class execution at the end of the test, mainly for closing the browser app & answer

Before and after the function set:

  • setUp: Pre - Functions performed at the start of the test function, may be used to write some embodiments preconditions (e.g., operation log, etc.)
  • tesrDown: Post - Functions performed at the end of the test function, may be used to write some embodiments postcondition (e.g. test data initialization)

Simple example:

Remarks:

setUpClass & tearDownClass must use decorator @classmethod 

Second, write test cases

1, test cases need to test at the beginning

2, unittest common assertion

unittest commonly asserted as follows:

  • assertEqual (arg1, arg2, msg = None): verification parameter equal
  • assertNotEqual (arg1, arg2, msg = None): authentication parameter ranges
  • assertTrue (expr, msg = None): verification parameter ture
  • assertFalse (expr, msg = None): false verification parameter
  • assertIsNone (expr, msg = None): verification parameter None
  • assertIsNotNone (expr, msg = None): Non-verification parameter None
  • assertIn (arg1, arg2, msg = None): Verify comprising arg1 arg2

 Simple example (ding-dong app to search for the car plus an example):

Third, the implementation of test cases

By unittest.main () implementation of test cases

Example:

 Fourth, the total sample code

1, the code details

# 叮咚搜索加车(appium+python+unittest)实例
from appium import webdriver
from time import sleep
import unittest

class DingDong(unittest.TestCase):
"""
叮咚买菜搜索加车
"""

@classmethod
def setUpClass(cls):
# 基础信息配置
desired_caps = {
'platformName': 'Android', # 平台
'deviceName': 'emulator-5554', # 手机设备名称
'platformVersion': '6.0.1', # 安卓系统版本号
'appPackage': 'com.yaya.zone', # apk包名
'appActivity': 'com.yaya.zone.activity.SplashActivity', # apk activity
'unicodeKeyboard': True, # 设置编码格式为unicode
'resetKeyboard': True, # 隐藏手机键盘
'noReset': True, # 非初始化
}

cls.driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps) # 启动app
sleep(5)

def test_case01(self):
"""搜索商品加入购物车"""
self.driver.find_element_by_id("com.yaya.zone:id/ll_search").click() # 首页-点击搜索框
sleep(1)
self.driver.find_element_by_id("com.yaya.zone:id/et_what_search").send_keys("鸡腿") # 输入关键词
sleep(1)
self.driver.find_element_by_id("com.yaya.zone:id/btn_cancel").click() # 点击搜索btn
sleep(3)
product_name = self.driver.find_element_by_id("com.yaya.zone:id/tv_name").text # 搜索结果页-商品名
print("搜索结果页_商品名称:", product_name)
product_price = self.driver.find_element_by_id("com.yaya.zone:id/tv_price").text # 搜索结果页-商品价格
print("搜索结果页_商品价格:", product_price)
self.driver.find_element_by_id("com.yaya.zone:id/iv_add_cart").click() # 搜索结果页-点击加车btn
sleep(1)
self.driver.find_element_by_id("com.yaya.zone:id/iv_cart").click() # 点击购物车btn
sleep(3)
cart_product_name = self.driver.find_element_by_id("com.yaya.zone:id/tv_title").text # 购物车-商品名
print("购物车_商品名称:", cart_product_name)
cart_product_price = self.driver.find_element_by_id("com.yaya.zone:id/tv_money").text # 购物车-商品价格
print("购物车_商品价格:", cart_product_price)
self.assertEqual(cart_product_name, product_name) # 判定购物车商品名,同搜索结果页商品名
self.assertEqual(cart_product_price, product_price) # 判定购物车商品价格,同搜索结果页商品价格

def test_case02(self):
"""购物车编辑商品数量"""
cart_product_num = self.driver.find_element_by_id("com.yaya.zone:id/tv_number").text # 购物车-商品数量
print("购物车_商品数量:", cart_product_num)
self.driver.find_element_by_id("com.yaya.zone:id/btn_add").click() # 购物车-增加商品数量
after_add_num = self.driver.find_element_by_id("com.yaya.zone:id/tv_number").text # 获取增加后的商品数量
sleep(2)
self.assertEqual(int(after_add_num), int(cart_product_num)+1) # 判定商品数量
self.driver.find_element_by_id("com.yaya.zone:id/btn_sub").click() # 购物车-减少商品数量
after_reduce_num = self.driver.find_element_by_id("com.yaya.zone:id/tv_number").text # 获取减少后的商品数量
self.assertEqual(after_reduce_num, cart_product_num) # 判定商品数量

if __name__ == '__main__':
unittest.main() # 运行该文件下所有测试用例

2,运行过程,app操作详情

3,运行结果

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/mini-monkey/p/11681011.html