其他分享
首页 > 其他分享> > 【pytest】fixture基本用法(参数解释及示例)

【pytest】fixture基本用法(参数解释及示例)

作者:互联网

@pytest.fixture(scope='function',params=params, autouse=autouse, ids=ids, name=name)
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
#arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
#arg autouse:  如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
#arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
#arg name:   fixture的名称。 这默认为装饰函数的名称。
#conftest.py
import pytest
@pytest.fixture(scope='function')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

    def test_three(self):
        print('Test_class-->>test three')
#conftest.py
import pytest
@pytest.fixture(scope='class')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

    def test_three(self):
        print('Test_class-->>test three')
#conftest.py
import pytest
@pytest.fixture(scope='module')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

class Testdemo:
    def test_one(self):
        print('Test_demo-->>test three')
#conftest.py
import pytest
@pytest.fixture(scope='session')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

class Testdemo:
    def test_one(self):
        print('Test_demo-->>test three')

#test_sample.py
import pytest
class Testsample:
    def test_one(self,my_fixture):
        print('Test_sample-->>test one')

    def test_two(self):
        print('Test_sample-->>test two')
#conftest.py
import pytest
param = [1,22,333]
@pytest.fixture(params=param)
def my_fixture(request):   #固定参数名
    #  return request.param   #固定返回值
    print('fixture---前置')
    yield request.param    
    print('fixture---后置')

##test_demo.py
class Testdemo:
    def test_one(self,my_fixture):
        print(my_fixture)
        print('Test_demo-->>test three')

标签:示例,fixture,print,pytest,test,class,def
来源: https://www.cnblogs.com/xwltest/p/16552715.html