pytest fixture of the pre- and post-function

A, fixture (translation: Fix here) of conftest.py (translation: ConocoPhillips Teste) file

  • When defining fixture, necessary to introduce import pytest
  • Define a common fixture: pre + post, a plurality of test classes can be called
  • pytest provided conftest.py file can fixture: pre + post, defined in this document
  • When you run the test, do not need to import this file, it will automatically go to find conftest.py (translation: ConocoPhillips Teste) the file, then go find the corresponding
  • Share files: conftest.py file name can not be changed; because, no need to introduce fixture which can be used

Second, create conftest.py file that defines the front + rear

  The yield keyword fixture function as the front and rear dividing line, and the yield may also receive return value returned tuples, acts as return

  yield (translation: UTE): dividing line, front return result

Copy the code
pytest Import 

@ pytest.fixture () 
DEF init_demo (): 
    Print ( "It is the pre-test") 
    A. 1 = 
    the yield # A parting line (yield + return value) 
    Print ( "It is the post-test." )
Copy the code

Third, call the fixture

1. Direct calls in test case

  The fixtures function name as the test case parameters

  If the fixture has a return value, the function name test fixture on the reception of the return value, the function name and the fixture can be used as the return value

def test_add_01(init_demo):
    b = init_demo + 2
    assert 3 == b

  operation result:

2. Call the fixture with the fixture decorator

  In front of the test / test class plus @ pytest.mark.usefixtures ( 'fixture function name')

  ps: definitions conftest.py file, in this file can define multiple fixture, pytest will automatically search for the file

Copy the code
import pytest


@pytest.mark.usefixtures('init_demo')
def test_add_02():
    b = 1 + 2
    assert 3 == b
Copy the code

  operation result:

3. autouse calls fixture

  • When defining fixture, there is a parameter autouse, the default setting to False
  • When the default is False, you can be selected by using the above two ways fixture
  • When set to Ture, in all the test will automatically call a session of the fixture, recommended that the switch be used with caution

  conftest code is as follows:

Copy the code
pytest Import 

@ pytest.fixture (autouse = Ture) 
DEF init_demo (): 
    Print ( "It is the pre-test") 
    the yield 
    Print ( "It was the post-test")
Copy the code

  Test code is as follows:

import pytest


def test_add_02():
    b = 1 + 2
    assert 3 == b

  operation result:

Four, fixture inheritance (the front of the front, rear, the rear)

  • Scope (scope keyword): function / function level (test case), class / class-level (test class), module / module level (test module -py files), session / session-level (test execution throughout the session)
  • Metaphor: biscuit
  • Inherited conditions: Scope Scope inherited from the inside out, you can also inherit the same level of scope
  • Execution order: pre-executed from the outer to the inner layer, the inner layer to the outer rear executed, perform the scope of the innermost layer, a layer in performing its scope, could not find an outward until until the domain
  • Inherited methods: directly to the function name as an inherited fixture can be passed to the Senate
  • Returns: inherit the parent class, but also inherited the return value of the parent class

  conftest.py code is as follows:

Copy the code
pytest Import 


@ pytest.fixture (scope = 'the session') 
DEF init_session (): 
    Print ( "It is a pre-test session") 
    the yield 
    Print ( "It is the post-test session") 


@ pytest.fixture (scope = 'Module1') 
DEF the init_module (init_session): 
    Print ( "it is a pre-test module") 
    the yield 
    Print ( "it is a post-test module") 


@ pytest.fixture (scope = 'class') 
DEF init_class (the init_module ): 
    Print ( "it is a pre-test class") 
    the yield 
    Print ( "it is a post test class") 


@ # pytest.fixture pytest.fixture corresponds @ (scope = 'function') 
DEF init_function (init_class) : 
    Print ( "this is a test case of the front") 
    yield 
    Print ( "this is the post-test case")
Copy the code

  Test code is as follows:

Copy the code
import pytest


@pytest.mark.usefixtures('init_function')
def test_demo():
    print('测试用例')
    assert 3 == 3
Copy the code

  operation result:

 Five levels of scope

Use fixture of the order - which subdirectories, you can also have their own conftest

  • First with his own, then go conftest.py in the same directory and then go to the next level directory on conftest.py

 

Guess you like

Origin www.cnblogs.com/wuzm/p/12591680.html