python接口自動化

運行方式詳解:

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("測試")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容