目錄
1、pytest-picked(運行未提交的git用例)
1.1、安裝
1.2、引數
1.3、用法
1.4、示例
2、pytest-lazy-fixture(在pytest.mark.parametrize中使用fixture)
2.1、安裝
2.2、示例
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
運行結果:


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/344186.html
標籤:其他
上一篇:Python測驗框架pytest(22)插件 - pytest-xdist(分布式執行)
下一篇:postman發送資料亂碼
