Pytest之參數化(四)

  • 2019 年 10 月 5 日
  • 筆記

懂得UI自動化測試的人,應該都比較清楚ddt的模塊,在一個測試場景中,如果是同樣的測試步驟,那麼使用ddt,就可以使用一個單個測試解決多個測試場景的使用。本文章主要總結pytest測試框架的參數化的應用。

還是通過一個具體的案例來說明這部分的案例應用,比如寫一個兩個數相加之和來,那麼它的測試場景就很多的,如果編寫測試點也是很多的,我們就按傳統的方式來測試它,見案例代碼和測試代碼:

#!/usr/bin/python3  #coding:utf-8  def add(a,b):      return a+b    def test_001():      assert  add(1,2)==3  def test_002():      assert add(2,2)==4  def test_003():      assert  add('hi',' wuya')=='hi wuya'

見如上的測試代碼,首先不考慮它的測試點是否設計合理,就單純的來說,一個函數的測試需要寫很多的測試代碼,相對而言不是一個好的選擇,寫了很多的垃圾代碼,但是在pytest的參數化而言,可以很輕鬆的來解決這個問題,見實現的代碼:

#!/usr/bin/python3  #coding:utf-8  import  pytest    def add(a,b):      return a+b    @pytest.mark.parametrize('a,b,result',[      (1,2,3),      (2,2,4),      ('hi',' wuya','hi wuya')  ])  def test_add(a,b,result):      assert  add(a,b)==result

執行如上的代碼,見執行結果的輸出:

============================= test session starts ==============================  platform darwin -- Python 3.7.4, pytest-4.0.2, py-1.8.0, pluggy-0.12.0 -- /usr/local/bin/python3.7  cachedir: .pytest_cache  metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.7.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.0.2', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Plugins': {'xdist': '1.29.0', 'forked': '1.0.2', 'sugar': '0.9.2', 'html': '1.22.0', 'cov': '2.7.1', 'allure-adaptor': '1.7.10', 'metadata': '1.8.0'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home'}  rootdir: /Applications/code/stack/pyUnit, inifile:  plugins: xdist-1.29.0, forked-1.0.2, sugar-0.9.2, html-1.22.0, cov-2.7.1, allure-adaptor-1.7.10, metadata-1.8.0  collecting ... collected 3 items    f1.py::test_add[1-2-3] PASSED                                            [ 33%]  f1.py::test_add[2-2-4] PASSED                                            [ 66%]  f1.py::test_add[hi- wuya-hi wuya] PASSED                                 [100%]    =========================== 3 passed in 0.03 seconds ===========================

依據執行結果可以看到,剛才說的幾個測試點都包含到了,而且只使用了一個測試的函數,這樣來說更加高效和方便。當然代碼還可以更加簡單,也就是對測試的數據可以分離,見修改後的代碼:

#!/usr/bin/python3  #coding:utf-8  import  pytest    def add(a,b):      return a+b    addList1=[      (1,2,3),      (2,2,4),      ('hi',' wuya','hi wuya')  ]    @pytest.mark.parametrize('a,b,result',addList1)  def test_add(a,b,result):      assert  add(a,b)==result

當然測試數據可以分離到文件中,這地方就不詳細的再介紹了。

固件參數化會使用到pytest中內置的固件request,並通過request.param來獲取參數。還是以上面的案例來修改,見修改後的測試代碼:

#!/usr/bin/python3  #coding:utf-8  import  pytest    def add(a,b):      return a+b    addList1=[      (1,2,3),      (2,2,4),      ('hi',' wuya','hi wuya')  ]    @pytest.mark.parametrize('a,b,result',addList1)  def test_add(a,b,result):      assert  add(a,b)==result      dict1=[      {'a':1,'b':2,'result':3},      {'a':2,'b':2,'result':4}  ]    @pytest.fixture(params=dict1)  def param(request):      return request.param    def test_add_param(param):      add(param['a'],param['b']==param['result'])

固件函數使用於另外一個測試的場景,我們需要連接很多好幾個數據庫來操作不同的業務場景,那麼可以使用固件參數來很好的解決該問題,測試代碼如下:

#!/usr/bin/python3  #coding:utf-8  import  pytest    @pytest.fixture(params=[      ('MySQL:','root','123456'),      ('Oracle','wuya','123456')  ])  def param(request):      return request.param    @pytest.fixture(autouse=True)  def connDb(param):      print('連接數據庫%s,賬戶:%s,密碼:%s'%param)      yield      print('關閉數據庫%s,賬戶:%s,密碼:%s' % param)    def test_database():      assert  1==1