作用域:

import pytest
import requests
@pytest.fixture()
def db():
print("Connection successful")
yield
print("Connection closed")

def search_user(user_id):
d = {
"001":"xiaoming",
"002":"xiaohua",
}
return d[user_id]

def test_case_01(db):
assert search_user("001") == "xiaoming"

def test_case_02(db):
assert search_user("002") == "xiaohua"

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

结果:

=========================================================== test session starts ============================================================
platform win32 -- Python 3.6.6, pytest-5.3.2, py-1.8.1, pluggy-0.13.1 -- c:\python36\python.exe
cachedir: .pytest_cache
rootdir: D:\s27\day68, inifile: pytest.ini, testpaths: ./scripts
collected 2 items

scripts/test_case.py::test_case_01 Connection successful
PASSEDConnection closed

scripts/test_case.py::test_case_02 Connection successful
PASSEDConnection closed


============================================================ 2 passed in 0.55s =============================================================

猜你喜欢

转载自www.cnblogs.com/zhang-da/p/12221026.html