python学习笔记-14. python常用的三方库
文章目录
前言
由于是测试学习为基础,所以先学两个测试用的,pytest和request
一、pytest的简单使用
1. python简介及优势
简介:
pytest 是一个非常流行且成熟的,全功能的 Python 测试框架,适用于单元测试、UI 测试、接口测试。它和单元测试框架 unittest 类似,但是 pytest 更简洁、高效。所以很多测试人员学习 unittest 和 pytest 之后,都会感觉到 pytest 才是做测试的最好框架。这是因为 pytest 有许多优点。
pytest 主要优点如下:
- 简单灵活,容易上手。
- 支持参数化。
- 可标记测试功能与属性。
- pytest 具有很多第三方插件,并且可以自定义扩展,比较好用的如 pytest-selenium(集成 Selenium)、pytest-html(生成 HTML 测试报告)、pytest-rerunfailures(失败 case 重复执行)等。
- 使用 skip 和 xfail 可以处理不成功的测试用例。
- 可通过 xdist 插件分发测试到多个 CPU。
- 允许直接使用 assert 进行断言,而不需要使用 self.assert *。
- 方便在持续集成工具中使用。
安装方式:
项目目录下直接使用pip install pytest即可
2. pytest的简单示例
import pytest
# 定义一个被测试的方法
def add(a, b):
return a + b
# 用例1
def test_add1():
print("add(2, 3)的结果是:{}".format(add(2, 3)))
assert add(2, 3) == 5
# 用例2
def test_add2():
assert add(2, 3) == 6
if __name__ == "__main__":
pytest.main()
执行结果如下:
collecting:表示扫描到的用例数量
============================= test session starts ==============================
collecting ... collected 2 items
test3.py::test_add1 PASSED [ 50%]add(2, 3)的结果是:5
test3.py::test_add2 FAILED [100%]
test3.py:9 (test_add2)
5 != 6
Expected :6
Actual :5
<Click to see difference>
def test_add2():
> assert add(2, 3) == 6
E assert 5 == 6
E + where 5 = add(2, 3)
test3.py:11: AssertionError
========================= 1 failed, 1 passed in 0.07s ==========================
3. pytest的文件及用例收集规则
测试文件的收集规则:
- 如果未指定参数,则从 testpaths (如果配置)或当前目录收集。
- 递归到目录文件中。
- 搜索 test_*.py 或 *_test.py 文件。
测试文件中收集测试用例规则:
- 不在类里面的,以 test 开头的函数或方法。
- 以 Test 开头的类,在此类下面的,以 test 开头的函数或方法。但是类中不能有构造函数 init 方法。
4. pytest的常用参数
- pytest -v :用于查看测试的详细信息
collected 2 items
test_3.py::test_add1 PASSED [ 50%]
test_3.py::test_add2 FAILED [100%]
======================================================================================================= FAILURES =======================================================================================================
______________________________________________________________________________________________________ test_add2 _______________________________________________________________________________________________________
def test_add2():
> assert add(2, 3) == 6
E assert 5 == 6
E + where 5 = add(2, 3)
test_3.py:11: AssertionError
=============================================================================================== short test summary info ================================================================================================
FAILED test_3.py::test_add2 - assert 5 == 6
============================================================================================= 1 failed, 1 passed in 0.05s ==============================================================================================
- pytest -q: 简化输出信息,执行结果如下:
.F [100%]
======================================================================================================= FAILURES =======================================================================================================
______________________________________________________________________________________________________ test_add2 _______________________________________________________________________________________________________
def test_add2():
> assert add(2, 3) == 6
E assert 5 == 6
E + where 5 = add(2, 3)
test_3.py:11: AssertionError
=============================================================================================== short test summary info ================================================================================================
FAILED test_3.py::test_add2 - assert 5 == 6
1 failed, 1 passed in 0.05s
通过查看运行结果可知,-q 参数简化了许多,只使用 . 和 F 标注出了测试用例运行后是成功的还是失败的。比直接使用命令 pytest 还简洁。
- pytest -s:将我们在测试用例中的调式信息进行输出,比如 print 打印信息
================================================================================================= test session starts ==================================================================================================
platform darwin -- Python 3.8.2, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/gaozeyu/PycharmProjects/pythonProject2
collected 2 items
test_3.py add(2, 3)的结果是:5
.F
======================================================================================================= FAILURES =======================================================================================================
______________________________________________________________________________________________________ test_add2 _______________________________________________________________________________________________________
def test_add2():
> assert add(2, 3) == 6
E assert 5 == 6
E + where 5 = add(2, 3)
test_3.py:11: AssertionError
=============================================================================================== short test summary info ================================================================================================
FAILED test_3.py::test_add2 - assert 5 == 6
============================================================================================= 1 failed, 1 passed in 0.05s ==============================================================================================
二、request的简单使用
项目目录下直接使用pip install request即可安装
1. 使用requests进行get请求
import requests
# 使用request进行get访问,这个是没有任何参数的
r = requests.get('https://api.github.com/events')
# 定义请求头和请求参数字典,然后使用params和headers关键字进行get请求
payload = {'wd':'测试'}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
r1 = requests.get("http://www.baidu.com/s?", params = payload, headers = headers)
# 使用text查看响应的数据,格式为unicode
print(r1.text)
# 使用content,查看返回的字节流数据
print(r1.content)
# 使用url查看请求的完整地址
print(r1.url)
# 使用encoding,查看响应的字符编码
print(r1.encoding)
# 使用status_code,查看响应码
print(r1.status_code)
2. 使用requests进行post请求
post参数传入区别与get,使用data而不是params
import requests
# 定义请求data字典
formdata = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
url = "http://httpbin.org/post"
# 定义请求头字典
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
response = requests.post(url, data=formdata, headers=headers)
print(response.text)
# 如果是json文件可以直接显示
print(response.json())
总结
简单了解下这2个库,后面再做详细的记录