運行方式詳解:
1.命令行模式
輸入pytest運行即可
參數(shù):
-vs -v輸出更詳細(xì)的信息,-s表示輸出調(diào)試信息
-n 多線程運行測試用例
--reruns 失敗用例重跑
raise Exception("巴拉巴拉巴拉")
--html 生成測試報告
--html=/.reports.report.html
2.主函數(shù)模式
pytest.main()
3.基于pytest.ini的配置文件運行
[pytest]
adopts = -vs
test-paths = ./testcases
python_files = test.py
python_classes = Test
python_functions = test_*
markers =
smoke:冒煙測試
@pytest.mark.smoke
增加標(biāo)記后可以通過如下方式只執(zhí)行帶標(biāo)記的用例
adopts = -vs -m smoke
測試用例的前后置、固件、夾具
def setup(self):
print("在每個用例之前執(zhí)行一次:初始化日志對象,初始化數(shù)據(jù)庫連接")
def teardown(self):
print("在每個用例之后執(zhí)行一次:關(guān)閉日志對象,關(guān)閉數(shù)據(jù)庫連接")
def setup_class(self):
print("在每個類之前執(zhí)行")
def teardown_class(self):
print("在每個類之后執(zhí)行")
@pytest.fixture裝飾器可以實現(xiàn)部分用例前后置。
@pytest.fixture(scope="", params="", autouse="", ids="", name="")
scope:作用域
function,class,module,package/session
@pytest.fixture(scope="function")
def exe_sql():
print("用例之前")
yield
print("用例之后")
@pytest.fixture(scope="class", autouse=True)
def exe_sql():
print("類之前")
yield
print("類之后")
如果scope=function,那么可以在用例的參數(shù)后面單獨調(diào)用。
如果scope="class",那么可以在類上面通過參數(shù)后面單獨調(diào)用。
@pytest.fixture(scope="class")
def exe_sql():
print("類之前")
yield
print("類之后")
@pytest.mark.usefixtures("exe_sql")
class TestQian:
def test_jing(self):
print("測試")