1. 程式人生 > 實用技巧 >pytest基礎應用

pytest基礎應用

一、pytest簡介

pytest 是 python 的第三方單元測試框架,比自帶的 unittest 更簡潔和高效,同時相容 unittest 框架。

它還有如下優點:

1、簡單靈活,容易上手,文件豐富;

2、支援引數化,可以細粒度地控制要測試的測試用例;

3、能夠支援簡單的單元測試和複雜的功能測試,還可以用來做 selenium/appnium等自動化測試、介面自動化測試(pytest+requests);

4、pytest具有很多第三方外掛,並且可以自定義擴充套件,比較好用的如pytest- selenium(整合selenium)、pytest-html(完美html測試報告生成)、 pytest-rerunfailures(失敗case重複執行)、pytest-xdist(多CPU分發)等

5、測試用例的skip和xfail處理;

6、可以很好的和CI工具結合,例如jenkins。

pytest是python的一個第三方類庫,可以直接使用pip install pytest 線上 安裝,或者下載離線包安裝。

二、pytest測試用例編寫規則

1、測試檔案以test_開頭(以_test結尾也可以)

2、測試類以Test開頭,並且不能帶有 init 方法

3、測試函式以test_開頭

4、斷言必須使用 assert

舉例:

import pytest

class TestDemo01:
    def testadd(self):
        assert 10+10 == 20

if
__name__ == '__main__': pytest.main()
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /appium_test/pytest
plugins: Faker-4.1.1, celery-4.2.0
collected 1 item

test_demo_01.py .                                                        [
100%] ============================== 1 passed in 0.06s ===============================
test_demo_01.py 為檔名

三、pytest中的fixture

  fixture是pytest特有的功能,它用pytest.fixture標識,定義在函式前面。 fixture有明確的名字,在其他函式,模組,類或整個工程呼叫它時會被啟用。 fixture是基於模組來執行的,每個fixture的名字就可以觸發一

fixture的函式, 它自身也可以呼叫其他的fixture。

可以把fixture看做是資源,在你的測試用例執行之前需要去配置這些資源, 執行完後需要去釋放資源。類似unittest中的setup和teardown功能。

四、fixture方法詳解

fixture(callable_or_scope=None,*args,scope="function",params=None,aut ouse=False,ids=None,name=None)

scope: 作用範圍,設定範圍後,會根據設定的範圍去觸發執行。

範圍可選值:

  • function:每個方法(函式)都會執行一次。(預設)
  • class:每個類都會執行一次。類中有多個方法呼叫,只在第一個方法呼叫時執行
  • module:一個 .py 檔案執行一次。一個.py 檔案可能包含多個類和方法。
  • package/session:多個檔案呼叫一次,可以跨 .py 檔案。

function舉例:

import pytest

@pytest.fixture(scope='function')
def setUp():
    print('測試初始化')
    yield
    print('測試清理')

class TestDemo01:
    def testadd(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 20-10 == 10

if __name__ == '__main__':
    pytest.main(['-s'])
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /appium_test/pytest
plugins: Faker-4.1.1, celery-4.2.0
collected 2 items

test_demo_02.py 測試初始化
test_demo_02 TestDemo01 testadd
.測試清理
測試初始化
test_demo_02 TestDemo01 testadd
.測試清理


============================== 2 passed in 0.05s ===============================

class舉例:

import pytest

@pytest.fixture(scope='class')
def setUp():
    print('測試初始化')
    yield
    print('測試清理')

class TestDemo01:
    def testadd(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 20-10 == 10

class TestDemo02:
    def testadd(self,setUp):
        print('test_demo_02 TestDemo02 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_02 TestDemo02 testadd')
        assert 20-10 == 10

if __name__ == '__main__':
    pytest.main(['-s'])
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /appium_test/pytest
plugins: Faker-4.1.1, celery-4.2.0
collected 4 items

test_demo_03.py 測試初始化
test_demo_02 TestDemo01 testadd
.test_demo_02 TestDemo01 testadd
.測試清理
測試初始化
test_demo_02 TestDemo02 testadd
.test_demo_02 TestDemo02 testadd
.測試清理


============================== 4 passed in 0.06s ===============================

Process finished with exit code 0

module舉例:

import pytest

@pytest.fixture(scope='module')
def setUp():
    print('測試初始化')
    yield
    print('測試清理')

class TestDemo01:
    def testadd(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_02 TestDemo01 testadd')
        assert 20-10 == 10

class TestDemo02:
    def testadd(self,setUp):
        print('test_demo_02 TestDemo02 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_02 TestDemo02 testadd')
        assert 20-10 == 10

if __name__ == '__main__':
    pytest.main(['-s'])
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /appium_test/pytest
plugins: Faker-4.1.1, celery-4.2.0
collected 4 items

test_demo_03.py 測試初始化
test_demo_02 TestDemo01 testadd
.test_demo_02 TestDemo01 testadd
.test_demo_02 TestDemo02 testadd
.test_demo_02 TestDemo02 testadd
.測試清理


============================== 4 passed in 0.06s ===============================

package/session舉例:

檔案一:test_demo_03.py
import pytest

@pytest.fixture(scope='session')
def setUp():
    print('測試初始化')
    yield
    print('測試清理')

class TestDemo01:
    def testadd(self,setUp):
        print('test_demo_03 TestDemo01 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_03 TestDemo01 testadd')
        assert 20-10 == 10

class TestDemo02:
    def testadd(self,setUp):
        print('test_demo_03 TestDemo02 testadd')
        assert 10+10 == 20

    def testsub(self,setUp):
        print('test_demo_03 TestDemo02 testadd')
        assert 20-10 == 10

if __name__ == '__main__':
    pytest.main(['-s'])
檔案二:test_demo_04.py
import pytest

class TestDemo01:
    def testadd(self):
        print('test_demo_04 TestDemo01 testadd')
        assert 10+10 == 20

    def testsub(self):
        print('test_demo_04 TestDemo01 testadd')
        assert 20-10 == 10

class TestDemo02:
    def testadd(self):
        print('test_demo_04 TestDemo02 testadd')
        assert 10+10 == 20

    def testsub(self):
        print('test_demo_04 TestDemo02 testadd')
        assert 20-10 == 10

if __name__ == '__main__':
    pytest.main(['-s'])

執行test_demo_03.py或test_demo_04.py

============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /appium_test/pytest
plugins: Faker-4.1.1, celery-4.2.0
collected 8 items

test_demo_03.py 測試初始化
test_demo_03 TestDemo01 testadd
.test_demo_03 TestDemo01 testadd
.test_demo_03 TestDemo02 testadd
.test_demo_03 TestDemo02 testadd
.
test_demo_04.py test_demo_04 TestDemo01 testadd
.test_demo_04 TestDemo01 testadd
.test_demo_04 TestDemo02 testadd
.test_demo_04 TestDemo02 testadd
.測試清理


============================== 8 passed in 0.06s ===============================