Python掌握

2026/06/243 分钟阅读655

概述

Python是一种高级的、解释型的编程语言,它支持多种编程范式,包括面向对象、命令式、函数式和过程式编程。Python的设计哲学强调代码的可读性和简洁性,尤其是通过使用缩进来表示代码块。

Python的主要特点包括:

  1. 易于学习和使用
  2. 高级数据类型(如列表、字典和集合)
  3. 动态类型系统
  4. 支持面向对象编程
  5. 大量的第三方库和框架
  6. 跨平台支持

目标

基础

  1. Python环境搭建
  2. 数据类型
  3. 条件控制
  4. 循环语句
  5. 列表推导式
  6. 函数
  7. 装饰器
  8. 面向对象

第三方库

  1. request
  2. pandas
  3. jsonpath
  4. pymysql
  5. locust
  6. pytest
  7. Path
  8. playwright
  9. SQLAlchemy
  10. jinja2

里程碑

Pytho环境管理

  • Pyenv:占用较小适合纯Python应用;
  • Anaconda:占用比较大,内置了很多数据科学用的库;

UI自动化

利用playwright框架

接口自动化

Pytest+Request+Allure+Jenkins

conftest.py文件 钩子配置

def pytest_assertrepr_compare(op, left, right):

if op == "==":

if not isinstance(right, type(left)):

return [

f"断言错误,实际值与期望值的数据类型不一致,实际值数据类型为:{type(left)}, 期望值为:{type(right)}",

f"实际值为:{left}, 期望值为:{right}",

]

else:

return [

f"断言错误,实际值与期望值不一致,实际值为:{left}, 期望值为:{right}",

]

if op == "in":

if isinstance(left, str) and isinstance(right, str):

return [

f"期望 {left}{right} 的子串,实际 {left} 不是 {right} 的子串,"

]

elif isinstance(right, list) or isinstance(right, set) or isinstance(right, tuple):

return [

f"期望 {left} 是集合 {right} 中的一个元素,实际集合 {right} 中没有 {left} 元素"

]

elif isinstance(right, dict):

return [

f"期望 {left} 是字典 {right} 中的一个key,实际字典 {right} 中没有值为 {left} 的key"

]

else:

return [

f"期望 {left}{right} 中的一部分,实际上 {left} 并不是 {right} 的一部分"

]

if op == "not in":

if isinstance(left, str) and isinstance(right, str):

return [

f"期望 {left} 不是 {right} 的子串,实际 {left}{right} 的子串,"

]

elif isinstance(right, list) or isinstance(right, set) or isinstance(right, tuple):

return [

f"期望 {left} 不是集合 {right} 中的一个元素,实际集合 {right} 中有 {left} 元素"

]

elif isinstance(right, dict):

return [

f"期望 {left} 不是字典 {right} 中的一个key,实际字典{right}中有值为 {left} 的key"

]

else:

return [

f"期望 {left} 不是 {right} 中的一部分,实际上 {left}{right} 的一部分"

]

def pytest_collection_modifyitems(items):

"""

测试用例收集完成时,讲收集到的item的name和nodeid进行中文显示

:param items:

:return:

"""

for item in items:

item.name = item.name.encode('utf-8').decode('unicode_escape')

item._nodeid = item._nodeid.encode('utf-8').decode('unicode_escape')

Pytho后端服务

FastApi

  1. FastAPI 中Body、Cookie、Header、Query和 Path的区别

Django

经验

动态导包

import os