pytest conftest

目录结构

conftest.py

__author__ = 'kangpc'
__date__ = '2020-7-13 18:04'

"""
conftest.py配置需要注意以下点:
	conftest.py配置脚本名称是固定的,不能改名称
	conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
	不需要import导入 conftest.py,pytest用例会自动查找

实现场景:
	用例1需要先登录,用例2不需要登录,用例3需要先登录
"""

import pytest

# @pytest.fixture()里面没有参数,默认scope="function"
@pytest.fixture()  
def login():
	print("****输入账号密码登录****")

test_fix1.py

__author__ = 'kangpc'
__date__ = '2020-7-13 18:03'

import pytest

def test_c1(login):
	print("----用例1:登录之后其他动作111----")

def test_c2():
	print("----用例2:不需要登录,动作222----")

def test_c3(login):
	print("----用例3:登录之后其他动作333----")

if __name__ == '__main__':
	pytest.main(["-s", "test_fix1.py"])

test_fix2.py

__author__ = 'kangpc'
__date__ = '2020-7-13 18:04'

import pytest

def test_c4(login):
	print("----用例4:登录之后其他动作444----")

def test_c5():
	print("----用例5:不需要登录,动作555----")


if __name__ == '__main__':
	pytest.main(["-q", "test_fix2.py"])


更多学习笔记移步 https://www.cnblogs.com/kknote
原文地址:https://www.cnblogs.com/kknote/p/13296801.html