自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

測試驅(qū)動技術(TDD)系列之-pytest實現(xiàn)測試數(shù)據(jù)驅(qū)動

開發(fā) 后端
本篇文章則介紹如何使用Python進行數(shù)據(jù)驅(qū)動。這里以pytest測試框架為例,重點講解pytest參數(shù)化相關知識

[[382621]]

 本篇文章則介紹如何使用Python進行數(shù)據(jù)驅(qū)動。這里以pytest測試框架為例,重點講解pytest參數(shù)化相關知識。(關于pytest的環(huán)境配置以及基礎使用不在本文的討論范圍)

pytest中使用標簽@pytest.mark.parametrize 實現(xiàn)參數(shù)化功能,在執(zhí)行用例的時候該標簽迭代中的每組數(shù)據(jù)都會作為一個用例執(zhí)行。

一組參數(shù)化數(shù)據(jù)

定義參數(shù)化數(shù)據(jù),代碼如下:

  1. class TestDemo1: 
  2. @pytest.mark.parametrize('actual_string, expect_string', [(1, 1), ('BB''BB'),('AA''BB')]) 
  3.  
  4.       def test_1(self, actual_string, expect_string): 
  5.  
  6.            assert (expect_string == actual_string) 

 運行結(jié)果如下,三組數(shù)據(jù)在三條測試用例中運行,其中數(shù)據(jù)('AA', 'BB')運行失敗!


多組參數(shù)化數(shù)據(jù)

在一個測試類中,可以定義多組參數(shù)化數(shù)據(jù)(參數(shù)化數(shù)據(jù)個數(shù)不同,test_1二個,test_2三個),代碼如下:

  1. class TestDemo1: 
  2.  
  3.    @pytest.mark.parametrize('actual_string, expect_string', [(1, 1), ('BB''BB'),('AA''BB')]) 
  4.  
  5.    def test_1(self, actual_string, expect_string): 
  6.  
  7.        assert (expect_string == actual_string) 
  8.  
  9.  
  10.    @pytest.mark.parametrize('result, a,b', [(1, 1,0),(2, 1,0) ]) 
  11.  
  12.    def test_2(self, result, a,b): 
  13.  
  14.        assert (result == a+b) 

 運行結(jié)果如下,二組數(shù)據(jù)分別在test_1和test_2中運行!


從excel中讀取數(shù)據(jù)作為參數(shù)

我們可以自定義一些方法,對外部文件進行讀取,然后把讀取的數(shù)據(jù)作為參數(shù)在pytest

中引用。把測試數(shù)據(jù)保存在excel中,如下圖


寫一個讀取excel類文件的方法,使用模塊pandas ,使用命令pip install pandas 安裝模塊,源碼如下:

  1. import pandas as pd 
  2.  
  3. # 讀取Excel文件 -- Pandas 
  4.  
  5. def read_data_from_pandas(excel_file, sheet_name): 
  6.  
  7.     if not os.path.exists(excel_file): 
  8.  
  9.         raise ValueError("File not exists"
  10.  
  11.     s = pd.ExcelFile(excel_file) 
  12.  
  13.     df = s.parse(sheet_name)#解析sheet頁的數(shù)據(jù) 
  14.  
  15.     return df.values.tolist()#數(shù)據(jù)返回為list 

 從excel中讀取數(shù)據(jù),并賦值給變量進行參數(shù)化,代碼如下:

  1. @pytest.mark.parametrize('actual_string, expect_string', read_data_from_pandas('E:/TestData.xls''data1')) 
  2.  
  3. def test_3(self, actual_string, expect_string): 
  4.  
  5.     assert (expect_string == actual_string) 

 運行結(jié)果如下,三組數(shù)據(jù)在三條測試用例中運行!


注意:excel中的首行,默認不會作為測試數(shù)據(jù)處理。

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2021-02-04 07:30:14

測試驅(qū)動技術excel讀取數(shù)據(jù)

2021-01-19 07:46:48

TestNG測試驅(qū)動TDD

2021-02-04 07:12:15

測試excelapi

2009-10-10 10:55:48

TDD技術

2014-04-09 11:13:37

測試驅(qū)動開發(fā)

2023-09-11 11:05:49

軟件開發(fā)TDD

2010-01-28 09:07:50

Visual Stud

2023-02-23 19:28:09

ODD測試

2018-05-11 08:29:10

Python自動化測試數(shù)據(jù)驅(qū)動

2018-05-11 13:39:05

PythonCSV接口測試

2013-06-07 19:04:15

測試

2013-06-27 10:34:08

準備性能測試數(shù)據(jù)

2009-12-11 15:13:15

VS 2010驅(qū)動

2023-07-28 10:27:48

Java單元測試

2021-12-30 07:33:03

數(shù)據(jù)庫

2011-07-22 09:29:54

裸線測試

2023-12-25 09:52:32

2013-02-27 15:24:45

Calxeda測試數(shù)據(jù)ARM架構(gòu)

2014-11-12 11:13:02

SUSE

2023-10-07 08:49:56

測試驅(qū)動開發(fā)Xunit 框架
點贊
收藏

51CTO技術棧公眾號