po设计模式实战-----风暴平台

1、风暴平台实战

目录结构如下图所示:

 1.1base层

#base:基础层,主要编写底层定位元素的类,它是一个包;元素定位有两类,单个元素定位和多个元素定位,还有一种特殊情况,即需要定位的元素在iframe框架中。
from selenium.webdriver.support.expected_conditions import NoSuchElementException    #显示异常信息要导入的类
# import time
#定义一个编写底层定位元素的类
class WebUI(object):
	def __init__(self,driver):
#=driver里的drive可以理解为webdriver实例化后的对象,这里只是做一个假设,在测试类中进行验证,
		self.driver=driver

		'''单个元素定位的方式
			args:*args:识别元素属性,ctr+鼠标放置到元素+左键:判断定位元素属性的方法是否正确
			:return:它是一个元组,需要带上具体什么方式定位元素属性以及元素属性的值'''

#单个元素定位:
	# def findElement(self,*args):
	# 	return self.driver.find_element(*args)

	# 如果想要获取到运行异常是的详细信息,可以使用函数异常逻辑,except返回具体错误信息。
	def findElement(self,*args):
		try:
			return self.driver.find_element(*args)
		except NoSuchElementException as e:
			return e.args[0]

		'''单个元素定位的方式
			args:*args:识别元素属性,ctr+鼠标放置到元素+左键:判断定位元素属性的方法是否正确
			index:识别元素的索引
			:return:它是一个元组,需要带上具体什么方式定位元素属性以及元素属性的值'''

# #多个元素定位,由于需要使用到他的索引,所以需要加一个index参数。
# 	def findElements(self, *args, index):
# 		return self.driver.find_elements(*args)[index]

# 如果想要获取到运行异常是的详细信息,可以使用函数异常逻辑,except返回具体错误信息。
	def findElements(self,*args,index):
		try:
			return self.driver.find_elements(*args)[index]
		except NoSuchElementException as e:
			return e.args[0]

#iframe框架中的元素定位,这里简述通过ID的方式进入iframe框架:
	def findFrame(self,frameID):
		return self.driver.switch_to.frame(frameID)

1.2common层

import os
def base_dir():
	return os.path.dirname(os.path.dirname(__file__))
print(base_dir())

1.3data层

login":[
  {"accountTextInformation": ["请输入您的登录账户","请输入正确的手机号码!"]},
  {"passwdTextInformation": ["请输入登录账户密码","密码字符应该在6-20之间!"]}
],
  "Add": [
    {"Pname":"请输入产品名称","Ptype": "请选择产品类型","Pversion": "请输入产品版本号","Pmaster": "请输入产品负责人","Pdescription": "请输入产品描述信息"}
  ]

1.4page层

page_firmware_storm:测试固件对象层
from selenium import webdriver    #自动化测试必须要继承的类,主要用在初始化中。
import time   #关于时间的库。
import unittest   #初始化和清零必须用到的库。
#定义一个测试固件的对象层的类
class LoginFirmware(unittest.TestCase):
	#初始化
	def setUp(self) -> None:
		self.driver=webdriver.Chrome()
		self.driver.maximize_window()
		self.driver.get("http://101.43.158.84/#/login")
		time.sleep(3)
		self.driver.implicitly_wait(30)

	#清理
	def tearDown(self) -> None:
		self.driver.quit()
page_storm_login:登录对象层
from base.base import WebUI    #继承base中关于元素定位的类:WebUI
from selenium.webdriver.common.by import By    #元素定位时必须要导入的类:By。
import time  #关于时间的库。
#定义一个登录风暴平台的对象层的类
class Login(WebUI):    #需要定位元素,所以要继承关于元素定位的类:WebUI
	#定位账户输入框的数据属性
	account=(By.XPATH,"/html/body/div/div/div/div/div/form/div[1]/div/div/input")
	#定位密码输入框的数据属性
	passwd=(By.XPATH,"/html/body/div/div/div/div/div/form/div[2]/div/div/input")
	#定位登录按钮的数据属性
	loginButton=(By.XPATH,"/html/body/div/div/div/div/div/form/div[3]/div/button/span")
	#定位用户输入框提示信息的数据属性
	textUser=(By.XPATH,"/html/body/div/div/div/div/div/form/div[1]/div/div[2]")
	#定位密码输入框提示信息的数据属性
	textPasswd=(By.XPATH,"/html/body/div/div/div/div/div/form/div[2]/div/div[2]")



#定义一个输入账户名的函数
	def input_account(self,user):
		self.findElement(*self.account).send_keys(user)
		time.sleep(2)

#定义一个输入密码的函数
	def input_passwd(self,code):
		self.findElement(*self.passwd).send_keys(code)
		time.sleep(2)

#定义一个点击登录按钮的函数
	def click_login(self):
		self.findElement(*self.loginButton).click()
		time.sleep(2)

#定义一个获取用户名提示信息的函数:
	def get_text_user(self):
		return self.findElement(*self.textUser).text
		time.sleep(2)

# 定义一个获取用户名提示信息的函数:
	def get_text_passwd(self):
		return self.findElement(*self.textPasswd).text
		time.sleep(2)

#定义一个登录风暴平台的函数
	def login_storm(self,username,password):
		self.input_account(user=username)
		self.input_passwd(code=password)
		self.click_login()
page_add_object_storm:增加产品对象层
from base.base import WebUI   #继承base中的元素定位的类:WebUI
from selenium.webdriver.common.by import By   #元素定位必须用到的类:By
import time   #关于时间的库
#定义一个添加产品的对象层的类
class Storm_add_object(WebUI):   #需要定位元素,所以要继承关于元素定位的类:WebUI
	#对象类的数据属性
	#定位产品管理的元素属性
	productManagement=(By.XPATH,"/html/body/div/div/section/header/div/ul/li[2]/a")
	#定位新增产品的元素属性
	addProduct=(By.XPATH,"/html/body/div/div/section/section/main/div/div[1]/button[2]/span")
	#定位产品名称输入框的元素属性
	productName=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[1]/div/div/input")
	#定位名称输入框的文本提示信息的元素属性
	textName=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[1]/div/div[2]")
	#定位产品类型的输入框的元素属性
	productType=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[2]/div/div/div[1]/input")
	#定位产品的类型的文本提示消息元素属性
	textType=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[2]/div/div[2]")
	#定位一个选择web类型的元素属性
	webProduct=(By.XPATH,"/html/body/div[3]/div[1]/div[1]/ul/li[1]")
	#定位一个选择app类型的元素属性
	appProduct = (By.XPATH, "/html/body/div[3]/div[1]/div[1]/ul/li[2]")
	#定位产品的版本输入框的元素属性
	productVersion=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[3]/div/div/input")
	#定位版本输入框的文本提示信息的元素属性
	textVersion=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[3]/div/div[2]")
	#定位产品负责人输入框的元素属性
	productMaster=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[4]/div/div/input")
	#定位负责人输入框的提示文本信息的元素属性
	textMaster=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[4]/div/div[2]")
	#定位产品描述输入框的元素属性
	productDescription=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[5]/div/div/textarea")
	#定位产品描述输入框的提示文本信息的元素属性
	textDescription=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[2]/form/div[5]/div/div[2]")
	#定位确定添加产品的元素属性
	enterSure=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[3]/div/div[3]/div/button[2]/span")
	# 定位搜索产品输入框的元素属性
	search = (By.XPATH, "/html/body/div/div/section/section/main/div/div[1]/div/form/div/div/div/input")
	# 定位一个搜索按钮的元素属性
	searchButton = (By.XPATH, "/html/body/div/div/section/section/main/div/div[1]/button[1]")

#定义一个点击产品管理的函数
	def product_manager(self):
		self.findElement(*self.productManagement).click()
		time.sleep(2)

#定义一个点击新增产品的函数
	def add_product(self):
		self.findElement(*self.addProduct).click()
		time.sleep(2)

#定义一个输入产品名称的函数
	def input_product_name(self,pName):
		self.findElement(*self.productName).send_keys(pName)
		time.sleep(2)

# 定义一个获取输入产品名称提示信息的函数
	def get_text_name(self):
		return self.findElement(*self.textName).text
		time.sleep(2)

#定义一个点击产品类型的函数
	def click_product_type(self):
		self.findElement(*self.productType).click()
		time.sleep(2)

# 定义一个获取输入产品类型提示信息的函数
	def get_text_type(self):
		return self.findElement(*self.textType).text
		time.sleep(2)

#定义一个选择web产品类型的函数
	def select_WEB(self):
		self.findElement(*self.webProduct).click()
		time.sleep(2)

#定义一个选择app产品类型的函数
	def select_App(self):
		self.findElement(*self.appProduct).click()
		time.sleep(2)

#定义一个输入版本号的函数
	def input_version(self,pVersion):
		self.findElement(*self.productVersion).send_keys(pVersion)
		time.sleep(2)

	# 定义一个获取输入产品版本号提示信息的函数
	def get_text_version(self):
		return self.findElement(*self.textVersion).text
		time.sleep(2)

#定义一个输入产品负责人的函数
	def input_master(self,pMaster):
		self.findElement(*self.productMaster).send_keys(pMaster)
		time.sleep(2)

# 定义一个获取输入产品负责人提示信息的函数
	def get_text_master(self):
		return self.findElement(*self.textMaster).text
		time.sleep(2)

#定义一个输入产品描述的函数
	def input_description(self,pDescription):
		self.findElement(*self.productDescription).send_keys(pDescription)
		time.sleep(2)

# 定义一个获取输入产品描述提示信息的函数
	def get_text_description(self):
		return self.findElement(*self.textDescription).text
		time.sleep(2)

#定义一个点击确定新增产品的函数
	def enter_sure(self):
		self.findElement(*self.enterSure).click()
		time.sleep(2)

#定义一个新增WEB产品的函数
	def add_WEB_product(self,name,version,master,descriptions):
		self.add_product()
		self.input_product_name(pName=name)
		self.click_product_type()
		self.select_WEB()
		self.input_version(pVersion=version)
		self.input_master(pMaster=master)
		self.input_description(pDescription=descriptions)
		self.enter_sure()

#定义一个新增APP产品的函数
	def add_APP_product(self,name,version,master,descriptions):
		self.add_product()
		self.input_product_name(pName=name)
		self.click_product_type()
		self.select_App()
		self.input_version(pVersion=version)
		self.input_master(pMaster=master)
		self.input_description(pDescription=descriptions)
		self.enter_sure()

#定义一个搜索产品的函数
	def search_product(self,keyword):
		self.findElement(*self.search).send_keys(keyword)
		time.sleep(2)
		self.findElement(*self.searchButton).click()
		time.sleep(2)

#定位产品列表中的产品负责人元素属性
	listProductMaster=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[4]/div")
#定义一个获取列表中的产品负责人的函数,在这里主要是用来产品添加成功后做断言。
	def get_list_product_master(self):
		return self.findElement(*self.listProductMaster).text
		time.sleep(2)
page_search:搜索产品对象层
from selenium.webdriver.common.by import By   #元素定位必须用到的类
import time   #关于时间的库
'''这里由于要使用到增加产品和修改产品的对象层中的一些方法和数据属性,所以需要导入修改产品对象层的类,这时因为修改对象层已经继承了
增加产品的对象层的类,而增加产品的对象曾的类已经继承base层中关于元素定位的类:WebUI,所以这里直接继承修改产品对象层的类即可。'''
from page.page_revise_object_storm import Storm_revise_object
#定义一个搜索产品的对象层的类。
class Search_product(Storm_revise_object):
	#定位搜索不到时的提示信息
	notSearched=(By.XPATH,"/html/body/div/div/section/section/main/div/div[2]/div[3]/div/span")

#定位搜索不到获取提示信息的函数:
	def get_not_search(self):
		return self.findElement(*self.notSearched).text
		time.sleep(2)
page_revise_object_storm:修改产品对象层
from selenium.webdriver.common.by import By   #元素定位必须用到的类
import time
# 这里由于要使用到增加产品的对象层中的一些方法和数据属性,所以需要导入增加产品对象层的类,该类已经继承base层中关于元素定位的类:WebUI,所以这里直接继承增加产品对象层的类即可。
from page.page_add_object_storm import Storm_add_object
#定义一个修改产品的对象层的类
class Storm_revise_object(Storm_add_object):
	#定位修改按钮的元素属性
	reviseButton=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[8]/div/button[1]")
	#定位产品列表中产品名称的元素属性
	listProductName=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[1]/div/a")
	# 定位产品列表中产品类型的元素属性
	listProductType = (By.XPATH, "/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[2]/div")
	# 定位产品列表中产品版本的元素属性
	listProductVersion = (By.XPATH, "/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[3]")
	# 定位产品列表中产品描述的元素属性
	listProductDescription = (By.XPATH, "/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[5]/div")

#定义一个点击修改按钮的函数
	def click_revise(self):
		self.findElement(*self.reviseButton).click()
		time.sleep(2)

#定义一个获取列表中的产品名称的函数
	def get_list_product_name(self):
		return self.findElement(*self.listProductName).text
		time.sleep(2)

#定义一个获取列表中的产品类型的函数
	def get_list_product_type(self):
		return self.findElement(*self.listProductType).text
		time.sleep(2)

#定义一个获取列表中的产品版本的函数
	def get_list_product_version(self):
		return self.findElement(*self.listProductVersion).text
		time.sleep(2)

#定义一个获取列表中的产品描述的函数
	def get_list_product_description(self):
		return self.findElement(*self.listProductDescription).text
		time.sleep(2)
page_delete:删除产品对象层
from selenium.webdriver.common.by import By   #元素定位必须用到的类
import time  #关于时间的库
'''这里由于要使用到搜索产品的对象层中的一些方法和数据属性,所以需要导入搜索产品对象层的类,这时因为搜索对象层已经继承了
修改产品的对象层的类,而修改产品的对象层的类已经继承增加产品的对象曾的类,增加产品的对象层的类已经继承了base层中关于元素定位的类:WebUI,所以这里直接继承搜索产品对象层的类即可。'''
from page.page_search import Search_product
class Delete(Search_product):
	#定位删除按钮
	deleteButton=(By.XPATH,"/html/body/div[1]/div/section/section/main/div/div[2]/div[3]/table/tbody/tr/td[8]/div/button[2]")
	#定位确认删除的按钮
	sureDelete=(By.CSS_SELECTOR,"body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary")
	#定义一个删除的函数
	def delete_product(self):
		self.search_product(keyword="yl")
		time.sleep(3)
		self.findElement(*self.deleteButton).click()
		time.sleep(3)
		self.findElement(*self.sureDelete).click()
		time.sleep(3)

1.5test:测试层

test_a_storm_login:登录的测试层
from page.page_storm_login import Login    #登录的对象层
from page.page_firmware_storm import LoginFirmware   #测试固件对象层
from utils.operation import readJson   #处理文件的函数
import time   #关于时间的类
#定义一个登录的测试类
class storm_login_platform(LoginFirmware,Login):   #这里的继承顺序一定要先继承测试固件,在继承登陆的对象层,多类继承从左到右继承。

	'''测试登录场景'''

	def test_login(self):
		'''用户名和密码正确'''
		self.login_storm(username="13762237226",password="123456")
		time.sleep(3)
		self.assertTrue(self.driver.current_url.endswith("101.43.158.84/#/"))

	def test_account_empty(self):
		'''用户名输入为空'''
		self.input_account(user="")
		self.click_login()
		self.assertEqual(self.get_text_user(),readJson()["login"][0]["accountTextInformation"][0])

	def test_account_format_error(self):
		'''用户名输入格式不对'''
		self.input_account(user="123456")
		self.click_login()
		self.assertEqual(self.get_text_user(),readJson()["login"][0]["accountTextInformation"][1])

	def test_passwd_empty(self):
		'''密码输入为空'''
		self.input_passwd(code="")
		self.click_login()
		self.assertEqual(self.get_text_passwd(),readJson()["login"][1]["passwdTextInformation"][0])

	def test_passwd_format_error(self):
		'''密码格式有误'''
		self.input_passwd(code="123")
		self.click_login()
		self.assertEqual(self.get_text_passwd(),readJson()["login"][1]["passwdTextInformation"][1])
test_b_storm_add_product:增加产品的测试层
from page.page_add_object_storm import Storm_add_object  #增加产品的对象层
from page.page_storm_login import Login   #登陆的对象曾,这是因为添加产品必须先登录
from page.page_firmware_storm import LoginFirmware   #测试固件对象层
from utils.operation import readJson    #处理文件的函数
import time   #关于时间的库

'''定义一个添加产品的测试类'''
class Storm_add_product(LoginFirmware,Login,Storm_add_object):   #这里的继承顺序一定要先继承测试固件,在继承登陆的对象层,再继承增加产品的对象层,多类继承从左到右继承。

	'''添加的场景'''
	def test_add(self):
		'''完整的输入添加WEB产品输入框'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_WEB_product(name="qy",version="1.3.2",master="qy",descriptions="测试")
		time.sleep(2)
		self.search_product(keyword="qy")
		self.assertEqual(self.get_list_product_master().replace(" ",""),"qy")

	def test_name_empty(self):
		'''产品名称为空'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_product()
		self.input_product_name(pName="")
		self.enter_sure()
		self.assertEqual(self.get_text_name(),readJson()["Add"][0]["Pname"])

	def test_Type_empty(self):
		'''产品类型为空'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_product()
		self.click_product_type()
		self.enter_sure()
		self.assertEqual(self.get_text_type(),readJson()["Add"][0]["Ptype"])

	def test_version_empty(self):
		'''产品版本为空'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_product()
		self.input_version(pVersion="")
		self.enter_sure()
		self.assertEqual(self.get_text_version(),readJson()["Add"][0]["Pversion"])

	def test_master_empty(self):
		'''产品负责人为空'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_product()
		self.input_master(pMaster="")
		self.enter_sure()
		self.assertEqual(self.get_text_master(),readJson()["Add"][0]["Pmaster"])

	def test_description_empty(self):
		'''产品描述为空'''
		self.login_storm(username="13762237226",password="123456")
		self.product_manager()
		self.add_product()
		self.input_description(pDescription="")
		self.enter_sure()
		self.assertEqual(self.get_text_description(),readJson()["Add"][0]["Pdescription"])
test_c_storm_revise_product:修改产品测试层

from page.page_storm_login import Login   #登陆的对象层
from page.page_firmware_storm import LoginFirmware   #测试固件对象层
from utils.operation import readJson   #读取文件的函数
import time  #关于时间的库
from page.page_revise_object_storm import Storm_revise_object #修改产品的对象层
class Revise(LoginFirmware,Login,Storm_revise_object):   #这里的继承顺序一定要先继承测试固件,在继承登陆的对象层,再继承增加产品的对象层,多类继承从左到右继承。

	'''场景:修改'''
	def test_a_revise_name(self):
		'''修改产品名称'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="qy")
		self.click_revise()
		self.findElement(*self.productName).clear()
		self.input_product_name(pName="yl")
		self.enter_sure()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_name().replace(" ",""),"yl")

	def test_revise_version(self):
		'''修改产品版本'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="yl")
		self.click_revise()
		self.findElement(*self.productVersion).clear()
		self.input_version(pVersion="1.3.4")
		self.enter_sure()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_version().replace(" ",""),"1.3.4")

	def test_revise_master(self):
		'''修改产品负责人'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="yl")
		self.click_revise()
		self.findElement(*self.productMaster).clear()
		self.input_master(pMaster="yl")
		self.enter_sure()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_master().replace(" ",""),"yl")

	def test_revise_description(self):
		'''修改产品描述'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="yl")
		self.click_revise()
		self.findElement(*self.productDescription).clear()
		self.input_description(pDescription="这是个测试案例")
		self.enter_sure()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_description().replace(" ",""),"这是个测试案例")

	def test_revise_type(self):
		'''修改产品类型'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="yl")
		self.click_revise()
		self.click_product_type()
		self.select_App()
		self.enter_sure()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_type().replace(" ",""),"APP")
test_d_storm_search:搜索产品测试层
from page.page_storm_login import Login  #登录对象层
from page.page_firmware_storm import LoginFirmware   #测试固件对象曾
from utils.operation import readJson  #读取文件的函数
import time  #关于时间的库
from page.page_search import Search_product  #搜索产品的对象曾
class Revise(LoginFirmware,Login,Search_product):
	'''搜索场景'''
	def test_search_ok(self):
		'''可以搜索得到'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_list_product_name().replace(" ", ""), "yl")

	def test_search_not(self):
		'''搜索不到'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="name")
		self.assertEqual(self.get_not_search(),"暂无数据")

	def test_search_empty(self):
		'''默认搜索'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.search_product(keyword="")
		self.assertEqual(self.get_list_product_name().replace(" ", ""), "中国")
test_e_delete:删除产品测试层

from page.page_storm_login import Login   #登录的对象层
from page.page_firmware_storm import LoginFirmware   #测试固件对象层
from utils.operation import readJson    #读取文件的函数
import time  #关于时间的库
from page.page_delete import Delete  #删除的对象层
class Delete_a_Product(LoginFirmware,Login,Delete):
	'''删除产品'''
	def test_delete(self):
		'''删除产品并验证产品删除成功'''
		self.login_storm(username="13762237226", password="123456")
		self.product_manager()
		self.delete_product()
		self.findElement(*self.search).clear()
		self.search_product(keyword="yl")
		self.assertEqual(self.get_not_search(), "暂无数据")

1.6utils:工具层

import json  #读取json文件必须要用到的库
from common.public import base_dir   #导入公共层关于路径的函数
import os  #处理路径的库
def readJson():
	return json.load(open(file=os.path.join(base_dir(),"data","storm.json"),encoding="utf-8"))

1.7run:运行层

import HTMLTestRunner   #生成测试报告必须要用到的库
import unittest   
import os
import time

# ##加载所有的测试模块来执行:unittest库下的TextTestRunner()类中的run方法。
# def getSuite():
# 	#start_dir=加载所有的测试模块来执行,pattern通过正则的方式获取到要执行的模块
# 	pathTest = os.path.dirname(os.path.dirname(__file__))    #获取当前路径的上一级目录
# 	#获取所有的测试模块:unittest库下的TestLoader()类中的discover的方法。
# 	suite = unittest.TestLoader().discover(start_dir=(os.path.join(pathTest, "test")), pattern="test_*.py")
# 	unittest.TextTestRunner().run(suite)
#
# #加载所有的测试模块并返回,调用函数执行:unittest库下的TextTestRunner()类中的run方法。
# def getSuite():
# 	#start_dir=加载所有的测试模块来执行,pattern通过正则的方式获取到要执行的模块
# 	pathTest = os.path.dirname(os.path.dirname(__file__))    #获取当前路径的上一级目录
# 	#获取所有的测试模块:unittest库下的TestLoader()类中的discover的方法。
# 	suite = unittest.TestLoader().discover(start_dir=(os.path.join(pathTest, "test")), pattern="test_*.py")
# 	return suite
# unittest.TextTestRunner().run(getSuite())


#生成自动化测试报告:

#加载所有的测试模块并返回:unittest库下的TextTestRunner()类中的run方法。
def getSuite():
	#start_dir=加载所有的测试模块来执行,pattern通过正则的方式获取到要执行的模块
	pathTest = os.path.dirname(os.path.dirname(__file__))    #获取当前路径的上一级目录
	#获取所有的测试模块:unittest库下的TestLoader()类中的discover的方法。
	suite = unittest.TestLoader().discover(start_dir=(os.path.join(pathTest, "test")), pattern="test_*.py")
	return suite

#获取当前时间,由于python格式的限制,当前时间最好采用下面的方式:
def getNowTime():
	return time.strftime("%y-%m-%d %H_%M_%S",time.localtime(time.time()))

#执行获取的测试模块,并获取测试报告
def stormReport():
	#获取当前路径的上一级目录
	reportPath=os.path.dirname(os.path.dirname(__file__))
#定义文件名和文件储存的路径(reportPath,"report"),定义文件名为当前时间+report.html,主要是为为了区分不同时间段生成的测试报告。
	filename=os.path.join(reportPath,"report",getNowTime()+"report.html")
	time.sleep(3)
#把测试报告写入文件中,b是以二进制的方式写入
	fp=open(filename,"wb")
	time.sleep(3)
	#HTMLTestRunner实例化的过程,stream是流式写入。
	runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title="风暴平台",description="风暴平台自动化测试")
	time.sleep(3)
	#运行所有测试套件:HTMLTestRunner里的run方法。
	runner.run(getSuite())
	time.sleep(3)

#主函数:执行的入口
if __name__ == '__main__':
	stormReport()

1.8report:测试报告层

posted @ 2022-04-16 14:58  柒の夜  阅读(219)  评论(0编辑  收藏  举报