在上一篇文章中聊到了fixture的一些用法,利用params和ids這2個參數來實現對於測試數據初始的一些需求。
【pytest】(十)fixture參數化-巧用params和ids優雅的建立測試數據
可是文中的示例代碼並非真正的接口測試,只是用了一些print描述了下個人設想,以及必定的驗證,缺少實際應用。html
恰好最近抽空在寫一些接口自動化,正好拿一個接口實踐下。
這個接口是一個列表接口,根據status
的傳參,能夠返回對應狀態的數據,其中:待處理-10,處理中-20,已完成-30
。
代碼部分的話 就分2部分貼出來吧。mysql
先看代碼sql
import pytest from faker import Faker from util.mysql_operating import DB fake = Faker() def init_data(fixture_value): if fixture_value == 10: return "untreated" elif fixture_value == 20: return "processing" elif fixture_value == 30: return "done" @pytest.fixture(params=[10, 20, 30], ids=init_data) def init_data_query_by_status(request): status = request.param print("fixture 參數", status) uid = fake.pyint(min_value=10086, max_value=99999, step=1) order_sn = "CK" + fake.pystr(min_chars=19, max_chars=19) real_sql = """INSERT INTO `tcwms`.`sm_outbound_order`(`id`, `out_id`, `order_sn`, `admin_id`, `admin_name`, `total_price`,\ `remark`, `status`, `type`, `use_type`, `mchid`, `wh_id`, `add_time`, `update_time`, `out_time`, `to_id`, `to_name`\ , `verify_status`, `verify_time`, `verify_ad_id`, `verify_ad_name`, `posting_status`, `posting_time`, \ `posting_ad_id`, `posting_ad_name`, `is_valid`, `finance_verify_status`, `finance_verify_time`, `finance_verify_remarks`,\ `finance_verify_ad_id`, `finance_verify_ad_name`, `pay_status`) \ VALUES ({}, 1752, '{}', 572, '', 619.00, '', {}, 1, 0, {}, {}, '2019-06-20 09:55:58', '2019-06-20 09:55:58',\ '2019-01-01 00:00:00', 210, '', 1, '2019-01-01 00:00:00', 0, '', 1, '2019-01-01 00:00:00', 0, '', 1, 0,\ '0000-00-00 00:00:00', '', 0, '', 0);""".format(uid, order_sn, status, 6001, 1) db = DB() db.insert_by_sql(real_sql) yield status real_delete_sql = "DELETE FROM `tcwms`.`sm_outbound_order` WHERE id={};".format(uid) db.delete_by_sql(real_delete_sql) db.close()
代碼比較多,可是結構很簡單。
這個fixture的功能就是:在case執行前插入對應狀態的測試數據,而後case執行事後,刪掉插入的測試數據。json
faker
庫,造一些測試數據。pymysql
庫去作一些封裝,具體就不看了,反正就是用來執行sql語句的。format
去格式化。init_data
就是返回不一樣的id,以便我用-k
命令時候使用yield
把參數status返回出來case部分我就只貼出來測試case的代碼了,知道意思就行:傳參,請求接口,斷言函數
def test_query_by_status(init_data_query_by_status): """ 查詢狀態,10-待處理;20-處理中;30-已完成 """ payload = { "status": init_data_query_by_status } r = requests.post(QA_URL + API_URL, json=payload, headers=HEADER) result = r.json() res_status = jmespath.search("row.taskListOfPage.resultData[*].status", result) res_mchid = jmespath.search("row.taskListOfPage.resultData[0].mchid", result) assert_result = loop_list_same_elements(init_data_query_by_status, res_status) assert assert_result is True assert res_mchid == 6001 if __name__ == "__main__": pytest.main(["-s", "test_wms_outbound_task_list.py::test_query_by_status"])
init_data_query_by_status
中咱們返回了statusjmespath
,用了jmespath庫,能夠方便的提取json,詳細用法在以前的文章有寫。先運行所有的參數,main
方法裏就不要加-k
了oop
if __name__ == "__main__": pytest.main(["-s", "test_wms_outbound_task_list.py::test_query_by_status"])
運行一下:post
collecting ... collected 3 items test_wms_outbound_task_list.py::test_query_by_status[untreated] test_wms_outbound_task_list.py::test_query_by_status[processing] test_wms_outbound_task_list.py::test_query_by_status[done] ============================== 3 passed in 1.00s ============================== Process finished with exit code 0 fixture 參數 10 執行的sql: INSERT INTO `tcwms`.`sm_outbound_order`(`id`, `out_id`, `order_sn`, `admin_id`, `admin_name`, `total_price`, `remark`, `status`, `type`, `use_type`, `mchid`, `wh_id`, `add_time`, `update_time`, `out_time`, `to_id`, `to_name` , `verify_status`, `verify_time`, `verify_ad_id`, `verify_ad_name`, `posting_status`, `posting_time`, `posting_ad_id`, `posting_ad_name`, `is_valid`, `finance_verify_status`, `finance_verify_time`, `finance_verify_remarks`, `finance_verify_ad_id`, `finance_verify_ad_name`, `pay_status`) VALUES (17288, 1752, 'CKpNInphuUHePrYnDHlXw', 572, '', 619.00, '', 10, 1, 0, 6001, 1, '2019-06-20 09:55:58', '2019-06-20 09:55:58', '2019-01-01 00:00:00', 210, '', 1, '2019-01-01 00:00:00', 0, '', 1, '2019-01-01 00:00:00', 0, '', 1, 0, '0000-00-00 00:00:00', '', 0, '', 0); PASSED [ 33%] 執行的sql: DELETE FROM `tcwms`.`sm_outbound_order` WHERE id=17288; fixture 參數 20 執行的sql: INSERT INTO `tcwms`.`sm_outbound_order`(`id`, `out_id`, `order_sn`, `admin_id`, `admin_name`, `total_price`, `remark`, `status`, `type`, `use_type`, `mchid`, `wh_id`, `add_time`, `update_time`, `out_time`, `to_id`, `to_name` , `verify_status`, `verify_time`, `verify_ad_id`, `verify_ad_name`, `posting_status`, `posting_time`, `posting_ad_id`, `posting_ad_name`, `is_valid`, `finance_verify_status`, `finance_verify_time`, `finance_verify_remarks`, `finance_verify_ad_id`, `finance_verify_ad_name`, `pay_status`) VALUES (65242, 1752, 'CKOcOngjWxWxQHsaradMd', 572, '', 619.00, '', 20, 1, 0, 6001, 1, '2019-06-20 09:55:58', '2019-06-20 09:55:58', '2019-01-01 00:00:00', 210, '', 1, '2019-01-01 00:00:00', 0, '', 1, '2019-01-01 00:00:00', 0, '', 1, 0, '0000-00-00 00:00:00', '', 0, '', 0); PASSED [ 66%] 執行的sql: DELETE FROM `tcwms`.`sm_outbound_order` WHERE id=65242; fixture 參數 30 執行的sql: INSERT INTO `tcwms`.`sm_outbound_order`(`id`, `out_id`, `order_sn`, `admin_id`, `admin_name`, `total_price`, `remark`, `status`, `type`, `use_type`, `mchid`, `wh_id`, `add_time`, `update_time`, `out_time`, `to_id`, `to_name` , `verify_status`, `verify_time`, `verify_ad_id`, `verify_ad_name`, `posting_status`, `posting_time`, `posting_ad_id`, `posting_ad_name`, `is_valid`, `finance_verify_status`, `finance_verify_time`, `finance_verify_remarks`, `finance_verify_ad_id`, `finance_verify_ad_name`, `pay_status`) VALUES (26399, 1752, 'CKimCduBGktGeCOZyxbhE', 572, '', 619.00, '', 30, 1, 0, 6001, 1, '2019-06-20 09:55:58', '2019-06-20 09:55:58', '2019-01-01 00:00:00', 210, '', 1, '2019-01-01 00:00:00', 0, '', 1, '2019-01-01 00:00:00', 0, '', 1, 0, '0000-00-00 00:00:00', '', 0, '', 0); PASSED [100%] 執行的sql: DELETE FROM `tcwms`.`sm_outbound_order` WHERE id=26399;
我手動進行了空格分隔下,方便查看。
能夠看到,case根據參數化運行了3次。在每一次的執行當中,都是這樣的順序:測試
接下來,我用-k
只運行 status-20
的case,id也就是processing
:ui
if __name__ == "__main__": pytest.main(["-s", "-k", "processing", "test_wms_outbound_task_list.py::test_query_by_status"])
運行結果:spa
collected 3 items / 2 deselected / 1 selected test_wms_outbound_task_list.py fixture 參數 20 執行的sql: INSERT INTO `tcwms`.`sm_outbound_order`(`id`, `out_id`, `order_sn`, `admin_id`, `admin_name`, `total_price`, `remark`, `status`, `type`, `use_type`, `mchid` , `wh_id`, `add_time`, `update_time`, `out_time`, `to_id`, `to_name` , `verify_status`, `verify_time`, `verify_ad_id`, `verify_ad_name`, `posting_status`, `posting_time`, `posting_ad_id`, `posting_ad_name`, `is_valid`, `finance_verify_status`, `finance_verify_time`, `finance_verify_remarks`, `finance_verify_ad_id`, `finance_verify_a d_name`, `pay_status`) VALUES (32128, 1752, 'CKQLrmWALMTjPDLjtCHKh', 572, '', 619.00, '', 20, 1, 0, 6001, 1, '2019-06-20 09:55:58', '2019-06-20 09:55:58', '2019-0 1-01 00:00:00', 210, '', 1, '2019-01-01 00:00:00', 0, '', 1, '2019-01-01 00:00:00', 0, '', 1, 0, '0000-00-00 00:00:00', '', 0, '', 0); . 執行的sql: DELETE FROM `tcwms`.`sm_outbound_order` WHERE id=32128; =================================== 1 passed, 2 deselected in 0.32s ======================================
能夠看到,只運行了status=20的case。
以上就是實際的例子了。後面可能還會遇到更多複雜的狀況,屆時再分享。