pytest--fixture

Foreplay

fixture was run before and after the test function, the function performed by the housing pytest. fixture code can be customized to meet the changing requirements of the test, including the definition of the incoming set of test data, arranged before the initial state of the test system, the test batch data source for providing the like.

Here is a simple fixture

import pytest


@pytest.fixture()
def some_data():
    return 37


def test_some_data(some_data):
    assert some_data == 37

Let's run it

@ Pytest.fixture () decorator is used to declare a function is a fixture, if the test function parameter list contains fixture name, then pytest detects and executes the fixture before the test function runs. fixture can complete the task, you can return data to test the function.

Test test_some_data () parameter list contains a fixture name some_data, pytest will fixture to the name search. pytest will first search the test module is located, then search conftest.py

Recommendation: Do not be a function of decorative fixture to test name, such as the above named some_data, rather than test_data. Of course, so named not wrong, just for convenience or to distinguish a fixture test (function)

conftest.py

The test fixture can be placed in a separate file, if you want to test multiple file sharing fixture, conftest.py can create a new file in a public directory, in which the fixture.

If you want fixture scope is limited to a test file, then write it in the test file. Where you can test for use in the directory and its subdirectories.

Description:

Although conftest.py is a python module, but it can not be tested when the file is imported, import conftest usage is not allowed to occur.

We will change the above code

conftest.py

import pytest


@pytest.fixture()
def some_data():
    return 37

test_fixture.py

def test_some_data(some_data):
    assert some_data == 37

When pytest execution test_some_data function will automatically go conftest.py where to find some_data and execute, we use cases such as this, some need to log in, some do not need to log on, we can use cases need to log in to add a fixture, of course, fixture can custom range, a function, or a class or a module, will be introduced later.

fixture configuration and destruction yield

 fixture will run before the test function, but if the fixture functions include yield, then the system will stop at the yield point in favor of the implementation of test functions, such as test function is finished and then return fixture, continue with the last yield. Therefore, the yield before the code can be viewed as the configuration (setup) procedure, the code will be deemed to clean up after the yield (teardown) process, no matter what happens during the test, the code will be executed after the yield.

Conftest.py in the above code change it

Import pytest 


@ pytest.fixture () 
DEF some_data ():
     the yield 37 [
     Print ( ' end of the test ' )

--setup-show

The above test, we will not see the process of implementation of the fixture. If you want to see what the testing process is performed, and the execution of the order, --setup-show option pytest provide this function can be achieved

Our test is sandwiched, pytest will perform each and TEARDOWN SETUP fixture into two portions.

F represents the front of the name fixture is a fixture scope, the function F represents the level range of action, if it is S, then represents the session level scope.

Scope specified fixture

 a fixture comprising optional parameter called scope (scope) for controlling the frequency of the fixture to perform configuration and logical destruction. @ Pytest.fixture () with a scope parameter is selected from four values: function, class, module, session (default value function). Used earlier fixture did not specify the scope, so their scope is the default function level.

scope=“function”

Function-level fixture for each test function needs to be run only once, configuration code runs before the test run, the destruction of the code runs after the test run. function parameter is the default scope

 conftest.py

Import pytest 


@ pytest.fixture (scope = " function " )   # function-level
 DEF the Login ():
     Print ( ' Login successful ' )

 

test_fixture.py

DEF test_index (Login):
     Print ( ' Access index ' ) 


DEF test_home (Login):
     Print ( ' Access Home ' )

The results can be seen, before we perform every function performed fixture

scope=“class”

Class level for each test fixture class only needs to run once, no matter how many class methods in the test class can share this fixture

conftest.py

import pytest


@pytest.fixture(scope="class")  # 类级别
def login():
    print('登录成功')

test_fixture.py

class Test1():
    def test_index(self,login):
        print('访问index1')

    def test_home(self,login):
        print('访问home1')

class Test2():
    def test_index(self,login):
        print('访问index2')

    def test_home(self,login):
        print('访问home2')

 

scope=“module”

 模块级别的fixture,每个模块只需要运行一次,无论模块里有多少个测试函数、类方法或者其他fixture都可以共享这个fixture

只需要修改conftest.py

import pytest


@pytest.fixture(scope="module")  # 模块级别
def login():
    print('登录成功')

 

scope=“session”

会话级别的fixture,每次会话只需要运行一次,所有测试函数、方法都可以共享这个fixture

 我们把上面的test_fixture.py在复制一份,改名为test_fixture2,这样我们在testpytest下就有两个test文件了

 

如果有两个文件,把session改为module,则会执行两次,因为有两个py文件

 使用usefixtures指定fixture

目前为止用到fixture的测试,都是在测试函数的参数列表中指定fixture,实际上,也可以用@pytest.mark.usefixtures('fixture1', 'fixture2')标记测试函数或类。使用usefixtures,需要在参数列表中指定一个或多个fixture字符串。

使用usefixture和在测试方法中添加fixture参数,二者本体上是差不多的,区别之一在与后者才能使用fixture的返回值

import pytest


@pytest.fixture(scope="function")
def login():
    print('登录成功')
conftest.py
import pytest


@pytest.mark.usefixtures('login')
def test_index():
    print('访问index1')


def test_home():
    print('访问home1')
test_fixture2.py

如果是类值需要在类名上面加上fixture

import pytest


@pytest.mark.usefixtures('login')
class Test1():
    def test_index(self):
        print('访问index1')

    def test_home(self):
        print('访问home1')


@pytest.mark.usefixtures('login')
class Test2():
    def test_index(self):
        print('访问index2')

    def test_home(self):
        print('访问home2')
类的fixture
@pytest.mark.usefixtures("cleandir", "anotherfixture")指定多个fixture

autouse

不想源测试数据有任何改动,或全部都实现自动应用, 没特例,也都不需要返回值时可以选择自动应用

使用fixture中参数autouse=True实现

import pytest


@pytest.fixture(autouse=True)
def login():
    print('登录成功')
conftest.py
class Test1():
    def test_index(self):
        print('访问index1')

    def test_home(self):
        print('访问home1')


class Test2():
    def test_index(self):
        print('访问index2')

    def test_home(self):
        print('访问home2')
test_fixture.py
def test_index():
    print('访问index1')


def test_home():
    print('访问home1')
test_fixture2.py

可以看到,我们加了autouse=True之后,会给所有的方法都应用,不用再每个方法都使用fixture

 如果级别是class的话,则有class的只会执行一次

为fixture重命名

 fixture的名字展示在使用它的测试或其他fixture函数的参数列表上,通常会和fixture函数名保持一致,但pytest允许使用@pytest.fixture()的name参数对fixture重命名

conftest.py

import pytest


@pytest.fixture(name='login')  # 重命名为login
def alibaba_taobao_app_login():
    print('登录成功')

test_fixture2.py

def test_index(login):
    print('访问index1')


def test_home(login):
    print('访问home1')

这个fixture原来的名字是 alibaba_taobao_app_login ,重命名后变成了login

 查看fixture

 我们可以使用--fixtures命令行选项,并提供所在测试文件名。pytest将列举所有可供测试使用的fixture,包括重命名的。我们自己重新定义的会出现在底部

 

Guess you like

Origin www.cnblogs.com/zouzou-busy/p/11298879.html