Free性欧美Hd另类_精品亚洲欧美视频在线观看_freesex欧美喷水_字幕av在线_久久久久久国产免费_伊人5566

中培偉業(yè)IT資訊頻道
您現(xiàn)在的位置:首頁(yè) > IT資訊 > 軟件研發(fā) > 詳解Python中的Pytest使用說明

詳解Python中的Pytest使用說明

2020-07-15 14:28:48 | 來源:中培企業(yè)IT培訓(xùn)網(wǎng)

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

  1、安裝

 ?。?)全局安裝

使用pip 進(jìn)行安裝

pip install -U pytest

檢查安裝版本

$ pip install -U pytest

This is pytest version 4.4.0, imported from xxxx

 ?。?)項(xiàng)目目錄下安裝

如果只是將pytest安裝到當(dāng)前項(xiàng)目中,不與其它的版本混淆,使用 virtualenv進(jìn)行安裝

mkdir pytest_project

cd pytest_project

virtualenv .venv

這將會(huì)在項(xiàng)目目錄下創(chuàng)建pytest-env目錄存放虛擬環(huán)境。

激活虛擬環(huán)境

source .venv/bin/activate

再次使用pip進(jìn)行安裝,安裝文件將存放在當(dāng)前項(xiàng)目目錄下,而不再是全局環(huán)境變量下

$ pip install pytest

(3)區(qū)別

全局安裝方式適合所有項(xiàng)目,在項(xiàng)目目錄下虛擬化安裝只適合當(dāng)前項(xiàng)目。

  2、基本操作

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

# test_capitalize.py

def capital_case(x):

return x.capitalize()

def test_capital_case():

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

在命令行運(yùn)行 pytest , 將自動(dòng)執(zhí)行 test_capitalize.py 文件中的 test_capital_case 測(cè)試方法;

collected 1 item

test_capitalize.py . [100%]

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

測(cè)試用例執(zhí)行通過。

  3、Pytest 運(yùn)行時(shí)設(shè)置

 ?。?)xunit 格式

函數(shù)級(jí)別設(shè)置運(yùn)行時(shí)

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------')

運(yùn)行文件 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 類級(jí)別

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

  函數(shù)級(jí)別

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 級(jí)別

yeild 關(guān)鍵字

class 類級(jí)別

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: 整個(gè)文件

● 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 多個(gè)文件共享

  4、參數(shù)化

 ?。?)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 級(jí)別的參數(shù)化

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使用說明的全部?jī)?nèi)容,如果想了解更多關(guān)于Python的信息,請(qǐng)繼續(xù)關(guān)注中培偉業(yè)。

標(biāo)簽: Python Pytest
主站蜘蛛池模板: 忘忧草日本社区 | 国产精品亚洲片牛牛 | 国产精品成年人 | 久久99亚洲精品久久 | 亚洲不卡一卡2卡三卡4卡5卡 | 国产亚洲99影院 | 再深点灬舒服灬太大的91优势 | 亚洲精品久久AV无码蜜桃第1集 | 啪一啪免费视频 | 色综合激情无码中文字幕 | 亚洲精品久久久久久下一站 | 老外黄色一级片 | 久久视频这里只精品3国产 亚洲欧美日本在线观看 | 日本美女视频有色 | 鲁一鲁一鲁一鲁一曰综合网 | 日本在线不卡视频 | 毛片免费视频播放 | 欧美久久久久久久久久久久久久 | 婷婷久久综合九色综合97最多收藏 | 亚洲欧美又粗又长久久久 | 无遮掩无码h成人av动漫 | 国产精品偷伦小说 | 五月丁香 | 国产a级黄色片 | 一级一级一级毛片 | 无码H肉3D动漫在线观看 | 久久人人爽人人爽人人片AV麻烦 | 91华人在线?看 | 精品乱码一区二区三四区 | 中文字幕无码专区人妻系列 | 影音先锋aⅴ男人资源先锋影院 | 91在线观看免费视频 | 少妇aaa片| 夫妻的情妇在线观看 | 国产精品久久久久久久白皙女 | 久久久精品视频网站 | 精品国产成人亚洲午夜福利 | 亚洲欧美中文日韩在线 | 欧美大胆a视频 | 韩国av在线 | 小13箩利洗澡无码视频免费网站 |