目录
目录
base_page层
common层
read_data.py
send_email.py
test_log.py
page层
page_ECShop_Login.py
testcase层
conftest.py
test_login.py
venv层
pytest.ini
run.tests.py
requirements.txt
目录
base_page层
# basepage 基础页面层(基类)
# 封装关于selenium一系列原生操作 元素定位 最大化 跳转 输入值 点击 等等
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from common.test_log import Logger
log = Logger().get_logger()
class BasePage: # 定义基类
# 类的初始化
def __init__(self):
# try...except 代码异常处理方式
try:
self.driver = webdriver.Chrome()
log.info("正在打开浏览器")
# 负责抛出异常
except Exception:
log.error("无法打开浏览器")
# 封装访问被测页面
def base_open(self, url):
if url != "":
self.driver.get(url)
self.driver.maximize_window()
log.info("正常成功访问被测页面")
else:
log.error("无法打开被测页面,被测页面为空!")
# 封装元素定位
# 隐式等待全局 显示等待单个元素(进阶!)
def base_element(self, loc): # 形参
return WebDriverWait(self.driver, timeout=10, poll_frequency=0.5).until(lambda x: x.find_element(*loc)) # 解包
# 封装跳转frame
def base_frame(self, loc):
# 元素定位frame
f1 = self.base_element(loc)
self.driver.switch_to.frame(f1)
# 封装输入值
def base_send(self, loc, content):
e = self.base_element(loc)
e.clear()
e.send_keys(content)
# 封装点击
def base_click(self, loc):
self.base_element(loc).click()
# 封装获取文本
def base_text(self, loc):
return self.base_element(loc).text
# 封装跳出frame
def base_default(self):
self.driver.switch_to.default_content()
# 封装关闭浏览器
def base_quit(self):
self.driver.quit()
# 封装获取页面标题
def base_title(self):
return self.driver.title
# 封装浏览器刷新
def base_refresh(self):
self.driver.refresh()
# 封装下拉框
def base_select(self, loc, content):
s1 = Select(self.base_element(loc))
s1.select_by_visible_text(content)
# 封装弹窗
def base_alert(self):
alert = self.driver.switch_to.alert
text = alert.text
print("\n------" + text + "------\n")
alert.accept()
return text
common层
read_data.py
import csv
from itertools import islice
import openpyxl
class ReadData:
# 封装读取txt文件的方法
def txt_read(self,path):
# 打开文件(相对路径)
with open(path, "r", encoding="utf-8") as f:
data = f.readlines() # 读取所有行数据 89页
# 新建空列表 用来存储读取到的数据
list1 = []
# for循环遍历所有行数据
for l in data: # 将每一行数据得到,并暂存给临时变量l
# 切片将每行数据最后的转置符号\n去掉,
list2 = l[:-1].split(":")
list1.append(list2)
return list1
# 封装读取csv文件的方法
def csv_read(self, path):
# 导入csv, reader()读取
data = csv.reader(open(path, "r", encoding="gbk"))
# 新建空列表
list1 = []
# for循环遍历读取的文件 islice()第1个参数需要遍历的文件数据,第2个参数开始遍历的位置,第3个参数表示遍历结束的位置
for l in islice(data, 1, None): # 每一行数据遍历到,并暂存给临时变量l
list1.append(l)
return list1
# 封装读取xlsx文件的方法 XML 92 JSON 94
def xlsx_read(self, path):
# openpyxl xlrd 第三方模块读取excel表格数据 下载安装 pip install openpyxl
# 加载excel文件
wb = openpyxl.load_workbook(path)
# 获取sheet对象
sheet = wb["Sheet1"]
# 新建空列表
list1 = []
# 最大行sheet.max_row 最大列sheet.max_column
# for循环遍历
# 遍历行
for r in range(2, sheet.max_row + 1): # 2-5 range(1,10) 1-9 range(1,11) 1-10
# 临时空列表
temp_list = []
# 遍历列
for c in range(1, sheet.max_column + 1): # 1-5
# cell().value 获取每个单元格的值 cell(2,3)
v1 = sheet.cell(r, c).value
temp_list.append(v1)
list1.append(temp_list)
return list1
if __name__ == "__main__": # 防止其他模块调用该模块的类与函数时的影响
# 类class是一种概念模型,没有实际对象!!需要使用类!必须为类创建实例化对象!对象名=类名()
r = ReadData()
send_email.py
import zmail
# ============HTML============
# 读取HTML文件内容
# 发送邮件功能封装
class SendEmail:
def send_html(self, path, sub):
with open(path, "r", encoding='utf-8') as file:
msg = file.read()
# 邮件内容
msg_content = {
'subject': '{}测试报告'.format(sub), # 主题
'content_html': msg, # 正文
'attachments': path # 附件
}
# 收件人(如果有多个收件人,以列表的方式保存)
rec = ['294885949@qq.com']
# 发件人
sender = {
'username': '294885949@qq.com',
'pwd': 'nubwqtjooeogbigb'
}
# 发送邮件
# 登录
server = zmail.server(sender['username'], sender['pwd'])
# 把邮件发送给收件人
server.send_mail(rec, msg_content)
test_log.py
# 日志需要使用loggin模块
import logging
# logging.debug("这是一条普通信息!!!")
"""
1. 创建日志器
2. 创建处理器(将日志信息输出到指定位置)
3. 创建格式器(将处理器所输出的信息格式化)
4. 为处理器设置格式
5.将处理器添加到日志器
6. 返回日志器
"""
# 定义日志类
class Logger:
def get_logger(self):
# 创建日志器
logger = logging.getLogger("MyLogger")
# 为日志器设置默认等级,记录什么等级以上的信息
logger.setLevel(logging.DEBUG)
# 创建处理器,将日志信息输出到控制台
sh = logging.StreamHandler()
# 设置需要在控制台显示什么等级以上的信息
sh.setLevel(logging.WARNING) # 只会在控制台输出警告级别以上的信息
# 创建处理器,将日志信息写入到文件
fh = logging.FileHandler("../report/my_log/test_log.log", encoding="utf-8")
# 设置等级
fh.setLevel(logging.INFO)
# 创建格式器 2020-01-01 0:00:00,000 INFO [root] [Test_Case.py (setUp:5)] - [测试详细信息]:登录测试开始执行初始化操作
f1 = logging.Formatter("%(asctime)s %(levelname)s [%(name)s] %(filename)s (%(funcName)s:%(lineno)d)-%(message)s")
# 给处理器设置输出的格式
sh.setFormatter(f1)
fh.setFormatter(f1)
# 将处理器添加到日志器
logger.addHandler(sh)
logger.addHandler(fh)
return logger
page层
page_ECShop_Login.py
from base.base_page import BasePage
from time import sleep
from selenium.webdriver.common.by import By
class PageLogin(BasePage):
"""用户登录"""
""" 页面元素 """
please_login = (By.LINK_TEXT, '请登录') # 请登录按钮
username = (By.CSS_SELECTOR, '[name="username"]') # 用户名
password = (By.CSS_SELECTOR, '[name="password"]') # 密码
submit = (By.CSS_SELECTOR, '[name="submit"]') # 提交
jump = (By.XPATH, '/html/body/div[5]/div/div/div/div/p[1]') # 跳转通知
out = (By.LINK_TEXT, '退出')
admin_submit = (By.CSS_SELECTOR, '[type="submit"]')
frame1 = (By.CSS_SELECTOR, '#menu-frame')
frame2 = (By.CSS_SELECTOR, '#main-frame')
admin_center = (By.PARTIAL_LINK_TEXT, 'ECSHOP 管理中心')
admin = (By.LINK_TEXT, 'admin')
info = (By.XPATH, '/html/body/div[1]/div/table/tbody/tr[1]/td[2]')
vip_manage = (By.CSS_SELECTOR, '[data-key="03_users_list"]') # 会员管理
order_manage = (By.CSS_SELECTOR,'[data-key="02_order_list"]') # 订单管理
system_manage = (By.CSS_SELECTOR, '[data-key="01_shop_config"]') # 系统设置
vip_sign = (By.LINK_TEXT, '会员注册项设置') # 会员注册
add_vip = (By.LINK_TEXT, "添加会员")
add_vip_submit = (By.CSS_SELECTOR, '[type="submit"]') # 确定添加会员
email = (By.CSS_SELECTOR, '[name="email"]')
confirm_password = (By.CSS_SELECTOR, '[name="confirm_password"]') # 确认密码
edit=(By.LINK_TEXT, '编辑')
view=(By.LINK_TEXT, '查看')
ok=(By.CSS_SELECTOR, '[value="确认"]')
payment=(By.CSS_SELECTOR, '[value="付款"]')
action_note=(By.CSS_SELECTOR, '[name="action_note"]')
send_form =(By.CSS_SELECTOR, '[value="生成发货单"]')
send_ok=(By.CSS_SELECTOR, '[value="确认生成发货单"]')
send_good=(By.CSS_SELECTOR, '[value="去发货"]')
send=(By.CSS_SELECTOR, '[value="发货"]')
is_no_send=(By.XPATH, '//*[@id="listDiv"]/table[1]/tbody/tr[3]/td[7]')
# true_or_false = (By.XPATH, '//*[@id="list-table"]/tbody/tr[5]/td[4]/img')
# length = len(driver.find_elements(By.XPATH, '//*[@id="listDiv"]/table/tbody/tr'))
# 点击请登录按钮
def page_please_login(self):
self.base_click(PageLogin.please_login)
# 输入用户名
def page_username(self, username):
self.base_send(PageLogin.username, username)
# 输入密码
def page_password(self, password):
self.base_send(PageLogin.password, password)
# 输入确认密码
def page_confirm_password(self, confirm_password):
self.base_send(PageLogin.confirm_password, confirm_password)
# 输入邮箱
def page_email(self, email):
self.base_send(PageLogin.email, email)
# 点击提交
def page_submit(self, submit):
self.base_click(submit)
# 获取跳转也红字文本
def page_jump(self):
return self.base_text(PageLogin.jump)
# 点击退出
def page_out(self):
self.base_click(PageLogin.out)
# 获取会员人数
def vip_num(self):
return len(self.driver.find_elements(By.XPATH, '//*[@id="listDiv"]/table/tbody/tr'))
# 管理员页面 frame 跳转
def page_admin_frame(self, manage):
self.base_frame(PageLogin.frame1)
sleep(1)
self.base_click(manage)
sleep(1)
self.base_default()
sleep(1)
self.base_frame(PageLogin.frame2)
# 点击添加会员
def page_add_vip(self, username, password, confirm_password, email):
self.page_admin_frame(PageLogin.vip_manage)
self.base_click(PageLogin.add_vip)
self.page_username(username)
self.page_email(email)
self.page_password(password)
self.page_confirm_password(confirm_password)
self.page_submit(PageLogin.admin_submit)
return self.base_text(PageLogin.info)
# 修改会员信息
def page_change(self,password, confirm_password, email):
self.page_admin_frame(PageLogin.vip_manage)
self.base_click(PageLogin.edit)
self.page_email(email)
self.page_password(password)
self.page_confirm_password(confirm_password)
self.page_submit(PageLogin.admin_submit)
return self.base_text(PageLogin.info)
def page_delete_vip(self):
self.page_admin_frame(PageLogin.vip_manage)
length = self.vip_num()
i = 3
while i <= length-3:
self.driver.find_element(By.XPATH, f'//*[@id="listDiv"]/table/tbody/tr[{i}]/td[1]/input').click()
i = i+1
self.driver.find_element(By.CSS_SELECTOR, "#btnSubmit").click()
return self.base_alert()
# 系统设置手机号验证关闭
def page_sys_manage(self):
self.base_frame(PageLogin.frame1)
self.base_click(PageLogin.system_manage)
self.base_click(PageLogin.vip_sign)
self.base_default()
self.base_frame(PageLogin.frame2)
src = self.driver.find_element(By.XPATH, '//*[@id="list-table"]/tbody/tr[5]/td[4]/img').get_attribute("src")
if src == 'http://127.0.0.1/ecshop/admin/images/yes.svg':
self.driver.find_element(By.XPATH, '//*[@id="list-table"]/tbody/tr[5]/td[4]/img').click()
return src
def page_send(self,action_note):
self.page_admin_frame(PageLogin.order_manage)
self.base_click(PageLogin.view)
self.base_click(PageLogin.ok)
# self.base_text(PageLogin.info)
self.base_click(PageLogin.payment)
self.base_send(PageLogin.action_note,action_note)
self.base_click(PageLogin.submit)
self.base_click(PageLogin.send_form)
self.base_click(PageLogin.send_ok)
self.base_click(PageLogin.send_good)
self.base_click(PageLogin.view)
self.base_click(PageLogin.send)
return self.base_text(PageLogin.is_no_send)
# 封装登录流程
def ec_login(self, username, password):
self.page_please_login()
self.page_username(username)
self.page_password(password)
self.page_submit(PageLogin.submit)
if username != "" and password != "":
j1 = self.page_jump()
if j1 == '登录成功':
self.page_out()
self.base_refresh()
return j1
else:
a = self.base_alert()
self.base_refresh()
return a
# 封装管理员登录流程
def ec_admin_login(self, username, password):
self.page_username(username)
self.page_password(password)
self.page_submit(PageLogin.admin_submit)
if username == "":
result = self.base_alert()
else:
if self.driver.title == 'ECSHOP 管理中心':
self.base_frame(PageLogin.frame1)
result = self.base_text(PageLogin.admin)
self.base_click(PageLogin.admin)
self.page_out()
else:
result = self.base_text(PageLogin.info)
self.base_default()
self.base_open("http://127.0.0.1/ecshop/admin/privilege.php?act=login")
return result
# 管理员模块
def ec_admin_model(self, username, password, confirm_password, email, zsgc):
self.page_username('admin')
self.page_password('a1234567')
self.page_submit(PageLogin.admin_submit)
if zsgc == '增':
result = self.page_add_vip(username, password, confirm_password, email)
elif zsgc == "改":
result = self.page_change(password, confirm_password, email)
elif zsgc == '删':
result = self.page_delete_vip()
elif zsgc == '关闭验证':
result = self.page_sys_manage()
else:
result = self.page_send(username)
self.base_default()
self.base_frame(PageLogin.frame1)
self.base_click(PageLogin.admin)
self.page_out()
self.base_open("http://127.0.0.1/ecshop/admin/privilege.php?act=login")
return result
class PageECShopUserCenter(BasePage):
"""用户中心"""
""" 页面元素 """
please_login = (By.LINK_TEXT, '请登录') # 请登录按钮
username = (By.CSS_SELECTOR, '[name="username"]') # 用户名
password = (By.CSS_SELECTOR, '[name="password"]') # 密码
submit = (By.CSS_SELECTOR, '[name="submit"]') # 提交
user_center = (By.LINK_TEXT, '用户中心')
user_info = (By.LINK_TEXT, '用户信息')
year = (By.CSS_SELECTOR, '[name="birthdayYear"]') # 年
month = (By.CSS_SELECTOR, '[name="birthdayMonth"]') # 月
day = (By.CSS_SELECTOR, '[name="birthdayDay"]') # 日
girl = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[2]/td[2]/input[3]')
boy = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[2]/td[2]/input[2]')
secrecy = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[2]/td[2]/input[1]') # 性别保密
email = (By.CSS_SELECTOR, '[name="email"]')
submit1 = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[10]/td/input[2]') # 点击用户信息下面的确认修改
submit2 = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[2]/table/tbody/tr[4]/td/input[2]') # 点击确认密码下面确认修改
out_password = (By.CSS_SELECTOR, '[name="old_password"]')
new_password = (By.CSS_SELECTOR, '[name="new_password"]')
comfirm_password = (By.CSS_SELECTOR, '[name="comfirm_password"]')
address = (By.LINK_TEXT, '收货地址')
new_address = (By.CSS_SELECTOR, '[value="新增收货地址"]')
# 删除最上面的地址
del_address = (By.XPATH, '/html/body/div[6]/div[2]/div/div/div/form[1]/table/tbody/tr[5]/td[2]/input[2]')
# 修改第一个用户的地址
province = (By.CSS_SELECTOR, '#selProvinces_0') # 省
city = (By.CSS_SELECTOR, '#selCities_0') # 城市
district = (By.CSS_SELECTOR, '#selDistricts_0') # 区
consignee = (By.CSS_SELECTOR, '#consignee_0') # 姓名
email1 = (By.CSS_SELECTOR, '#email_0') # 邮箱
address1 = (By.CSS_SELECTOR, '#address_0') # 详细地址
mobile = (By.CSS_SELECTOR, '#mobile_0') # 手机
jump = (By.XPATH, '/html/body/div[5]/div/div/div/div/p[1]') # 跳转通知
out = (By.LINK_TEXT, '退出')
# 用户登录
def page_login(self, username, password):
self.base_click(PageECShopUserCenter.please_login)
self.base_send(PageECShopUserCenter.username, username)
self.base_send(PageECShopUserCenter.password, password)
self.base_click(PageECShopUserCenter.submit)
# 用户中心
def page_user_center(self):
self.base_click(PageECShopUserCenter.user_center)
# 用户信息
def page_user_info(self):
self.base_click(PageECShopUserCenter.user_info)
# 点击年月日
def page_birthday(self, year, month, day):
sleep(1)
if year != "":
self.base_select(PageECShopUserCenter.year, year)
sleep(1)
if month != "":
self.base_select(PageECShopUserCenter.month, month)
sleep(1)
if day != "":
self.base_select(PageECShopUserCenter.day, day)
sleep(1)
# 选择性别
def page_sex(self, sex):
if sex == '1':
self.base_click(PageECShopUserCenter.secrecy)
elif sex == '2':
self.base_click(PageECShopUserCenter.boy)
else:
self.base_click(PageECShopUserCenter.girl)
# 修改邮箱
def page_email(self, email):
self.base_send(PageECShopUserCenter.email, email)
# 点击确认修改
def page_user_center_submit(self, submit):
if submit == '1':
self.base_click(PageECShopUserCenter.submit1)
else:
self.base_click(PageECShopUserCenter.submit2)
# 修改密码
def page_new_password(self, out_password, new_password, comfirm_password):
self.base_send(PageECShopUserCenter.out_password, out_password)
self.base_send(PageECShopUserCenter.new_password, new_password)
self.base_send(PageECShopUserCenter.comfirm_password, comfirm_password)
# 点击收货地址
def page_address(self):
self.base_click(PageECShopUserCenter.address)
# 删除最上面的收货地址
def page_del_address(self):
self.base_click(PageECShopUserCenter.del_address)
return self.base_alert()
# # 点击新增收货地址
# def page_new_address(self):
# self.base_click(PageECShopUserCenter.new_address)
# 判断有几个收货地址
def page_len_address(self):
length = len(self.driver.find_elements(By.CSS_SELECTOR, '[name="theForm"]'))
length = length - 1
return length
# 新增地址
def page_new_address(self, province, city, district, consignee, address, mobile, length):
if province != '':
self.base_select((By.CSS_SELECTOR, f'#selProvinces_{length}'), province)
sleep(1)
if city != '':
self.base_select((By.CSS_SELECTOR, f'#selCities_{length}'), city)
sleep(1)
if district != '':
self.base_select((By.CSS_SELECTOR, f'#selDistricts_{length}'), district)
sleep(1)
self.base_send((By.CSS_SELECTOR, f'#consignee_{length}'), consignee)
self.base_send((By.CSS_SELECTOR, f'#address_{length}'), address)
self.base_send((By.CSS_SELECTOR, f'#mobile_{length}'), mobile)
self.base_click(PageECShopUserCenter.new_address)
if province == "" or city == "" or district == "" or consignee == "" or address == "" or mobile == "":
a = self.base_alert()
return a
else:
return self.base_text(PageECShopUserCenter.jump)
# 修改地址
def page_modify_address(self, province, city, district, consignee, address, mobile, email):
self.base_select(PageECShopUserCenter.province, province)
sleep(1)
self.base_select(PageECShopUserCenter.city, city)
sleep(1)
self.base_select(PageECShopUserCenter.district, district)
sleep(1)
self.base_send(PageECShopUserCenter.email1, email)
self.base_send(PageECShopUserCenter.consignee, consignee)
self.base_send(PageECShopUserCenter.address1, address)
self.base_send(PageECShopUserCenter.mobile, mobile)
self.base_click(PageECShopUserCenter.submit) # 确认修改
# 点击退出
def page_out(self):
self.base_click(PageECShopUserCenter.out)
# 用户信息
def ec_user_center(self, username, password, year, month, day, sex, email, out_password,
new_password, comfirm_password, submit, num):
self.page_login(username, password)
self.page_user_center()
self.page_user_info()
print("------------------------------=====" + num)
if submit == '1':
self.page_birthday(year, month, day)
self.page_sex(sex)
self.page_email(email)
self.page_user_center_submit(submit)
sleep(2)
else:
print("------------------------------=====2" + num)
self.page_new_password(out_password, new_password, comfirm_password)
self.page_user_center_submit(submit)
sleep(2)
if num == '1':
result = self.base_text(PageECShopUserCenter.jump)
if result == '您的新密码已设置成功!':
return result
else:
result = self.base_alert()
sleep(3)
self.page_out()
sleep(3)
self.base_refresh()
return result
# 收货地址-增删改
def ec_address(self, username, password, province, city, district, consignee, address, mobile, email, zsg):
self.page_login(username, password)
self.page_user_center()
self.page_address()
length = self.page_len_address()
if zsg == '改':
self.page_modify_address(province, city, district, consignee, address, mobile, email)
if province == "" or city == "" or district == "" or consignee == "" or address == "" or mobile == "" or email == "":
result = self.base_alert() # 获取警告框文本并关闭
self.page_out()
return result
result = self.base_text(PageECShopUserCenter.jump)
elif zsg == '删':
result = self.page_del_address()
else:
result = self.page_new_address(province, city, district, consignee, address, mobile, length)
self.page_out()
self.base_refresh()
return result
testcase层
conftest.py
# pytest的测试配置文件
# 一定要创建在测试用例所在路径
# 编写全局测试夹具
# pytest夹具
import pytest
from page.page_ECShop_Login import PageLogin, PageECShopUserCenter
from page.page_ECShop_Shopping import PageECShopSearch, PageECShopShopping
from page.page_ECShop_sign import PageSign
@pytest.fixture(scope="class") # 非自动运行
def ec_login_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageLogin() # 对象
p.base_open("http://127.0.0.1/ecshop/")
yield p # 与return区别
# 还原清理
p.base_quit()
@pytest.fixture(scope="class") # 非自动运行
def ec_admin_login_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageLogin() # 对象
p.base_open("http://127.0.0.1/ecshop/admin/privilege.php?act=login")
yield p # 与return区别
# 还原清理
p.base_quit()
@pytest.fixture(scope="class") # 非自动运行
def ec_user_center_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageECShopUserCenter() # 对象
p.base_open("http://127.0.0.1/ecshop/")
yield p # 与return区别
# 还原清理
p.base_quit()
@pytest.fixture(scope="class") # 非自动运行
def es_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageECShopSearch() # 对象
p.base_open("http://127.0.0.1/ecshop/")
yield p # 与return区别
# 还原清理
p.base_quit()
@pytest.fixture(scope="class") # 非自动运行
def es_shopping_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageECShopShopping() # 对象
p.base_open("http://127.0.0.1/ecshop/")
yield p # 与return区别
# 还原清理
p.base_quit()
@pytest.fixture(scope="class") # 非自动运行
def ec_sign_fixture(): # 单独定义的函数!没有self参数!!
# 初始化
p = PageSign() # 对象
p.base_open("http://127.0.0.1/ecshop/")
yield p # 与return区别
# 还原清理
p.base_quit()
test_login.py
import pytest
from common.read_data import ReadData
class TestECShopSign:
# 测试用例
# pytest的参数化
@pytest.mark.sign
@pytest.mark.parametrize("username, email, password, conform_password, "
"QQ, work_phone, home_phone, phone, question, "
"passwd_answer, ass",
ReadData().csv_read("../data/csv_ECShop_sign.csv"))
def test_sign(self, username, email, password, conform_password, QQ,
work_phone, home_phone, phone, question, passwd_answer,
ass, ec_sign_fixture):
print("OOOOOOOOOOOOOOOO" + ass)
a = ec_sign_fixture
result = a.ec_sign(username, email, password, conform_password,
QQ, work_phone, home_phone, phone, question, passwd_answer)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
class TestECShopLogin:
@pytest.mark.login
@pytest.mark.parametrize("username, password, ass", ReadData().csv_read("../data/ECShop_Login.csv"))
def test_login(self, username, password, ass, ec_login_fixture):
a = ec_login_fixture
result = a.ec_login(username, password)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
class TestECShopShopping:
# 测试用例
# pytest的参数化
@pytest.mark.Shopping
@pytest.mark.parametrize("user, pwd, province, city, district, consignee, address, mobile, tr, pay, ass",
ReadData().csv_read("../data/Ecshop_Shopping.csv"))
def test_shopping(self, user, pwd, province, city, district, consignee, address, mobile, tr, pay, ass, es_shopping_fixture):
a = es_shopping_fixture
result = a.ec_shopping(user, pwd, province, city, district, consignee, address, mobile, tr, pay)
# 断言
assert ass in result
print("\n+++"+ass+"\n")
print("\n+++"+result)
class TestECSSearch:
# 测试用例
# pytest的参数化
@pytest.mark.search
@pytest.mark.parametrize("content, ass", ReadData().csv_read("../data/Ecshop_Search.csv"))
def test_search(self, content, ass, es_fixture):
a = es_fixture
result = a.ec_search(content)
# 断言
assert ass in result
print("\n+++"+ass+"\n")
print("\n+++"+result)
class TestECShopUserCenter:
@pytest.mark.center
@pytest.mark.parametrize("username, password, year, month, day, sex, email, out_password, "
"new_password, comfirm_password, submit, num, ass",
ReadData().csv_read("../data/ECShop_UserCenter.csv"))
def test_user_center(self, username, password, year, month, day, sex, email, out_password,
new_password, comfirm_password, submit, num, ass, ec_user_center_fixture):
a = ec_user_center_fixture
result = a.ec_user_center(username, password, year, month, day, sex, email, out_password,
new_password, comfirm_password, submit, num)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
class TestECShopAddress:
@pytest.mark.address
@pytest.mark.parametrize("username, password, province, city, district, consignee, address, mobile, email"
", zsg, ass", ReadData().csv_read("../data/ECShop_Address.csv"))
def test_address(self, username, password, province, city, district, consignee, address, mobile,
email, zsg, ass, ec_user_center_fixture):
a = ec_user_center_fixture
result = a.ec_address(username, password, province, city, district, consignee, address, mobile, email, zsg)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
class TestECShopAdminLogin:
@pytest.mark.admin_login
@pytest.mark.parametrize("username, password, ass", ReadData().csv_read("../data/ECShop_Admin_Login.csv"))
def test_admin_login(self, username, password, ass, ec_admin_login_fixture):
a = ec_admin_login_fixture
result = a.ec_admin_login(username, password)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
class TestECShopAdmin:
@pytest.mark.admin
@pytest.mark.parametrize("username, password, confirm_password, email, zsgc, ass",
ReadData().csv_read("../data/ECShop_Admin.csv"))
def test_admin(self, username, password, confirm_password, email, zsgc, ass, ec_admin_login_fixture):
a = ec_admin_login_fixture
result = a.ec_admin_model(username, password, confirm_password, email, zsgc)
# 断言
print("\nass = -->" + ass)
print("\nresult = -->" + result)
assert ass in result
venv层
pytest.ini
[pytest] # 设置测试执行默认参数 addopts = -vs # 设置默认测试用例路径 # ./当前路径venv ../上级路径 testpaths = ../testcase # 设置默认的模块名称 python_files = test_login.py # 设置默认测试类名称 python_classes = Test* # 设置默认测试用例函数名称 python_functions = test_ # 设置标记 markers = sign:ECShop注册 login:ECShop登录 Shopping:购物功能 search:搜索功能 center:ECShop用户中心 address:收货地址增删改 admin_login:管理员登录 admin:管理模块
run.tests.py
import time
import pytest
result_name = time.strftime("%Y-%m-%d %H-%M-%S")
# pytest套件 指定路径 指定模块 指定类 指定测试用例
# pytest套件 分组执行 -m
pytest.main([f"--html=../report/{result_name}.html"])
# pytest.main(["-m=admin", f"--html=../report/{result_name}.html"])
# -m=baidu or webtour 同时运行两个标记的用例
# pytest-html 用户生成Html格式报告的第三方模块
# pytest可以兼容Unittest
requirements.txt
pytest
pytest-xdist
pytest-ordering
pytest-rerunfailures
pytest-html
allure-pytest