7-conftest.py and fixture

Article Directory

一, conftest.py

conftest.py is a configuration file dedicated to storing fixtures. You can put some general pre-operations in it, such as login, get some parameters, etc.

Note:
1. The name must be conftest.py
2. Scope: only works for the current package
3. When there are multiple conftest.py in the project
, it will take effect from outside to inside and from top to bottom. 4. Pytest will execute conftest by default Fixture in .py
5. When executing test cases, conftest.py can be used directly without manual import

二、fixture

Fixtures can be run before certain specific use cases are executed, which is more flexible than setup/teardown in unittest.
1. Detailed explanation of fixture source code

fixture(scope=‘function’,params=None,autouse=False,ids=None,name=None)

① Scope: There are four levels of parameters, namely:

  1. function: the default value , under the scope of conftest, each test method will be executed once before running
  2. class: In the scope of conftest, each test class will be executed once before running
  3. module: In the scope of conftest, each test module will be executed once before running
  4. session: Under the scope of conftest, each package will be executed once before running

The scope parameter scope controls the scope of the fixture: session>module>class>function

② params: optional parameter list, it will cause multiple parameters to call the fixture function and all tests use it

③ autouse: The default is False , and the fixture needs to be manually invoked by the use case; if it is True, all test cases in the scope will automatically invoke the fixture

④ ids: part of the params test ID. If not, it will be automatically generated from params

⑤ name: Default: the name of the decorator. It is recommended to write a different name when the fixtures of the same module call each other.


2. Use skills:

① Use the function name to call directly, but there is no return value

#conftest.py文件
import pytest
@pytest.fixture()
def login():
    print('登录')

#test.py文件
import pytest
@pytest.mark.usefixtures('login')
def test1():
    print("test1里的用例")
 
if __name__ == '__main__':
    pytest.main(["-s", "test.py"])

Results of the:

test.py 登录
test1里的用例
.

② The function has a return value, and the function name needs to be passed in as a parameter

#conftest.py文件
import pytest
@pytest.fixture()
def login():
    a = 1
    b = 2
    return a,b

#test.py文件
import pytest
def test1(login):
    print(login[0])  #返回login函数第一个值
    print("test1里的用例")
 
if __name__ == '__main__':
    pytest.main(["-s", "test.py"])

Results of the:

test.py 1
test1里的用例
.

③ When multiple fixtures call each other, you only need to pass in the function name, similar to ②

#conftest.py文件
@pytest.fixture(scope='class')
def a_init():       
    a = 1
    return a

@pytest.fixture(scope='class')
def n_init(a_init):    
    b = a_init+1
    return b

Guess you like

Origin blog.csdn.net/weixin_45128456/article/details/113181809