import pytest
from data import Data
from checkdata import CheckData
class TestCase:
@pytest.mark.parametrize('data',CheckData(Data.order_data_model, Data.order_data_define).get_null_data_list())
def test_case(self,data):
# res = create_order(data)
# assert res.text['success'] == False
assert data is None
if __name__ == '__main__':
pytest.main()
#输入-输出
a = input('输出dotcpp的网站:')
b = float(input('我认为适宜的温度:'))
index = list(map(int,input().split()))
#这种方式可以输入任意个int型的数字,在这里采用列表来存储。输入时空格分开
print(index)
print(index[0])
#数值类型
int float bool
c = 9
d =9.9
e = True
数值运算符
+ - * / //取整 %取余
比较运算符
> < >= <= == != is is not
转义符 \
print("children\'s class") #children's class
制表符 \t
[:0] [0:1] [0:5:2]
#索引-找单个[:3]-第4个-d
#结果=数据[索引值]
s1 = ‘abcde’
res = s1[2] #正向c-从0开始
res = s1[-1] #反向e-从-1开始
循环打印单个字符串
s1 = 'abcde'
for i in range(len(s1)):
print(s1[i])
i = i+1
#切片-找多个[1:3]-取头不取尾-取第2、3位-bc
#结果=数据[start:end]
#切片2-跨步取值-[0:5:2] -ab cd e -跨两步,得ace
列表:[]
元组:() 不可修改
八、替换-replace
s ='123rrr123ttt123'
res=s.replace('123','999',2)
#参数1被参数2替换,替换参数3个,无参数3时全替换
#只能替换字符串型,不能替换数值型
九、查找-find-返回第一个位置索引
s ='123rrr123ttt123'
res=s.find('23')
res=s.find('23',4,10) #从第五开始找
set={11,22,33}
set = set() #定义空集合
集合内没有重复元素
去除列表内重复元素:list-set-list\
li = [1,22,22,33]
a = list(set(li))
十七、随机数
import random
res = random.randint(1,999) 随机整数
res = random.random() 随机小数,0-1之间
res = random.uniform(10,20) 10-20之间的小数
li = [1,33,55]
res = random.choice(li) 在列表中挑选一个随机值
十八、条件判断
if elif else
逻辑运算:and or not
成员运算:in ,not in
身份运算:is ,is not 判断内存id
赋值运算:= ,+= ,-= ,*= ,/= ,%=
十九、循环
while True:break continue
for i in [1,2,3]/dic/dic.value/dic.items():
元组拆分-遍历键和值:
for k,v in dic.items():
列表循环
li2 = ['www{}'.format(i) for i in range(10)]
li = [ i+1 for i in (11,22,33)]
二十、range内置函数
生成指定序列
li = list(range(100))
li = list(range(9,99,2))
# excel的使用
# Excel使用xlsx格式,解析成三个对象,工作簿workbook、表单Sheet、单元格Cell
import openpyxl
workbook = openpyxl.load_workbook('test1.xlsx')
print(workbook.sheetnames)
sh = workbook['Sheet1']
print(sh.cell(row=3,column=3).value)
#按行读取 rows 按列读取 column
res = list(sh.rows)
for i in res:
print(i,i.value)
# 转为字典格式
res = list(sh.rows)
title = [i.value for i in res[0]]
cases = []
for item in res[1:]:
data = [i.value for i in item]
dic = dict(zip(title,data))
print(dic)
cases.append(dic)
@ddt
class TestMusen(unittset.TestCase)
@list_data(cases)
def test(self, item):
expected = eval(item['espected'])
params = eval(item['data'])
res = func(**params)
self.assertEqual(expectet,res)
# 结果
{'case':'1','data':'2','expected:'3'}
{'case':'1','data':'2','expected:'3'}
# 封装Excel
import openpyxl
class HandleExcel:
def __init__(self,filename,sheetname):
self.filename = filename
self.sheetname = sheetname
self.workbook = openpyxl.load_workbook(filename)
self.sheet = self.workbook[sheetname]
def read_data(self):
res = list(self.sheet.rows)
title = [i.value for i in res[0]]
cases = []
for item in res[1:]:
data = [i.value for i in item]
dic = dict(zip(title, data))
cases.append(dic)
return cases
def write_data(self,row_no:int,colunm_no:int,value):
self.sheet.cell(row=row_no,column=colunm_no,value=value)
self.workbook.save(filename=self.filename)
@ddt
class TestMusen(unittest.TestCase):
excel = HandleExcel('仓库作业-入库.xlsx', 'Sheet1')
cases = excel.read_data()
@list_data(cases)
def test(self, item):
expected = eval(item['expected'])
params = eval(item['params'])
res = func(**params)
self.assertEqual(True, res)
readyaml.py
import yaml
with open('yaml.yaml','r',encoding='utf-8') as f:
res =yaml.load(f,Loader=yaml.Loader)
print(res,type(res))
print(res['case']['data'])