如何做接口自動化測試?(如何做接口自動化測試工作)
通過用Python實現(xiàn)頭條項目接口自動化測試,下面一起來學(xué)習(xí)。
目標(biāo)
- 1. 熟悉接口自動化測試的流程
- 2. 能夠?qū)σ粋€項目的接口實現(xiàn)自動化測試
接口自動化測試流程
- 1. 需求分析
- 2. 挑選需要做自動化測試的功能
- 3. 設(shè)計測試用例
- 4. 搭建自動化測試環(huán)境[可選]
- 5. 設(shè)計自動化測試項目的架構(gòu)[可選]
- 6. 編寫代碼
- 7. 執(zhí)行測試用例
- 8. 生成測試報告并分析結(jié)果
項目接口介紹
1. 項目介紹
項目名稱:黑馬頭條
項目描述:黑馬頭條是一款基于數(shù)據(jù)挖掘的推薦引擎產(chǎn)品,它為用戶推薦有價值的、個性化的科技資訊,提供連接人與信息的新型服務(wù)。
項目技術(shù)架構(gòu):
2. 項目接口分析
分析接口文檔…
3. 挑選需要做接口測試的功能
用例設(shè)計
1. 單接口的用例設(shè)計
2. 業(yè)務(wù)功能的用例設(shè)計
項目搭建
1. 新建項目
項目名稱:apiAutoTestHmtt
2. 創(chuàng)建目錄結(jié)構(gòu)
3. 安裝依賴包
pip install requests
編寫代碼
1. 封裝接口類
根據(jù)用例分析待測功能,按功能模塊定義接口類
登錄:login.py頻道:channel.py文章:article.py收藏:collections.py
2. 編寫測試腳本
1. 定義測試腳本文件
登錄模塊:test_login.py頻道模塊:test_channel.py文章模塊:test_article.py收藏模塊:test_collections.py
2. 使用unittest管理測試腳本
3. 執(zhí)行測試腳本
1. 使用unittest執(zhí)行測試腳本
2. 調(diào)試代碼
4. 數(shù)據(jù)庫數(shù)據(jù)校驗
4.1 用例場景
調(diào)用收藏文章的接口后,校驗數(shù)據(jù)庫中是否插入了對應(yīng)的收藏記錄。
4.2 表結(jié)構(gòu)
CREATE TABLE `news_collection` (`collection_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵id',`user_id` bigint(20) unsigned NOT NULL COMMENT '用戶ID',`article_id` bigint(20) unsigned NOT NULL COMMENT '文章ID',`create_time` datetime NOT NULL COMMENT '創(chuàng)建時間',`is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否取消收藏, 0-未取消, 1-已取消',`update_time` datetime NOT NULL COMMENT '更新時間',PRIMARY KEY (`collection_id`),UNIQUE KEY `user_article` (`user_id`,`article_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶收藏表';
4.3 示例代碼
# 收藏def test_collections(self):article_id = 1 # 文章iduser_id = 1 # 用戶id# 收藏response = self.collections_api.collections(article_id)# 斷言響應(yīng)數(shù)據(jù)json_result = response.json()self.assertEqual("OK", json_result.get("message"))# 數(shù)據(jù)庫數(shù)據(jù)校驗conn = pymysql.connect("localhost", "root", "root", "hmtt")cursor = conn.cursor()sql = "select collection_id,is_deleted from news_collection where user_id=%s and article_id=%s"cursor.execute(sql, (user_id, article_id))data = cursor.fetchone()cursor.close()conn.close()self.assertIsNotNone(data)self.assertEqual(0, data[1]) # 未取消
4.4 封裝數(shù)據(jù)庫操作工具類
為了減少代碼的冗余,提高測試效率,可以對數(shù)據(jù)庫的相關(guān)操作封裝成工具類。
示例代碼
import pymysqlclass DBUtil:_conn = None # 數(shù)據(jù)庫連接對象@classmethoddef get_conn(cls):"""獲取數(shù)據(jù)庫連接對象"""if cls._conn is None:cls._conn = pymysql.connect("localhost", "root", "root", "hmtt")return cls._conn@classmethoddef close_conn(cls):"""關(guān)閉數(shù)據(jù)庫連接"""if cls._conn:cls._conn.close()cls._conn = None@classmethoddef get_cursor(cls):"""獲取游標(biāo)對象"""return cls.get_conn().cursor()@classmethoddef close_cursor(cls, cursor):"""關(guān)閉游標(biāo)對象"""if cursor:cursor.close()@classmethoddef get_one(cls, sql):"""查詢一條記錄"""data = Nonecursor = Nonetry:cursor = cls.get_cursor()cursor.execute(sql)data = cursor.fetchone()except Exception as e:print("get_one error: ", e)finally:cls.close_cursor(cursor)cls.close_conn()return data
數(shù)據(jù)驅(qū)動
1. 數(shù)據(jù)驅(qū)動
1.1 定義數(shù)據(jù)文件
1. 定義存放測試數(shù)據(jù)的目錄,目錄名稱:data
2. 分模塊定義數(shù)據(jù)文件
登錄模塊:login.json頻道模塊:channel.json文章模塊:article.json收藏模塊:collections.json
3. 根據(jù)業(yè)務(wù)編寫用例數(shù)據(jù)
1.2 測試數(shù)據(jù)參數(shù)化
修改測試腳本,使用parameterized實現(xiàn)參數(shù)化
生成測試報告
使用HTMLTestRunner生成測試報告
report_file = "./report/report{}.html".format(time.strftime("%Y%m%d-%H%M%S"))with open(report_file, "wb") as f:runner = HTMLTestRunner(f, title="黑馬頭條接口自動化測試報告", description="V1.0")runner.run(suite)