自动化 测试17

目前在学习ui自动化测试,适用于selenium webdriver3.0+python;本章会先简单介绍自动化测试框架的搭建;

一.数据驱动

数据驱动测试是自动化测试中的主流设计模式之一,属于中级自动化测试工程师必备知识,必须深入掌握数据驱动测试的工作原理和实现方法。

相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为相分离,这样的测试脚本设计模式称为数据驱动。

例如测试网站的登录功能,测试工程师想验证不同的用户名和密码再网站登录时对系统影响的结果,就可以使用数据驱动模式来进行自动化测试。

实施数据驱动的测试步骤如下:

(1).编写测试脚本,脚本需要从程序对象,文件或数据库读入测试数据,

(2).将测试脚本使用的测试数据存入程序对象,文件或数据库等外部介质中,

(3).运行脚本过程中,循环调用存储在外部介质中的测试数据,

(4).验证所有的测试结果是否符合预期结果。

举例说明:

1.在同一个目录下创建data.txt (用来存放测试数据),main.py(用来存放测试脚本)

2.数据驱动的目的是:读取data.txt中所有文件的内容

3.data.txt 文件(电脑中已经存在的txt文件):

d:\a.txt
d:\eula.1028.txt
d:\eula.1031.txt
d:\eula.1033.txt
d:\eula.1036.txt

4.main.py

 

 
#encoding=utf-8
 
#打开步骤3中,创建的data.txt 中的文件目录列表
 
with open("data.txt") as fp:
 
for i in fp:
 
#循环打开每一个文件
 
with open(i.strip()) as fp1:
 
#进行读数据
 
print fp1.read()
 
 


二.关键字驱动

关键字驱动测试框架可以理解为高级的数据驱动测试框架,使用被操作的元素对象,操作的方法和操作的数据值作为测试过程输入的自动化测试框架,简单表示为item.operation(value),被操作的元素对象,操作的方法(operation)和操作的数据值(value)可以保存再数据数据,数据文件,数据库中作为关键字驱动测试框架的输入。主要的思想就是:脚本与数据分离,界面元素名与测试内部对象名分离,测试描述与具体实现细节分离。

例如:在页面上的用户名输入框中输入用户名,则可以在数据文件中进行如下定义:

用户名输入框,输入,testman

举例说明:

 

1.在同一个目录下创建data.txt (用来存放测试数据),kd.py(实现功能的脚本),main.py(利用测试数据执行功能的脚本)

2.data.txt 文件:(定义的功能名称,文件名,对文件的操作模式,操作的数据内容)

op
write,d:\a.txt,w,glory road is good!
read,d:\a.txt,r
3.kd.py

 
#encoding=utf-8
 
def op():
 
with open("d:\a.txt") as fp:
 
print "op:",fp.read()
 
 
 
def write(file_path,mode,content):
 
with open(file_path,mode) as fp:
 
fp.write(content)
 
 
 
def read(file_path,mode):
 
with open(file_path,mode) as fp:
 
print "read:",fp.read()
4.main.py 

 
#encoding=utf-8
 
from kd import *
 
#打开data.txt
 
with open("data.txt") as fp:
 
    for i in fp:
 
        #判断如果有逗号,则需要split分割命令
 
        if i.find(",")!=-1:
 
            prams=i.strip().split(",")
 
            #如果命令有四个参数
 
            if len(prams)==4:
 
                command=prams[0]+"(""+prams[1]+"",""+prams[2]+"",""+prams[3]+"")"
 
                print command
 
                eval(command)
 
            #如果命令有三个参数    
 
            if len(prams)==3:
 
                command=prams[0]+"(""+prams[1]+"",""+prams[2]+"")"
 
                print command
 
                #执行命令,会自动执行kd.py里的函数
 
                eval(command)
 
        #如果命令没有逗号,只有一个参数        
 
        else:
 
            prams=i.strip()
 
            command=prams+"()"
 
            print command
 
            eval(command)

三.数据驱动+关键字驱动混合模式

已经完成数据驱动和关键字驱动框架的搭建,但实际工作中仍然出现不满足测试需求的情况。

举例说明:

1.在同一个目录下创建data.txt (用来存放测试数据),kd.py(实现功能的脚本),file_name.txt (需要数据驱动的数据),main.py(利用测试数据执行功能的脚本)

2.data.txt 文件:(定义的功能名称,文件名,对文件的操作模式,操作的数据内容,{file_name.txt}是需要数据驱动的字段)

write,{file_name.txt},w,glory road is good!

3.kd.py(与关键字驱动的例子完全一样)

 
#encoding=utf-8
 
def op():
 
with open("d:\a.txt") as fp:
 
print "op:",fp.read()
 
 
 
def write(file_path,mode,content):
 
with open(file_path,mode) as fp:
 
fp.write(content)
 
 
 
def read(file_path,mode):
 
with open(file_path,mode) as fp:
 
print "read:",fp.read()
4.file_name.txt

a.txt
b.txt
c.txt
5.main.py 

 
#encoding=utf-8
 
from kd import *
 
with open("data.txt") as fp:
 
    for i in fp:
 
        if i.find(",")!=-1:
 
            prams=i.strip().split(",")
 
            if len(prams)==4:
 
                #判断如果有{,则需要数据驱动
 
                if prams[1].find("{")!=-1:
 
                    file_name=prams[1].strip("{").strip("}")
 
                    with open(file_name) as fp1:
 
                        for j in fp1:
 
                            command=prams[0]+"(""+j.strip()+"",""+prams[2]+"",""+prams[3]+"")"
 
                            print command
 
                            eval(command)  
 
                else:
 
                    command=prams[0]+"(""+prams[1]+"",""+prams[2]+"",""+prams[3]+"")"
 
                    print command
 
                    eval(command)
 

来自:https://blog.csdn.net/qq_30758629/article/details/80376376?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160980730916780273655084%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=160980730916780273655084&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-28-80376376.pc_search_result_no_baidu_js&utm_term=python%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95%E6%A1%86%E6%9E%B6

原文地址:https://www.cnblogs.com/xyt123/p/14233731.html