Python之Unittest和Requests库详解
1.按类来执行
import unittest class f1(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_001(self):
pass
def test_002(self):
pass
'''按类来执行'''
if __name__ == '__main__':
suite = unittest.TestSuite(unittest.makeSuite(f1))
unittest.TextTestRunner(verbosity=).run(suite)
2.加载测试模块来执行
import unittest
from selenium import webdriver
class BaiduLink(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait()
self.driver.get(url="htttp://www.baidu.com")
def tearDown(self):
self.driver.quit()
def test_001(self):
self.driver.find_element_by_link_text("新闻").click()
def test_002(self):
self.driver.find_element_by_link_text("地图").click()
'''加载测试模块来执行(TestLoader)'''
if __name__ == '__main__':
suite=unittest.TestLoader().loadTestsFromModule(BaiduLink)
# suite=unittest.TestLoader().loadTestsFromModule("f2.py")
unittest.TextTestRunner(verbosity=).run(suite)
3.优化测试套件
import unittest
from selenium import webdriver
class BaiduLink(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait()
self.driver.get(url="htttp://www.baidu.com")
def tearDown(self):
self.driver.quit()
def test_001(self):
self.driver.find_element_by_link_text("新闻").click()
def test_002(self):
self.driver.find_element_by_link_text("地图").click()
def suite(self):
suite = unittest.TestLoader().loadTestsFromModule(BaiduLink)
return suite
'''加载测试模块来执行(TestLoader)'''
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=).run(BaiduLink.suite())
4.skip的应用

5.批量执行测试用例discover

6.分离测试固件


7.生成测试报告
import unittest
import os
from testCase1 import HTMLTestRunner_cn
import time
'''批量执行所有的测试用例''' def allTests():
suite = unittest.TestLoader().discover(
start_dir=os.path.dirname(__file__),
pattern="test_*.py",
top_level_dir=None
)
return suite
def getNowTime():
return time.strftime("%Y-%m-%d %H_%M_%S",time.localtime(time.time())) def run():
# unittest.TextTestRunner(verbosity=).run(allTests())
fp = os.path.join(os.path.dirname(__file__),"report",getNowTime() + "testReport.html")
HTMLTestRunner_cn.HTMLTestRunner(
stream=open(fp,"wb"),
title="自动化测试报告",
description="自动化测试报告详细信息"
).run(allTests()) if __name__ == '__main__':
run()
8.


9.

10.关闭证书

11.cookie关联


12.对用户名密码的认证方式

13.session

14.上传文件

15.数据驱动操作elcel文件的操作方法
import xlrd
import os
from xlutils.copy import copy def base_dir(filename):
return os.path.join(os.path.dirname(__file__),filename)
# '''elcel文件的操作'''
# work = xlrd.open_workbook(base_dir("data.xls"))
# sheet = work.sheet_by_index()
# #查看文件有多少行
# print(sheet.nrows)
# #获取单元格的内容
# print(sheet.cell_value(,)) '''excel文件内容的修改'''
work = xlrd.open_workbook(base_dir("data.xls"))
old_content=copy(work)
ws = old_content.get_sheet()
ws.write(,,"")
old_content.save(base_dir("data1.xls"))
Python之Unittest和Requests库详解的更多相关文章
- python接口自动化测试之requests库详解
前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- Python爬虫:requests 库详解,cookie操作与实战
原文 第三方库 requests是基于urllib编写的.比urllib库强大,非常适合爬虫的编写. 安装: pip install requests 简单的爬百度首页的例子: response.te ...
- Python爬虫学习==>第八章:Requests库详解
学习目的: request库比urllib库使用更加简洁,且更方便. 正式步骤 Step1:什么是requests requests是用Python语言编写,基于urllib,采用Apache2 Li ...
- 爬虫学习--Requests库详解 Day2
什么是Requests Requests是用python语言编写,基于urllib,采用Apache2 licensed开源协议的HTTP库,它比urllib更加方便,可以节约我们大量的工作,完全满足 ...
- requests库详解 --Python3
本文介绍了requests库的基本使用,希望对大家有所帮助. requests库官方文档:https://2.python-requests.org/en/master/ 一.请求: 1.GET请求 ...
- python的requests库详解
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- Python爬虫系列-Requests库详解
Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...
- 三十一、python中urllib和requests包详解
A.urllibimport urllibimport urllib.requestimport json '''1.loads,dumpsjson.loads():将字符串转化成python的基础数 ...
随机推荐
- 安装git-review
参考 https://blog.csdn.net/qq18340811755/article/details/80965188 当yum install git-review安装失效,没有安装包时,只 ...
- Spring boot加载REACTIVE源码分析
一,加载REACTIVE相关自动配置 spring boot通过判断含org.springframework.web.reactive.DispatcherHandler字节文件就确定程序类型是REA ...
- .net 多线程之线程取消
//线程取消不是操作线程,而是操作信号量(共享变量,多个线程都能访问到的东西,变量/数据库的数据/硬盘数据) //每个线程在执行的过程中,经常去查看下这个信号量,然后自己结束自己 //线程不能别人终止 ...
- java.lang.Long 类源码解读
总体阅读了Long的源码,基本跟Integer类类似,所以特别全部贴出源码,直接注释进行理解. // final修饰符 public final class Long extends Number i ...
- cesium3dtiles位置改变
cesium偏移3dtiles高度var heightOffset = 20.0; var boundingSphere = tileset.boundingSphere; var cartograp ...
- vue登录注册及token验证
// router.jsimport Vue from 'vue'import VueRouter from 'vue-router' Vue.use(VueRouter) const routes ...
- c# 复制整个文件夹的内容,Copy所有文件
/// <summary> /// 文件夹下所有内容copy /// </summary> /// <param name="SourcePath"& ...
- Android 开发 SharedPreferences数据会话类模板
简单的模板 public class SPDataSession { private static SPDataSession mSPDataSession; private SharedPrefer ...
- eShopOnContainers 看微服务③:Identity Service
引言 通常,服务所公开的资源和 API 必须仅限受信任的特定用户和客户端访问.那进行 API 级别信任决策的第一步就是身份认证——确定用户身份是否可靠. 在微服务场景中,身份认证通常统一处理.一般有两 ...
- H5使用codovar插件实现微信支付(微信APP支付模式,前端)
H5打包的app实现微信支付及支付宝支付,本章主要详解微信支付,支付宝支付请查看另一篇“H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)” ps:本文只试用H5开发的,微信 AP ...