网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Pytest如何快速入门

Pytest如何快速入门?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

目前累计服务客户上千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供网站设计制作、成都做网站、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。成都创新互联公司始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。

 

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、基本操作

 

(1)主要内容

 

基本使用方式:我们将从一个简单的测试开始,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}==')

关于Pytest如何快速入门问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


本文名称:Pytest如何快速入门
标题URL:http://bjjierui.cn/article/gjepdd.html

其他资讯