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的基础数 ...
随机推荐
- linux下批量查找UTF-8的BOM文件,并去除BOM
首先查找看看有哪些文件包含BOM find . -type f -print0 | xargs -0r awk '/^\xEF\xBB\xBF/ {print FILENAME} {nextfile} ...
- python生成linux命令行工具
您是否也曾一直想生成类似cd, cat等小巧/迷人/实用的小工具作为系统命令或者将python程序打包为exe进行分发?ok,机会来了.利用python 的argparse 和 pyinstaller ...
- dialog学习
11.dialog底部弹出动画: ==== 11.dialog底部弹出动画: 点击Button调用代码 private void show() { Dialog dialog = new Dialog ...
- [UE4]Overlap Event 碰撞事件
一.对于VR中角色的手模型,一般是在角色中另外添加一个球型碰撞体 二.并且一定要勾选“Generate Overlap Events(触发重叠事件)”选项(默认状态是勾选的) 三.添加开始碰撞事件 ...
- mysql 表
关系 create table scores( id int primary key auto_increment, stuid int, subid int, score decimal(5,2) ...
- [UNITY 5.4 UGUI] 控件重叠触摸穿透
问题. imge 和 button重叠时,imge 覆盖在button上面,导致点击事件无法传递到button. 1.给imge 添加 [Canvas Group]组件 2.修改[Canvas Gro ...
- 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删
原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...
- js点击图片放大
废话不说直接放代码了: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 使用 nodeJs 开发微信公众号(上传图片)
在给用户发送消息中涉及到的素材(图片.视频.音频.文章等)需要事先传到微信服务器,然后获得媒体id(media_id),然后把 media_id 传递给用户 上传分上传临时素材(只保存三天)和上传永久 ...
- python学习 生成随机函数 random模块的用法
random模块是用于生成随机数 常用函数 函数 含义 random() 生成一个[0,1.0)之间的随机浮点数 uniform(a,b) 生成一个a到b之间的随机浮点数 randint(a,b) 生 ...