Python測試框架pytest(23)外掛 - pytest-picked、pytest-lazy-fixture
1、pytest-picked(執行未提交的git用例)
自動化測試用例一般編寫完後且又執行通過,都會提交到 git 倉庫裡。但是每次新增用例後,希望只執行未提交到 git 倉庫裡的用例。
pytest-picked 外掛可以實現只執行未提交到 git 倉庫裡的測試用例。
1.1、安裝
在命令列中執行以下命令進行安裝:
pip install pytest-picked
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快。)
pip install pytest-picked -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
1.2、引數
-
--picked=[{only,first}] 單獨或首先執行與更改的檔案相關的測試
-
--mode=PICKED_MODE Options: unstaged, branch
-
--parent-branch=PARENT_BRANCH 回購的主要分支(master、main、trunk等)
1.3、用法
pytest --picked pytest --picked=first pytest --picked --mode=branch pytest --picked --mode=unstaged # 預設 pytest --picked --mode=branch --parent-branch=main #如果你的父分支與主分支"master"不同
1.4、示例
以gitlab為例,首先要建立gitlab賬號
訪問官網並註冊賬號
https://gitlab.com/
使用git前,要先安裝git
訪問官網下載並安裝即可
https://git-scm.com/
建立專案,專案目錄結構:
建立test_case1.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ class TestDemo(): def test_case1(self): print("執行用例1") def test_case2(self): print("執行用例2") def test_case3(self): print("執行用例3")
建立test_case2.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ class TestClass(): def test_case4(self): print("執行用例4") def test_case5(self): print("執行用例5") def test_case6(self): print("執行用例6")
在gitlab上建立相關專案,例如My_Demo
PyCharm上建立本地git倉庫
配置gitlab上所建立的專案(My_Demo)地址
提交
push到遠端倉庫裡
已將專案同步到gitlab上
之後再新增2個檔案,新增用例
建立test_case3.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ class TestAdd1(): def test_case7(self): print("執行用例7") def test_case8(self): print("執行用例8") def test_case9(self): print("執行用例9")
建立test_case4.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ class TestAdd2(): def test_case10(self): print("執行用例10") def test_case11(self): print("執行用例11") def test_case12(self): print("執行用例12")
建立完成後的目錄結構
命令列跳轉到專案的根目錄,輸入命令檢視當前分支狀態
git status
新增的2個檔案沒有提交到git倉庫裡
1、使用引數(--picked)
命令列輸入執行命令
pytest --picked
執行結果:只執行新增的2個檔案用例
2、使用引數(--picked=first)
命令列輸入執行命令
pytest --picked=first
執行結果:首先執行修改後的測試檔案,之後執行所有未修改的測試檔案。
2、pytest-lazy-fixture(在pytest.mark.parametrize中使用fixture)
pytest-lazy-fixture 外掛,解決在測試用例中使用 @pytest.mark.parametrize 引數化時呼叫 fixture。
2.1、安裝
在命令列中執行以下命令進行安裝:
pip install pytest-lazy-fixture
或者(使用國內的豆瓣源,資料會定期同步國外官網,速度快。)
pip install pytest-lazy-fixture -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2.2、示例
示例一:@pytest.mark.parametrize 引數化
建立test_lazy_fixture.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ import pytest @pytest.fixture(params=["admin", "123456"]) def my_fixture(request): return request.param @pytest.mark.parametrize("param1, param2", [("login", pytest.lazy_fixture("my_fixture"))]) def test_case(param1, param2): print("\n引數param1:" + param1) print("\n引數param2:" + param2) assert param2 in ["admin", "123456"]
開啟命令列,輸入執行命令:
pytest -s -v test_lazy_fixture.py
執行結果:
示例二:@pytest.fixture 引數化
建立test_lazy_fixture2.py檔案
指令碼程式碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公眾號:AllTests軟體測試 """ import pytest @pytest.fixture def my_fixture_1(): one = "admin" return one @pytest.fixture def my_fixture_2(): two = "123456" return two @pytest.fixture(params=[pytest.lazy_fixture("my_fixture_1"), pytest.lazy_fixture("my_fixture_2")]) def my_fixture_all(request): return request.param def test_case(my_fixture_all): print("\n引數:" + my_fixture_all) assert my_fixture_all in ["admin", "123456"]
開啟命令列,輸入執行命令:
pytest -s -v test_lazy_fixture2.py
執行結果:
本文來自部落格園,作者:AllTests軟體測試,轉載請註明原文連結:https://www.cnblogs.com/alltests/p/15494242.html