shuguang's blog

环境决定基础,选择决定价值,努力决定方向。

自动化测试模型

  • 本文主要介绍自动化测试模型相关概念和实例。

#区分自动化测试库、框架和工具

  • 自动化测试库(Library)
    • 类似于面向对象的类库,面向过程的函数库,自动化测试库剔红了一组操作Web页面的方法。
  • 框架(Framework)
    • 框架是为解决一个或一类问题而开发得产品,unittest框架主要用于测试用例组织和执行工作,因此通常称为单元测试框架
    • 工具(Tools)
      • 工具与框架类似,只是会有更高的抽象(一般会有单独得操作界面),屏蔽了底层代码,例如selenium IDE和QTP就是自动化测试工具。
  • 自动化测试模型:
    • 可以认为是自动化测试框架与工具设计的思想。

#自动化测试模型介绍

  • 线性测试

    • 通过录制或编写对应程序的操作步骤的线性脚本,每个脚本相对独立,且不产生其他依赖与调用。
    • 优点:
      • 每个脚本独立完整可单独执行。
    • 缺点:
      • 测试用例的开发和维护成本高。

  • 模块化驱动测试

    • 借鉴编程语言中的模块化思想,抽取重复的操作独立成公共模块。
    • 优点:
      • 提高了开发效率,降低了维护复杂性

  • 数据驱动测试

    • 由于数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变。(脚本与数据分离,也就是参数化
    • 优点:
      • 进一步增强了脚本的复用性。

  • 关键字驱动测试

    • 封装了底层代码,通过关键字的改变从而引起测试结果的改变。(过程式的编写用例)

    • 典型的关键字驱动工具以UTF、RIDE为主。(封装了底层代码实现,提供用户独立的用户界面)

    • 优点:

      • 降低了脚本编写的难度。

#模块化驱动测试实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# mail163.py
from selenium import webdriver
from .public import Login
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://email.163.com/")

# 调用登录模块
Login().user_login(driver)

# 收信 ...

# 调用退出模块
Login().user_logout(driver)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# public.py
# 登录
class Login():
def user_login(self, driver):
driver.find_element_by_css_selector(".j-inputtext").clear()
driver.find_element_by_css_selector(".j-inputtext").send_keys("xxxxxx")
driver.find_element_by_css_selector(".dlpwd").clear()
driver.find_element_by_css_selector(".dlpwd").send_keys("xxxxxxx")
driver.find_element_by_css_selector(".u-loginbtn").click()
# 收信
# ......

# 退出
def user_logout(self, driver):
driver.find_element_by_link_text("退出").click()
driver.quit()

#数据驱动测试实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# mail163.py
from selenium import webdriver
from .public import Login
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://email.163.com/")

username = "guest"
password = "123456"

# 调用登录模块
Login().user_login(driver, username, password)

# 收信 ...

# 调用退出模块
Login().user_logout(driver)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# public.py
# 登录
class Login():
def user_login(self, driver, username, password):
driver.find_element_by_css_selector(".j-inputtext").clear()
driver.find_element_by_css_selector(".j-inputtext").send_keys(username)
driver.find_element_by_css_selector(".dlpwd").clear()
driver.find_element_by_css_selector(".dlpwd").send_keys(password)
driver.find_element_by_css_selector(".u-loginbtn").click()
# 收信
# ......

# 退出
def user_logout(self, driver):
driver.find_element_by_link_text("退出").click()
driver.quit()