[pytest]
#生成allure报告
addopts= -vs --alluredir=report --clean-alluredir
#指定路径
testpaths= ./testcase
#指定文件
python_files= test_*.py
#指定class
python_classes=Test*
#指定方法
python_functions=test_*
#指明要执行的case
markers=
login
order
smoke
addopts详解:
- -s 详细输出
- -v 输出具体运行的case
--alluredir= report 生成临时报告
--clean-alluredir 清空临时报告
#标记marker
@pytest.mark.login
#执行顺序
@pytest.mark.run(order=1)
def test_login(self,browser_fixture):
conftest.py:管理fixture,一个文件夹下只能有一个conftest.py
@pytest.fixture(scope='class',params=[url])
def browser_fixture(request):
driver = webdriver.Chrome()
driver.get(request.param)
print('打开浏览器'+request.param)
yield driver
driver.quit()
print('关闭浏览器')
常用读取文件方法
readexcel.py
from openpyxl import Workbook, load_workbook
class ReadExcel():
# 定义方法:读取指定目录下Excel文件某个sheet单元格的值
def read_excel(filepath,index,x,y):
#wb=Workbook()
wb=load_workbook(filepath)
sheetname=wb.sheetnames[index]
sheet=wb[sheetname]
return sheet.cell(x,y).value
def write_excel(filepath,index,x,y,newstr):
wb = load_workbook(filepath)
sheetname = wb.sheetnames[index]
sheet = wb[sheetname]
sheet.cell(x,y).value=newstr
wb.save(filepath)
readfile.py
class ReadFile:
def get_txtfile(self,filename):
# 获取登录参数
with open(filename, 'r', encoding='utf-8') as loginfile:
login_dict = {}
for loginline in loginfile.readlines():
loginline = loginline.strip()
k = loginline.split(' ')[0]
v = loginline.split(' ')[1]
login_dict[k] = v
loginfile.close()
return login_dict
def get_txtfile1(filename):
# 获取登录参数
with open(filename, 'r', encoding='utf-8') as loginfile:
login_dict = {}
for loginline in loginfile.readlines():
loginline = loginline.strip()
k = loginline.split(' ')[0]
v = loginline.split(' ')[1]
login_dict[k] = v
loginfile.close()
return login_dict
def get_jsonfile(self,filename):
with open(filename,'r',newline='') as file:
lcookie=json.load(file)
file.close()
return lcookie
def get_jsonfile1(filename):
with open(filename,'r',newline='') as file:
lcookie=json.load(file)
file.close()
return lcookie
readyml
def read_yaml():
fileNamePath = os.path.split(os.path.realpath(__file__))[0]
yamlpath = os.path.join(fileNamePath, 'nfig.yaml')
with open(yamlpath,'r',encoding='utf-8') as ymalfile:
conf=ymalfile.read()
mysqlyaml=yaml.load(conf)
#print(mysqlyaml)
return mysqlyaml