入门8-Pytest部分测试用例的前后置(fixture)(2-conftest.py结合)

发布时间 2023-05-09 21:57:58作者: 蜗牛果果
  1. conftest.py文件专门用来存放fixture的文件,名称固定不能修改。
  2. conftest.py中的所有方法在调用时都不需要导包
  3. 一个用例可以同时调用多个conftest.py中的多个方法
  4. 一般conftest.py中的方法autouse= True, 自动执行。
  5. conftest.py放在最外层,使用scope= "session"时,整个项目开头执行一次。及时设置为多线程,也是执行一次。

注意,如果全局和子folder下的fixture同名,则最近的fixture起作用,即company下的conftest.py起作用,全局的不会生效  

示例
#
company下conftest.py import pytest from Common.common import CommonUtil @pytest.fixture(scope = "function",autouse = True,name = "logincompany") def exe_login(): print("Login company" ) yield print("Logout company") @pytest.fixture(scope = "function",autouse = True, name = "username_company") def get_loginusers(): print("the user is company loginuser")
#全局conftest
import pytest
from Common.common import CommonUtil


@pytest.fixture(scope = "function",autouse = True,name = "login")
def exe_login():
    print("Login system" )
    yield 
    print("Logout system")

@pytest.fixture(scope = "function",autouse = True, name = "username")
def get_loginusers():
    print("the user is system loginuser")