本文包括:
execl数据驱动、MySQL数据驱动、CSV数据驱动、爬取拉勾网实例、ddt测试驱动、对XML文件的读取、测试执行的log读取…
Quick Start
一.execl数据驱动
1.xlutils简要说明
导包:pip3 install xlutils
注意⚠️:xlutils在介些Execl文件的时候,只识别.xls后缀的文件,如果是.xlsx后缀的文件被解析,.xlsx后缀的文件会被破坏
2.接口自动化中对execl简单的读取
#!/user/bin/env python
#coding:utf-8
#Author:shenqiang
'''xlrd写入文件,同时清空原文件,一般这种方法只用来读'''
import xlrd
import os
'''拿到文件的路径'''
def base_path(filename = None):
return os.path.join(os.path.dirname(__file__),filename)
'''读取文件内容'''
work = xlrd.open_workbook(base_path('execlTestFile.xls'))
'''以下标或者sheet名取对应的哪页'''
sheet = work.sheet_by_index(0)
# sheet = work.sheet_by_name()
'''查看文件有多少行'''
print(sheet.nrows)
'''获取单元格内容,第3行,第3列'''
print(sheet.cell_value(2,2))
3.改写execl文件的内容
#!/user/bin/env python
#coding:utf-8
#Author:shenqiang
import xlrd
import os
from xlutils.copy import copy
'''拿到文件的路径'''
def base_path(filename = None):
return os.path.join(os.path.dirname(__file__),filename)
'''打开文件'''
work = xlrd.open_workbook(base_path('execlTestFile.xls'))
'''把文件内存存在一个变量里'''
file_content = copy(work)
'''拿到文件需要改写的sheet页'''
file = file_content.get_sheet(0)
# print(file_content)
'''
定位文件位置写入内容
行和列以从0开始数下标
'''
file.write(2,2,'沈强')
'''保存文件,并且重新命名'''
file_content.save(base_path('execlTestFile.xls'))
4.configparser配置文件的读取(服务和数据库连接)
配置文件名
config.ini
文件内容:
[linux] ip:10.0.13.26 port:22 username:root password:W2ynE6b58wheeFho [mysql] ip:10.0.13.26 port:22 username:root password:W2ynE6b58wheeFho
文件名
tryConfigparser.py
文件内容
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : shenqiang
import os
import configparser
'''拿到文件的路径'''
def base_path(filename=None):
return os.path.join(os.path.dirname(__file__), filename)
'''函数的默认参数处理'''
def getConfigparser(Linux='linux'):
'''实例化对象'''
config = configparser.ConfigParser()
'''读取文件内容'''
config.read(base_path('config.ini'))
ip = config.get(Linux, 'ip')
port = config.get(Linux, 'port')
username = config.get(Linux, 'username')
password = config.get(Linux, 'password')
return [ip, port, username, password]
print(getConfigparser(),type(getConfigparser()))
# '''遍历文件内容'''
# for i in range(len(getConfigparser())):
# print(getConfigparser()[i])
5.mysql常用的一些操作指令
启动MySQL服务 mysql.server start 停止MySQL服务 mysql.server stop 重启MySQL服务 mysql.server restart 进入MySQL数据库 mysql -u root -p Password: 密文传输(shen6409175) '''查看数据库''' show databases; '''选中数据库''' use students; '''查看表''' show tables; '''创建表''' create table student( id int primary key, name varchar(50), age varchar(10), address varchar(100) ); '''查看表结构''' desc student; '''查看表设计''' show create table student;