无码人妻A片一区二区三区_18禁裸乳无遮挡啪啪无码免费_91精品亚?影视在线?看_人人妻人人爽人人澡AV_国产精品人妻一区二区三区四区_午夜免费影视

中培偉業IT資訊頻道
您現在的位置:首頁 > IT資訊 > 軟件研發 > 詳解Python中的Pytest使用說明

詳解Python中的Pytest使用說明

2020-07-15 14:28:48 | 來源:中培企業IT培訓網

對于編程人員來說,Python是編程語言是編程行業的入門基礎。而在Python測試框架中Pytest不僅簡單靈活,而且還是新手的快速入門。Pytest還具有許多第三方插件,功能非常強大。因此,有的人說Pytest是測試行業從業人員的必備工具。本文將詳解Python中的Pytest使用說明,包括Pytest安裝,基本操作,運行時設置和參數化。

  1、安裝

  (1)全局安裝

使用pip 進行安裝

pip install -U pytest

檢查安裝版本

$ pip install -U pytest

This is pytest version 4.4.0, imported from xxxx

  (2)項目目錄下安裝

如果只是將pytest安裝到當前項目中,不與其它的版本混淆,使用 virtualenv進行安裝

mkdir pytest_project

cd pytest_project

virtualenv .venv

這將會在項目目錄下創建pytest-env目錄存放虛擬環境。

激活虛擬環境

source .venv/bin/activate

再次使用pip進行安裝,安裝文件將存放在當前項目目錄下,而不再是全局環境變量下

$ pip install pytest

(3)區別

全局安裝方式適合所有項目,在項目目錄下虛擬化安裝只適合當前項目。

  2、基本操作

基本使用方式:我們將從一個簡單的測試開始,Pytest命名規范文件名以test_開頭或以_test.py結尾。首先創建一個名為test_capitalize.py的文件,在此文件中編寫一個名為capital_case的函數,以字符串作為參數,將參數轉化為大寫返回。另外編寫一個test_capital_case參數函數,主要驗證capital_case函數完成它所說的內容,我們用test_作為測試函數名稱的前綴。

# test_capitalize.py

def capital_case(x):

return x.capitalize()

def test_capital_case():

assert capital_case('semaphore') == 'Semaphore'

在命令行運行 pytest , 將自動執行 test_capitalize.py 文件中的 test_capital_case 測試方法;

collected 1 item

test_capitalize.py . [100%]

========================================= 1 passed in 0.01 seconds ==========================================

測試用例執行通過。

  3、Pytest 運行時設置

  (1)xunit 格式

函數級別設置運行時

setup_module

setup

teardown

teardown_module

如下代碼

def setup_module():

print("module --setup-- ")

def teardown_module():

print('module --teardown--')

def setup():

print("function --setup--")

def teardown():

print("function --teardown--")

def test_01():

print("---test01---")def test_02(): print('-----test02------')

運行文件 pytest -s -v tmp.py

testcases/tmp.py::test_01

module --setup--

function --setup

-----test01---

PASSED function --teardown--

testcases/tmp.py::test_02

function --setup--

-----test02------

PASSED

function --teardown--

module --teardown--

Class 類級別

tmp2.py

class TestTmp2:

@classmethod

def setup_class(cls):

print('- class setup -')

@classmethod

def teardown_class(cls):

print('- class teardown - ')

def setup(self):

print('- method setup -')

def teardown(self):

print("- method teardown -")

def test_01(self):

print("-- test01 --")

def test_02(self):

print('-- test02 --')

pytest -s -v tmp2.py

tmp2.py::TestTmp2::test_01 - class setup

-- method setup -

-- test01 --

PASSED- method teardown -

testcases/tmp/tmp2.py::TestTmp2::test_02 - method setup -

-- test02 --

PASSED- method teardown -

- class teardown -

  (2)fixture

  函數級別

tmp.py

import pytest

@pytest.fixture(scope='module')

def fix_module():

print('-- module setup --')

yield

print('-- module teardown --')

@pytest.fixture()def fix_func(fix_module):

print('-- func setup --')

yield

print('-- func teardown --')

@pytest.fixture()def fix_func2():

print('-- func2 setup --')

yield

print('-- func2 teardown --')

def test_01(fix_func):

print('-- test 01 --')

def test_02(fix_func2):

print('-- test 02 --')

scope="module", module 級別

yeild 關鍵字

class 類級別

import pytest

@pytest.fixture(scope='module')

def fix_module():

print('-- module setup --')

yield

print('-- module teardown --')

class TestTmp2:

@pytest.fixture()

def fix_func(self, fix_module):

print('-- func setup --')

yield

print('-- func teardown --')

def test_01(self,fix_func):

print('-- test 01 --')

def test_02(self,fix_func):

print('-- test 02 --')

pytes -s -v tmp2.py

tmp2.py::TestTmp2::test_01 -- module setup --

-- func setup --

-- test 01 --

PASSED-- func teardown --

tmp2.py::TestTmp2::test_02 -- func setup --

-- test 02 --

PASSED-- func teardown --

-- module teardown --

tmp2.py

import pytest

@pytest.fixture(scope='module')

def fix_module():

print('-- module setup --')

yield

print('-- module teardown --')

@pytest.fixture(scope='session')

def fix_session():

print('-- session set up --')

yield

print('-- session teardown --')

@pytest.fixture(scope='class')

def fix_class():

print('-- class set up --')

yield

print('-- class tear down --')

@pytest.fixture(scope='function')

def fix_function():

print('-- function set up --')

yield

print('-- function tear down --')

@pytest.mark.usefixtures('fix_session','fix_module','fix_class' ,'fix_function')

class TestTmp2:

def test_01(self):

print('-- test 01 --')

def test_02(self):

print('-- test 02 --')

● session: 所有

● module: 整個文件

● class:類

● function:方法

testcases/testfixture/tmp2.py::TestTmp2::test_01

-- session set up --

-- module setup --

-- class set up --

-- function set up --

-- test 01 --

PASSED-- function tear down --

testcases/testfixture/tmp2.py::TestTmp2::test_02

-- function set up --

-- test 02 --

PASSED-- function tear down --

-- class tear down --

-- module teardown --

-- session teardown --

conftest.py 多個文件共享

  4、參數化

  (1)mark.parametrize

import pytest

a = [

('share','title1','conent1'),

('share','title2','conent2'),

]

@pytest.mark.parametrize('tab,title,content',a)

def test_a(tab,title,content):

print('----',tab,title,content,'-----')

  (2)fixture 級別的參數化

import pytest

@pytest.fixture(params=[0,1],ids=['spam','ham'])

def a(request):

return request.param

def test_a(a):

print(f'--{a}--')

# assert a

def test_b(a):

print(f'=={a}==')

以上就是Python中的Pytest使用說明的全部內容,如果想了解更多關于Python的信息,請繼續關注中培偉業。

標簽: Python Pytest
主站蜘蛛池模板: 极品少妇XXXXⅩ另类 | 国产在线精品区 | 国内精品美女A∨在线播放 激情婷婷丁香五月色综合 精品推荐国产精品店 | 未成年人在线观看 | 亚洲欧洲一二区 | A级毛片在线视频免费观看 土豆成人网 | 欧洲高清转码区一二区 | 精品欧美一区二区三区在线观看 | 日韩专区视频 | 年轻的朋友3中文 | 午夜中文字幕hd无码无删减 | 亚洲va欧美va国产综合先锋 | 三级超碰 | 国产精品呻吟高潮 | 国产色综合久久无码有码 | 一区二区三区成人在线视频 | 成人无遮挡裸免费视频在线观看 | 亚洲精品区无码欧美日韩 | 91?看视频| 少妇高潮无套内谢 | 国产精品wwww | 大屁股大乳丰满人妻 | 一本色道久久综合狠狠躁篇 | 一区二区国产精品精华液 | 日韩精品一区二区午夜成人版 | 无码被窝影院午夜看片爽爽JK | 亚洲av产在线精品亚洲第一站 | 欧美激情区 | 国产真实偷乱视频在线观看 | 国产九九九 | 狠狠躁18三区二区一区AI明星 | 精品久久久久久久无码 | 色婷婷成人做爰视频免费 | 草色在线 | 国产欧美精品区一区二区三区 | 变态视频在线观看 | 亚洲天堂一区二区三区四区 | 91在线入口 | 依人九九宗合九九九 | 亚洲欧美中文日韩在线视频 | 大肉大捧一进一出好爽视色大师 |