python接口测试非json的断言_荐在接口自动化测试中,如何利用Pytest + JSONPath 进行接口响应断言...
之前有一篇文章,介紹了如何使用JSONSchema對接口響應進行斷言,主要的適用場景是對響應的數據結構進行校驗,保證客戶端收到的數據結構穩定和合法。今天,介紹使用JSONPath對接口響應的值進行斷言方法。
上一篇文章《在接口自動化測試中,如何利用Pytest + JSON Schema 進行接口響應斷言》中,介紹了JSON Schema校驗接口響應的數據結構的方法。在實際的測試工作中,很多時候是需要對接口的響應數值進行校驗的。這時候就不能利用JSON Schema進行校驗了,需要借助JSONPath表達式從JSON中抽取數值,進行斷言。
通過JSONPath表達式,使得從多層嵌套JSON數據中提取數據變得非常簡單。
下面這段數據是從JsonPath的官網https://goessner.net/articles/JsonPath/上摘抄的,假定這是一個接口的response。
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
從這個response中,提取所有商品的價格,放到一個列表中,如果使用JSONPath表達式,只需要一行代碼:
$.store..price
代表JSON對象的根節點,代表JSON對象的根節點,代表JSON對象的根節點,代表JSON對象的根節點,.store表示根節點下面的store屬性,$.store…price則表示根節點下面的store屬性下面的所有price屬性的值。兩個點表示store下面遞歸查找所有的price屬性,因此在這個例子中可以查到所有的book的price和bicycle的price。
一個更簡單的辦法是從根節點開始遞歸查找price:
$..price
如果不使用JSONPath,想要獲得所有的商品價格,則要寫一個循環來讀取,可以看出,使用JSONPath從JSON中提取數據變得簡單和高效。
接下來看看JSONPath中都有哪些表達式。
01 — JSONPath表達式
JSONPath通過表達式定位JSON中的數據,一共有15個表達式,通過它們的靈活組合,提取JSON中數據非常容易。我們把JSON對象中每一個key都叫做一個屬性(property)。下表是所有的JSONPath表達式,及其含義。
以response這個JSON對象為例,通過上面這些表達式的組合來取得值:
表達式
取得的值
$.store.*
response的store屬性下的所有對象,是個list,第1個元素book,是一個list,第2個元素是bicycle,是一個對象。
$.store.bicycle.color
response的store屬性下bicycle屬性的color屬性的值,red
$.store..price、$..price
response中所有的price屬性的值: [8.95, 8.99, 22.99, 19.95]
$.store.book[*] $..book[*]
response中所有的book對象,是個list
$…book[*].title
response中所有的book對象的title的list:[Sayings of the Century,Moby Dick,The Lord of the Rings]
$…book[0]
response中的第一本書,也是個list:[ { “category”:“reference”, “author”:“Nigel Rees”, “title”:“Sayings of the Century”, “price”:8.95 } ]
$…book[0].title
response中的第1本書的title:“Sayings of the Century”
$..book[0,1].title $..book[:2].title
response中的前2本書的title:[Sayings of the Century, Moby Dick]
$…book[::2].title
response中每一個取一本圖書的title
$..book[-1:].title $..book[(@.length-1)].title
response最后1本書的title,是個list:[The Lord of the Rings]
$…book[?(@.author==‘J.R.R. Tolkien’)].title
過濾表達式,作者為’J.R.R. Tolkien’的book的書名
$…[?(@.isbn)]
過濾表達式,所有包含isbn這個屬性的對象
$…[?(!@.isbn)]
過濾表達式,所有不包含isbn這個屬性的對象
$…book[?(@.price < 10)]
價格低于10的書,是一個對象list
$..book[?(@.price > $.expensive)]
價格比$.expensive高的書,是個list
$..book[?(@.author =~ /.*Tolkien/i)]
作者的名字以Tolkien結尾的書,是個list,大小寫敏感
$..book[?(@.category == 'fiction' || @.category == 'reference')]
category為fiction或者reference的書,是個list
通過上面的例子,可以看到還是挺簡單的。
02 — 在斷言中的應用
依然假定上面的JSON對象是某個接口的response反序列化得到的。接下來,看看在自動化測試中如何利用JSONPath表達式進行斷言。
在Python語言中,使用JSONPath需要安裝一個第三方庫jsonpath。
pip install jsonpath
測試用例1:
驗證response中包含’Nigel Rees’, ‘Evelyn Waugh’, ‘Herman Melville’, 'J. R. R. Tolkien’這四位作者的圖書。
import jsonpath
def test_authors():
author_list = jsonpath.jsonpath(response, '$.store.book[*].author')
assert author_list == ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
2. 測試用例2:
驗證response中的商品價格包含8.95, 12.99, 8.99, 22.99, 19.95這5種。
def test_all_price():
store_price_list = jsonpath.jsonpath(response, '$.store..price')
# store_price_list = jsonpath.jsonpath(response, '$..price')
assert store_price_list == [8.95, 12.99, 8.99, 22.99, 19.95]
3.測試用例3:
驗證response中的第3本圖書的書名是Moby Dick。
def test_third_book_title():
book_3 = jsonpath.jsonpath(response, '$.store.book[2]')
assert book_3[0]['title'] == "Moby Dick"
4.測試用例4:
驗證response中的最后一本圖書的isbn是0-395-19395-8。
def test_last_book_isbn():
last_book_isbn = jsonpath.jsonpath(response, f'$.store.book[-1:].isbn')
# last_book_isbn = jsonpath.jsonpath(response, f'$.store.book[(@.length-1)].isbn')
assert last_book_isbn == "0-395-19395-8"
5.測試用例5:
驗證repsonse中前兩本書價格之和是8.95 + 12.99
def test_12_books_price():
book_12 = jsonpath.jsonpath(response, '$..book[0,1].price')
assert book_12[0] + book_12[1] == 8.95 + 12.99
6.測試用例6:
驗證response中有兩本書的價格小于10
def test_2books_cheap_than_10():
book_lg10 = jsonpath.jsonpath(response, '$..book[?(@.price<10)]')
assert len(book_lg10) <= 2
7.測試用例7:
驗證response中具有color屬性的商品,其color是red
def test_has_color():
colorful_goods = jsonpath.jsonpath(response, '$.store..[?(@.color)]')
assert "red" == colorful_goods[0]['color']
好了就舉這些例子吧。更多的例子大家可以參考第一小節JSONPath表達式來嘗試。
Python還有一個包叫jsonpath2,用法和jsonpath差不多。大家可以自行嘗試,示例代碼可以參考:
https://jsonpath2.readthedocs.io/en/latest/_modules/bookstore_test.html#TestBookStore
03 — 總結
本文介紹了JSONPath表達式語法,以及在自動化測試中的應用。使用JSONPath表達式提取JSON數據的數值,非常簡介和易懂。推薦大家需要對JSON數據的準確性進行校驗時,使用JSONPath表達式。
參考資料
https://goessner.net/articles/JsonPath/
https://www.cnblogs.com/angle6-liu/p/10580792.html
https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
https://jsonpath2.readthedocs.io/en/latest/_modules/bookstore_test.html#TestBookStore
原文鏈接:https://blog.csdn.net/liuchunming033/article/details/106272542
總結
以上是生活随笔為你收集整理的python接口测试非json的断言_荐在接口自动化测试中,如何利用Pytest + JSONPath 进行接口响应断言...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 资源分享
- 下一篇: 峰米公布 S5 Rolling 概念投影