预处理和后处理

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 =============================================================

posted @ 2020-01-21 11:05  干it的小张  阅读(342)  评论(0编辑  收藏  举报