fixture可以放在单独的测试文件里,但是如果我们希望共享fixture,就需要在公共目录下新建个conftest.py文件,并将fixture放在里面。
图片中我们新建的conftest.py文件,只能是test_package文件夹下的.py文件及子级的文件夹下的.py文件可以使用。最上面的绿色部分是不能使用的,这是因为作用域的问题,所以也引申出当前作用域下的.py文件是可以共享当前的conftest.py。
# exercise/test_package/conftest.py
import pytest
@pytest.fixture(name='login')
def common_login():
yield '我是conftest的fixture...'
# exercise/test_package/test_account_login.py
import pytest
@pytest.mark.usefixtures('login')
class TestAccountLogin:
def test_01_account_login(self, login):
print(login)
assert 1 == 1
if __name__ == '__main__':
pytest.main(['-v', '-s', 'test_account_login.py'])
D:\python_env\Scripts\python.exe F:/TESTING/exercise/test_package/test_account_login.py
==================