Python interface test course (first day) -Python basis

table of Contents

Python interface test course (first day) -Python basis
Python interface test course (the next day) - fast interface testing practice
Python interface testing program (Day) - Interface safety validation, parameterization and assert
Python interface testing program (first four days) - Interface testing framework to achieve

For more information, please add add learning of micro letter: lockingfree get

The first day: Python basis

Outline

Introduction to Python, build environment and package management

Introduction to Python

  1. Features: Python is a dynamic, interpreted, strongly typed language
    1. News: During operation before doing data check (without declaring variables in advance) - Static Voice (C / Java): compile-time checking of data types (type when you declare a variable coding)
    2. Interpreted: in the implementation of the program, only a section of the interpreted into machine language the computer to execute (without compilation, slower) - compiled languages ​​(C / Java): must first compile the code into a binary executable file and then
    3. Strong type: The type of security, once the variable is assigned a data type, if not coerced, it will always be this type (strict, to avoid the type of error, slower) - weakly typed (VBScript / JavaScript): type will be during operation conversion, as in the 1 + js "2" = "12", a string will be converted to digital
  2. Coding principle: elegant, clear and simple
  3. advantage
    1. Easy to learn
    2. Development of high efficiency
    3. High-level language
    4. Portable, scalable, embeddable
    5. Tripartite huge library
  4. Shortcoming
    1. Slow
    2. Code can not be encrypted
    3. Multithreading can not take full advantage of multi-core cpu (GIL Global explanatory lock, can only run one thread at a time)
  5. Applications
    1. Automated testing (UI / Interface)
    2. Automated operation and maintenance
    3. reptile
    4. Web Development (Django / Flask / ..)
    5. Graphics GUI development
    6. Game Script
    7. Financial, quantitative trading
    8. Data analysis, large data
    9. Artificial intelligence, machine learning, NLP, Computer Vision
    10. cloud computing

Environment to build

Windows Python3 environment to build

  1. Download Python3.6.5 * .exe installation package
  2. 双击安装,第一个节目选中Add Python3.6.5 to PATH,点击Install Now(默认安装pip),一路下一步
  3. 验证:打开cmd命令行,输入python,应能进入python shell 并显示为Python 3.6.5版本

包管理

  1. pip安装
    1. pip install 包名 - 卸载: pip uninstall 包名
    2. pip install 下载的whl包.whl
    3. pip install -r requiements.txt(安装requirements.txt中的所有依赖包)
    4. pip list 查看已安装的三方包,pip freeze 已文件格式显示已安装的三方包(用于导出requiremnts.txt文件)
  2. 源码安装
    1. 下载源码包,解压,进入解压目录
    2. 打开命令行,执行 python setup.py install
    3. 验证:进入python shell,输入import 包名,不报错表示安装成功
  3. 三方包默认安装路径:Python3.6.5/Lib/site-packages/ 下

Python基本语法

  1. 缩进
if x > 0:
    print("正数")
elif x = 0:
    print("0")
else:
    print("负数")

def add(x,y):
    return x+y
  1. 一行多条语句
x=1; y=2; print(x+y)
  1. 断行
print("this line is too long, \ 
so I break it to two lines")
  1. 注释
# 单行注释
a = 1

'''这是一段
多行注释'''

def add(x, y):
    """加法函数:这是docstring(函数说明)"""
    pass
  1. 变量
    1. 变量类型(局部变量、全局变量、系统变量)
    2. 变量赋值
      • 多重赋值x=y=z=1
      • 多元赋值x,y = y,x
    3. 变量自增 x+=1 x-=1(不支持x++, x--)

Python3中没有常量

基本数据类型(6种)

1. 数字Number

  1. 种类
    1. 整型int(Python3中没有长整型,int长度几乎没有限制)
    2. 浮点型float
    3. 布尔型bool
      • False: 0,0.0,'',[],(),{}
      • True: 除False以外,['']或[[],[]]不是False
    4. 复数型complex
  2. 操作符: +,-,*,/,//(地板除),**(乘方) - Python3中的/是真实除,1/2=0.5
  3. 类型转
    1. str(): 其他类型转为字符串, 如str(12)
    2. int(): 字符串数字转为整型(字符串不是纯整数会报错), 如int("12")
    3. float(): 字符串转换为浮点数,如float("1.23")

2. 字符串String

  1. 字符串系统方法
    • len(): 计算字符串长度,如len("abcdefg")
    • find()/index(): 查找字符串中某个字符第一次出现的索引(index()方法查找不到会报错), 如"abcdefg".find("b"); "abcedfgg".index("g")
    • lower()/upper(): 将字符串转换为全小写/大写,如"AbcdeF".lower();"abcedF".upper()
    • isdigit()/isalpha()/isalnum(): 判断字符串是否纯数字/纯字母/纯数字字母组合, 如isdigit("123"),结果为 True
    • count(): 查询字符串中某个元素的数量,如"aabcabc".count("a")
    • join(): 将列表元素按字符串连接,如"".join(["a","b","c"])会按空字符连接列表元素,得到"abc"
    • replace(): 替换字符串中的某已部分,如"hello,java".replace("java", "python"),将java 替换为 python
    • split(): 和join相反,将字符串按分隔符分割成列表, 如"a,b,c,d".split(",")得到["a", "b", "c", "d"]
    • strip()/lstrip()/rstrip(): 去掉字符串左右/左边/右边的无意字符(包括空格,换行等非显示字符),如" this has blanks \n".strip()得到"this has balnks"
  2. 字符串格式化
    • %: 如"Name: %s, Age: %d" % ("Lily", 12)"Name: %(name)s, Age: %(age)d" % {"name": "Lily", "age": 12}
    • format: 如"Name: {}, Age: {}".format("Lily", 12)"Name: {name}, Age: {age}".format(name="Lily",age=12)
    • substitude(不完全替换会报错)/safe_substitude: 如"Name: ${name}, Age: ${age}".safe_substitude(name="Lily",age=12)
  3. 案例: 利用format生成自定义html报告

    tpl='''
    {title}

    {title}









    {trs}
    序号 用例 结果



    '''

    tr='''{sn}
    {case_name}
    {result}
    '''

    title="自动化测试报告"
    case_results = [("1", "test_add_normal", "PASS"),("2", "test_add_negative", "PASS"), ("3", "test_add_float", "FAIL")]

    trs=''
    for case_result in case_results:
    tr_format = tr.format(sn=case_result[0], case_name=case_result[1], result=case_result[2])
    trs += tr_format

    html = tpl.format(title=title, trs=trs)

    f = open("report.html", "w")
    f.write(html)
    f.close()
    结果预览

    自动化测试报告

序号 用例 结果
1 test_add_normal PASS
2 test_add_negative PASS
3 test_add_float FAIL

3. 列表List

列表元素支持各种对象的混合,支持嵌套各种对象,如["a", 1, {"b": 3}, [1,2,3]]

  1. 列表操作
    • 赋值: l = [1, "hello", ("a", "b")]
    • 获取: a = l[0] # 通过索引获取
    • 增: l.append("c");l.extend(["d","e"]);l+["f"]
    • 删: l.pop() # 按索引删除,无参数默认删除最后一个;l.remove("c") # 按元素删除
    • 改:l[1]="HELLO" # 通过索引修改
    • 查: 遍历 for i in l: print(i)
  2. 列表系统方法
    • append()/insert()/extend(): 添加/插入/扩展(连接)
    • index(): 获取元素索引
    • count(): 统计元素个数
    • pop()/remove(): 按索引/元素删除
    • sort()/reverse(): 排序/反转
    • 案例: 字符串反转s="abcdefg"; r=''.join(reversed(a))

4. 元组Tuple

  1. 不可改变,常用作函数参数(安全性好)
  2. 同样支持混合元素以及嵌套
  3. 只有一个元素时,必须加","号,如a=("hello",) - 因为Python中()还有分组的含义,不加","会识别为字符串

字符串/列表/元组统称为序列, 有相似的结构和操作方法

序列相关操作方法

  1. 索引
    • 正反索引: l[3];l[-1]
    • 索引溢出(IndexError): 当索引大于序列的最大索引时会报错,如[1,2,3,4]最大索引是3,引用l[4]会报IndexError
  2. 切片
    • l[1:3] # 从列表索引1到索引3(不包含索引3)进行截取, 如 l = [1, 2, 3, 4, 5], l[1:3]为[2, 3]
    • l[:5:2] # 第一个表示开始索引(留空0), 第二个表示结束索引(留空为最后一个,即-1), 第三个是步长, 即从开头到第5个(不包含第5个),跳一个取一个
    • 案例: 字符串反转 s="abcdefg";r=s[::-1]
  3. 遍历
    • 按元素遍历: for item in l: print(item)
    • 按索引遍历: for index in range(len(l)): print(l[index])
    • 按枚举遍历: for i,v in enumerate(l): print((i,v))
  4. 扩展/连接(添加多个元素): extend()/+ "abc"+"123";[1,2,3]+[4,5];[1,2,3].extend([4,5,6,7])
  5. 类型互转: str()/list()/tuple()

    list转str一般用join(), str转list一般用split()

  6. 系统函数
    • len(): 计算长度
    • max()/min(): 求最大/最小元素
    • sorted()/reversed(): 排序/反转并生成新序列(sort()/reverse()直接操作原序列)l_new=sorted(l);l_new2=reversed(l)

5. 集合Set

  1. 集合可以通过序列生成a = set([1,2,3])
  2. 集合无序,元素不重复(所有元素为可哈希元素)
  3. 集合分为可变集合set和不可变集合frozenset
  4. 操作方法: 联合|,交集&,差集-,对称差分^
  5. 系统函数: add()/update()/remove()/discard()/pop()/clear()
  6. 案例1: 列表去重: l=[1,2,3,1,4,3,2,5,6,2];l=list(set(l)) (由于集合无序,无法保持原有顺序)
  7. 案例2: 100w条数据,用列表和集合哪个性能更好? - 集合性能要远远优于列表, 集合是基于哈希的, 无论有多少元素,查找元素永远只需要一步操作, 而列表长度多次就可能需要操作多少次(比如元素在列表最后一个位置)

6. 字典Dict

  1. 字典是由若干key-value对组成, 字典是无序的, 字典的key不能重复,而且必须是可哈希的,通常是字符串
  2. 字典操作
    • 赋值: d = {"a":1, "b":2}
    • 获取: a = d['a']a = d.get("a") # d中不存在"a"元素时不会报错
    • 增: d["c"] = 3; d.update({"d":5, "e": 6}
    • 删: d.pop("d");d.clear() # 清空
    • 查: d.has_key("c")
    • 遍历:
      • 遍历key: for key in d:for key in d.keys():
      • 遍历value: for value in d.values():
      • 遍历key-value对: for item in d.items():
  3. 案例: 更新接口参数 api = {"url": "/api/user/login": data: {"username": "张三", "password": "123456"}},将username修改为"李四"
    api['data']['username'] = "李四"api['data'].update({"username": "李四"})

    哈希与可哈希元素

  4. 哈希是通过计算得到元素的存储地址(映射), 这就要求不同长度的元素都能计算出地址,相同元素每次计算出的地址都一样, 不同元素计算的地址必须唯一, 基于哈希的查找永远只需要一步操作, 计算一下得到元素相应的地址, 不需要向序列那样遍历, 所以性能较好
  5. 可哈希元素: 为了保证每次计算出的地址相同, 要求元素长度是固定的, 如数字/字符串/只包含数字,字符串的元组, 这些都是可哈希元素

6种类型简单的特点总结

  1. 数字/字符串/元祖: 长度固定
  2. 序列(字符串/列表/元祖): 有序
  3. 集合/字典: 无序, 不重复/键值不重复

条件/循环

条件判断

  1. 示例:
if x>0:
    print("正数")
elif x=0:
    print("0")
else: 
    print("负数")
  1. 三元表达式: max = a if a > b else b
  2. 案例: 判断一个字符串是不ip地址

    ip_str = '192.168.100.3'
    ip_list = ip_str.split(".") # 将字符串按点分割成列表
    is_ip = True # 先假设ip合法
    if len(ip_list) != 4:
    is_ip= False
    else:
    for num in ip_list:
    if not isdigit(num) or not 0 <= int(num) <= 255:
    is_ip = False
    if is_ip:
    print("是ip")
    else:
    print("不是ip")

    使用map函数的实现方法(参考):

    def check_ipv4(str):
    ip = str.strip().split(".")
    return False if len(ip) != 4 or False in map(lambda x:True if x.isdigit() and 0<= int(x) <= 255 else False, ip) else True

    循环

  3. for in 循环
  4. while 循环

文件读写(文本文件)

html/xml/config/csv也可以按文本文件处理

文件操作方法

  1. open(): 打开f =open("test.txt")f =open("test.txt","r", encoding="utf-8")with open("test.txt) as f: # 上下文模式,出结构体自动关闭文件
  2. read()/readline()/readlines(): 读取所有内容/读取一行/读取所有行(返回列表) - 注意: 内容中包含\n换行符,可以通过strip()去掉
  3. f.write()/f.save(): 写文件/保存文件
  4. f.seek(): 移动文件指针,如f.seek(0), 移动到文件开头
  5. f.close(): 关闭文件(打开文件要记得关闭)

文件打开模式

  1. r/w/a: 只读/只写/追加模式
  2. rb/wb/ab: 二进制只读/只写/追加模式(支持图片等二进制文件)
  3. r+/rb+, w+/wb+, a+/ab+: 读写,区别在于, r+/w+会清空文件再写内容, r+文件不存在会报错, a+不清空原文件,进行追加, w+/a+文件不存在时会新建文件

    文件是可迭代的,可以直接用 for line in f: 遍历

函数/类

函数定义和调用

def add(x, y): # 定义函数
    return x+y

print(add(1,3)) # 调用函数

案例: 用户注册/登录函数

users = {"张三": "123456"}

def reg(username, password):
    if users.get(username): # 如果用户中存在username这个key
        print("用户已存在")
    else:
        users[username] = password # 向users字典中添加元素
        print("添加成功")

def login(username, password)
    if not users.get(username):
        print("用户不存在")
    elif users['username'] == password:
        print("登录成功")
    else:
        print("密码错误")

参数和返回值

  1. 函数没有return默认返回None
  2. 参数支持各种对象,包含数字,支付串,列表,元组,也可以是函数和类
  3. 参数默认值: 有默认值的参数必须放在最后面, 如```def add(x, y=1, z=2):
  4. 不定参数: *args和**kwargs, 如def func(*args, **kwargs):可以接受任意长度和格式的参数
  5. 参数及返回值类型注释(Python3)
def(x:int, y:int) -> int: # x,y为int型,函数返回为int型,只是注释,参数格式非法不会报错
    return x+y

函数作为参数(如: 装饰器)

def a():
    print("I'm a")
def deco(func):
    print("call from deco")
    func()

deco(a) # 输出"call from deco"并调用a(),输出"I'm a"

函数嵌套(支持闭包)

def a():
    a_var = 1
    def b:() # 嵌套函数
        a_var += 1

函数递归(自己调用自己,直到满足需求)

案例: 求N!

def factorial(n):
    return 1 if n == 0 or n == 1 else n * factorial(n-1)

模块/包

模块

  1. 一个py文件为一个模块
  2. 模块导入
    • import os # 需要通过os调用相关方法, 如os.mkdir(),
    • form configparser import ConfigParser: 可以直接使用CinfigParser()
    • 支持一次导入多个

  3. 一个文件夹为一个包(Python3,文件夹中不需要建立__init__.py文件)

    常用系统模块

  4. os: 与操作系统交互
    • os.name/os.sep/os.linesep: 系统名称/系统路径分隔符/系统换行符
    • os.makedir()/os.makedirs(): 建立目录/建立多级目录
    • os.getenv("PATH"): 获取系统PATH环境变量的设置
    • os.curdir/os.prdir: 获取当前路径/上级路径
    • os.walk(): 遍历文件夹及子文件
    • os.path.basename()/os.path.abspath()/os.path.dirname(): 文件名/文件绝对路径/文件上级文件夹名
    • os.path.join()/os.path.split(): 按当前系统分隔符(os.sep)组装路径/分割路径
    • os.path.exists()/os.path.isfile()/os.path.isdir(): 判断文件(文件夹)是否存在/是否文件/是否文件夹
    • 案例: 用例发现, 列出文件夹及子文件夹中所有test开头的.py文件,并输出文件路径
    for root,dirs,files in os.walk("./case/"):
     for file in files:
         if file.startswith("test") and file.endswith(".py"):
             print(os.path.join(root, file)
  5. sys: 与Python系统交互
    • sys.path: 系统路径(搜索路径)
    • sys.platform: system platform may be used to determine or 3 python2
    • sys.argv: py script accepts command-line arguments
    • sys.stdin / sys.stdout / sys.stderr: standard input / output / error

Common algorithm

Bubble Sort

def buddle_sort(under_sort_list):
    l = under_sort_list
    for j in range(len(l)):
        for i in range(len(l)-j-1):
           if l[i] > l[i+1]:
                l[i], l[i+1] = l[i+1], l[i]

Quick Sort

def quick_sort(l):
    if len(l) < 2:
        return l # 如果列表只有一个元素, 返回列表(用于结束迭代)
    else:
        pivot =  l[0] # 取列表第一个元素为基准数
        low = [i for i in l[1:] if i < pivot] # 遍历l, 将小于基准数pivot的全放入low这个列表
        high = [i for i in l[1:] if i >= pivot ]
        return quick_sort(low) + [pivot] + quick_sort(high) # 对左右两部分分别进行迭代

Binary search

def bin_search(l, n): # l为有序列表
    low, high = 0, len(l) - 1 # low,high分别初始化为第一个/最后一个元素索引(最小/最大数索引)
    while low < high:
        mid = (high-low) // 2 # 地板除,保证索引为整数
        if l[mid] == n:
            return mid
        elif l[mid] > n: # 中间数大于n则查找前半部分, 重置查找的最大数
            high = mid -1 
        else: # 查找后半部分, 重置查找的最小数
            low = mid + 1
    return None  # 循环结束没有return mid 则说明没找到

operation

  1. Find the longest line in the file
  2. Counting the number of characters in the file
  3. Analyzing the string "abacdbdde" do not overlap the first character
  4. Analyzing the character string "{{({ () [({})])}}" is closed all brackets

Guess you like

Origin www.cnblogs.com/superhin/p/11455214.html