pytest需要掌握的:
命令行
参数化
fixture=conftest.py
pytest.ini
安装allure
一、数据驱动,整合unittest
1、新建一个表格,,
2、另存为桌面 ,选择格式:csv,保存到桌面
3、把文件放在data下:
4、导入csv方法读取文件
(1)用列表方式读取:
import csv from utils.pathUtils import filePath def readCsvList(): """列表的方式读取csv文件""" with open(filePath(fileName='data.csv'),encoding='utf-8') as f: reader=csv.reader(f) lists=[] #不读取第一行内容 next(reader) for item in reader: lists.append(item) return lists print(readCsvList())
执行以上代码,,结果为:
执行测试用例
import time as t from page.sina import Sina from page.init import InitSina # from utils.joinUtils import readJson from utils.csvUtils import readCsvList import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆用户名和密码为空验证""" self.login(username=readCsvList()[0][0],password=readCsvList()[0][1]) self.clickLogin self.assertEqual(self.getDivText,readCsvList()[0][2]) def test_sina_login_002(self): """登陆,验证邮箱格式不正确""" self.login(username=readCsvList()[1][0],password=readCsvList()[1][1]) self.assertEqual(self.getDivText,readCsvList()[1][2]) def test_sina_login_003(self): """登陆,验证用户名和密码错误""" self.login(username=readCsvList()[2][0], password=readCsvList()[2][1]) self.clickLogin self.assertEqual(self.getDivText,readCsvList()[2][2])
执行以上代码结果为:
(2)字典方式读取csv的文件内容
def readCsvDict(): """字典方式读取csv的文件内容""" with open(filePath(fileName='data.csv'),encoding='utf-8') as f: lists=[] reader=csv.DictReader(f) for item in reader: lists.append(dict(item)) return lists # print(readCsvDict()) # # print(readCsvDict()[0]['期望结果']) # print(readCsvDict()[1]['期望结果'])
执行测试用例:
from page.sina import Sina from page.init import InitSina from utils.csvUtils import readCsvDict import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆用户名和密码为空验证""" self.login(username=readCsvDict()[0]['\ufeffusername'],password=readCsvDict()[0]['password']) self.assertEqual(self.getDivText,readCsvDict()[0]['期望结果']) def test_sina_login_002(self): """登陆,验证邮箱格式不正确""" self.login(username=readCsvDict()[1]['\ufeffusername'],password=readCsvDict()[1]['password']) self.clickLogin self.assertEqual(self.getDivText,readCsvDict()[1]['期望结果']) def test_sina_login_003(self): """登陆,验证用户名和密码错误""" self.login(username=readCsvDict()[2]['\ufeffusername'],password=readCsvDict()[2]['password']) self.clickLogin self.assertEqual(self.getDivText,readCsvDict()[2]['期望结果'])
执行以上代码,结果为:
(3)读取excel文件内容
import xlrd from utils.pathUtils import filePath def readExcel(): """读取excel的文件""" lists=[] book=xlrd.open_workbook(filePath(fileName='data.xlsx')) sheet=book.sheet_by_index(0) for item in range(1,sheet.nrows): lists.append(sheet.row_values(item)) return lists print(readExcel())
执行测试用例
import time as t from page.sina import Sina from page.init import InitSina from utils.excelUtils import readExcel import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆用户名和密码为空验证""" self.login(username=readExcel()[0][0],password=readExcel()[0][1]) self.clickLogin self.assertEqual(self.getDivText,readExcel()[0][2]) def test_sina_login_002(self): """登陆,验证邮箱格式不正确""" self.login(username=readExcel()[1][0],password=readExcel()[1][1]) self.assertEqual(self.getDivText,readExcel()[1][2]) def test_sina_login_003(self): """登陆,验证用户名和密码错误""" self.login(username=readExcel()[2][0], password=readExcel()[2][1]) self.clickLogin self.assertEqual(self.getDivText,readExcel()[2][2])
执行以上代码为: