Python基礎學習之 pytest

  Pytest

  1.安裝

  首先使用pip3 install pytest安裝pytest

  pytest --version查看版本

  1.編寫規則

  測試文件以test_開頭或以_test結尾也可以

  測試函數以test_開頭

  測試類以Test開頭,並不能有__init__方法

  例如:test_pydemo.py文件

  def test_add():

  print("I am 1")

  assert add.add_test(1,3)==4

  print("I am 2")

  assert add.add_test(1, 2) == 6

  print("I am 3")

  assert add.add_test(1, 5) == 6

  2.pytest用法

  安裝第三方插件,可以使用pip install安裝或者pycharm中安裝,使用的時候導入import pytest

  在pycharm中,files-》settings-》tools=》python integrated tools=》設定default test runner

  2.1 pytest-參數化

  使用pip install pytest-parametrize安裝

  pytest.mark.parametrize(參數名,參數化元組的數組)

  代碼如下:

  import pytest

  @pytest.mark.parametrize("x,y",[

  (3+5, 8),

  (2+4, 6),

  (6*9, 42),

  ])

  def test_add_by_para(x,y):

  assert add.add_test(x,y)==x+y

  2.2 pytest-assume

  多個assert

  import pytest

  def test_add():

  print("I am 1")

  assert add.add_test(1,3)==4

  print("I am 2")

  assert add.add_test(1, 2) == 6

  print("I am 3")

  assert add.add_test(1, 5) == 6

  pytest-assume

  多個assert語句,當出現錯誤的時候,不會繼續往下執行,如果繼續執行,使用pytest-assume插件

  import pytest

  def test_add():

  print("I am 1")

  pytest.assume(add.add_test(1,3)==4)

  print("I am 2")

  pytest.assume(add.add_test(1, 2) == 5)

  print("I am 3")

  pytest.assume(add.add_test(1, 7) == 8)

  2.3 pytest-rerunfails

  當出錯誤時,會重複執行,run次數可以設置,pytest --reruns 5

  2.4 pytest-ordering

  按照order順序執行

  import pytest

  @pytest.mark.run(order=2)

  def test_add_order():

  print("i am 2")

  assert add.add_test(4,4)==8

  @pytest.mark.run(order=1)

  def test_add_order1():

  print("i am 1")

  assert add.add_test(6,4)==10

  2.5 pytest-sugar

  pytest-sugar改變了pytest的默認外觀,增加了一個進度條,並立即顯示失敗的測試

  2.6 pytest-cov

  pytest-cov增加了對pytest的覆蓋支持,以顯示哪些代碼行已經測試,哪些沒有。它還將包括項目的測試覆蓋率。

  3.pytest高級用法

  測試用例的執行之前而初始化一些數據及方法,相當於Unittest中的setUp,tearDown

  3.1 fixture參數

  fixture默認參數爲function級別的,function:每一個函數或方法都會調用

  import pytest

  @pytest.fixture()

  def loginlogout():

  print("Before")

  yield

  print("After")

  class TestFixture():

  def test_fixture(self,loginlogout):

  assert add.add_test(2,3)==5

  def test_fixture2(self,loginlogout):

  assert add.add_test(5,3)==8

  class TestFixture1():

  def test_fixture3(self,loginlogout):

  assert add.add_test(2,3)==5

  def test_fixture4(self,loginlogout):

  assert add.add_test(5,3)==8

  以上結果執行4次loginlogout,默認爲function級別,即:每個函數或方法執行一次

  3.2 fixture的scope用法

  使用scope設置級別,scope有以下級別:

  function:每一個函數或方法都會調用

  class:每一個類調用一次,一個類可以有多個方法

  module:每一個.py文件調用一次,該文件內又有多個function和class

  session:是多個文件調用一次,可以跨.py文件調用,每個.py文件就是module

  3.2.1 funtion級別

  作用範圍是每個測試用例來之前運行一次

  test_confest.py

  @pytest.fixture()

  def conftest():

  print("conftest")

  def test_confest1(conftest):

  print("conftest1")

  assert 1==1

  def test_confest2(conftest):

  print("conftest2")

  assert 2==2

  def test_confest3():

  assert 2 == 2

  if __name__ == "__main__":

  pytest.main(["-s", "test_confest.py"])

  以上結果,打印了兩個conftest,原因是兩個方法設置了fixture,默認爲function

  test_confest.py conftest

  .conftest1

  conftest

  .conftest2

  3.2.2 class級別

  如果一個class裏面有多個用例,都調用了此fixture,那麼此fixture只在該class裏所有用例開始前執行一次

  test_confest.py

  @pytest.fixture(scope="class")

  def conftest():

  print("conftest")

  class TestConftest():

  def test_confest1(self,conftest):

  print("conftest1")

  assert 1==1

  def test_confest2(self,conftest):

  print("conftest2")

  assert 2==2

  def test_confest3(self,conftest):

  assert 2==2

  if __name__ == "__main__":

  pytest.main(["-s", "test_confest.py"])

  以上結果爲執行一次confest,原因是scope設置爲class,而其中只有一個class,故只有一個confest

  3.2.3 module級別

  當前.py腳本里面所有用例開始前只執行一次

  @pytest.fixture(scope="module")

  def conftest():

  print("conftest")

  def test_confest1(conftest):

  print("conftest1")

  assert 1==1

  class TestConftest():

  def test_confest2(self,conftest):

  print("conftest2")

  assert 1==1

  if __name__ == "__main__":

  pytest.main(["-s", "test_confest.py"])

  測試結果:

  test_confest.py conftest

  .conftest1

  .conftest2

  3.2.4 session級別

  fixture爲session級別是可以跨.py模塊調用的,也就是當我們有多個.py文件的用例時候,如果多個用例只需調用一次fixture,那就可以設置爲scope=“session”,並且寫到conftest.py文件裏

  conftest.py

  import pytest

  @pytest.fixture(scope="session")

  def first():

  print("session級別,每一個py文件執行一次")

  return 2

  test_conftest1.py

  import pytest

  def test_conftest1(first):

  '''用例傳fixture'''

  print("test_conftest1=%d" % first)

  assert 1 == 1

  if __name__ == "__main__":

  pytest.main(["-s", "test_conftest1.py"])

  test_conftest2.py

  import pytest

  def test_conftest_demo1(first):

  '''用例傳fixture'''

  print("test_conftest_demo1=%d" % first)

  assert 1 == 1

  def test_conftest_demo2(first):

  '''用例傳fixture'''

  print("test_conftest2_demo2=%d" % first)

  assert 1 == 1

  if __name__ == "__main__":

  pytest.main(["-s", "test_conftest2.py"])

  結果如下:無錫婦科醫院排行 http://www.0510bhyy.com/

  test_conftest1.py session級別,每一個py文件執行一次

  .test_conftest1=2

  test_conftest2.py session級別,每一個py文件執行一次

  .test_conftest_demo1=2

  .test_conftest2_demo2=2

  3.2.5 conftest

  conftest.py文件名稱是固定的,pytest會自動識別該文件。放到工程的根目錄下,就可以全局調用了,如果放到某個package包下,那就只在該package內有效

  3.3 fixture的usefixtures用法

  將定義在函數裏面的放在外面,使用usefixtures

  import pytest

  @pytest.fixture(scope="class")

  def loginlogout():

  print("Before")

  yield

  print("After")

  class TestFixture():

  @pytest.mark.usefixtures("loginlogout")

  def test_fixture(self):

  assert add.add_test(2,3)==5

  @pytest.mark.usefixtures("loginlogout")

  def test_fixture2(self):

  assert add.add_test(5,3)==8

  3.4 fixture的autouse用法

  使用autouse設置是否自動使用fixtures

  import pytest

  pytest.fixture(scope="class",autouse=True)

  def loginlWogout():

  print("Before")

  yield

  print("After")

  class TestFixture():

  def test_fixture(self):

  assert add.add_test(2,3)==5

  def test_fixture2(self):

  assert add.add_test(5,3)==8


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章