1、ids參數(shù)說明
ids
參數(shù)就是給每一個變量起一個別名赴涵。
示例:
import pytest
data = [("孫悟空", 666), ("豬八戒", 777), ("沙和尚", 888)]
# ids參數(shù)的個數(shù)要與params參數(shù)的個數(shù)相同垄琐,用的很少
@pytest.fixture(params=data, ids=['suk', 'zbj', 'shs'])
def need_data(request):
return request.param
def test_data(need_data):
print(f"測試人物:{need_data[0]}")
print(f"測試分數(shù):{need_data[1]}")
if __name__ == '__main__':
pytest.main()
"""
之前的測試結(jié)果:
test_01.py::test_data[need_data0] 測試人物:孫悟空
測試分數(shù):666
PASSED
test_01.py::test_data[need_data1] 測試人物:豬八戒
測試分數(shù):777
PASSED
test_01.py::test_data[need_data2] 測試人物:沙和尚
測試分數(shù):888
PASSED
加上ids參數(shù)后的執(zhí)行結(jié)果:只是[]的內(nèi)容變化了蹦肴,這就是ids參數(shù)的作用
test_01.py::test_data[suk] 測試人物:孫悟空
測試分數(shù):666
PASSED
test_01.py::test_data[zbj] 測試人物:豬八戒
測試分數(shù):777
PASSED
test_01.py::test_data[shs] 測試人物:沙和尚
測試分數(shù):888
PASSED
"""
2、name參數(shù)說明
name
參數(shù)就是給Fixture修飾的方法起個別名揽涮。
示例:
import pytest
data = [("孫悟空", 666), ("豬八戒", 777), ("沙和尚", 888)]
# ids參數(shù)的個數(shù)要與params參數(shù)的個數(shù)相同抠藕,用的很少
@pytest.fixture(params=data, name="abc")
def need_data(request):
return request.param
# 注意,如果定義了name屬性蒋困,用例的方法參數(shù)就需要傳入name屬性定義的值盾似。
# 否則將會報錯。
# 還有用例中調(diào)用參數(shù)的使用雪标,也需要使用name定義的別名零院。
# 即:當取了別名之后,那么原來的方法名就用不了了村刨。
def test_data(abc):
print(f"測試人物:{abc[0]}")
print(f"測試分數(shù):{abc[1]}")
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果:
test_01.py::test_data[abc0] 測試人物:孫悟空
測試分數(shù):666
PASSED
test_01.py::test_data[abc1] 測試人物:豬八戒
測試分數(shù):777
PASSED
test_01.py::test_data[abc2] 測試人物:沙和尚
測試分數(shù):888
PASSED
"""
3告抄、scope參數(shù)說明
scope
參數(shù)有四個級別:function(默認)
,class
嵌牺,module
打洼,session
龄糊。
作用范圍:session
> module
> class
> function
-
scope="function"
:@pytest.fixture()
裝飾器如果不寫參數(shù),默認參數(shù)就是scope="function"
拟蜻。它的作用范圍是绎签,在每個測試用例執(zhí)行的前(后)運行一次枯饿。 -
scope="class"
:@pytest.fixture()
裝飾器為class
級別的時候酝锅,F(xiàn)ixture方法只在此class
里所有用例開始執(zhí)行前(后)執(zhí)行一次。與class
中的function
沒有關(guān)系奢方。 -
scope="module"
:@pytest.fixture()
裝飾器為module
級別的時候搔扁,在一個.py
腳本中所有用例開始執(zhí)行前(后)執(zhí)行一次。 -
scope="session"
:@pytest.fixture()
裝飾器為session
級別的時候蟋字,是可以跨.py
模塊調(diào)用的稿蹲,每個.py
文件就是module
。
?
(1)scope="function"
import pytest
@pytest.fixture(scope="function")
def login():
print("打開APP")
print("輸入賬號鹊奖,密碼苛聘,進行登錄")
yield # 當用例執(zhí)行完成后,執(zhí)行yield后的代碼
print("關(guān)閉APP")
def test_add_cart(login):
print("添加購物車--需要登錄")
def test_browser_goods():
print("瀏覽商品--不需要登錄")
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果: 只作用域方法或者函數(shù)范圍
test_01.py::test_add_cart 打開APP
輸入賬號,密碼忠聚,進行登錄
添加購物車--需要登錄
PASSED關(guān)閉APP
test_01.py::test_browser_goods 瀏覽商品--不需要登錄
PASSED
"""
(2)scope="class"
import pytest
@pytest.fixture(scope="class")
def login():
print("\nscope=class 作用域的前置函數(shù)\n")
yield # 當用例執(zhí)行完成后,執(zhí)行yield后的代碼
print("\n\nscope=class 作用域的后置函數(shù)")
class Test_Demo:
def test_add_cart(self, login):
print("添加購物車--測試用例")
def test_add_address(self):
print("添加收貨地址--測試用例")
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果: 只在測試類開始前后執(zhí)行了
scope=class 作用域的前置函數(shù)
添加購物車--測試用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收貨地址--測試用例
PASSED
scope=class 作用域的后置函數(shù)
"""
(3)scope="module"
import pytest
@pytest.fixture(scope="module")
def login():
print("\nscope=module 作用域的前置函數(shù)\n")
yield # 當用例執(zhí)行完成后,執(zhí)行yield后的代碼
print("\n\nscope=module 作用域的后置函數(shù)")
# 如果該用例不傳入fixture方法设哗,該用例會優(yōu)先與fixture前置執(zhí)行
def test_browser_goods(login):
print("瀏覽商品--不需要登錄")
class Test_Demo:
def test_add_cart(self, login):
print("添加購物車--測試用例")
def test_add_address(self):
print("添加收貨地址--測試用例")
class Test_Demo2:
def test_add_cart(self, login):
print("第2次-添加購物車--測試用例")
def test_add_address(self):
print("第2次-添加收貨地址--測試用例")
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果:
說明一下:scope=module時,整個.py模塊中fixture只執(zhí)行一次
scope=module 作用域的前置函數(shù)
瀏覽商品--不需要登錄
PASSED
test_01.py::Test_Demo::test_add_cart 添加購物車--測試用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收貨地址--測試用例
PASSED
test_01.py::Test_Demo2::test_add_cart 第2次-添加購物車--測試用例
PASSED
test_01.py::Test_Demo2::test_add_address 第2次-添加收貨地址--測試用例
PASSED
scope=module 作用域的后置函數(shù)
"""
(4)scope="session"
test_01.py
文件中的用例两蟀。
import pytest
@pytest.fixture(scope="session")
def login():
print("\nscope=session 作用域的前置函數(shù)\n")
yield # 當用例執(zhí)行完成后,執(zhí)行yield后的代碼
print("\n\nscope=session 作用域的后置函數(shù)")
# 如果該用例不傳入fixture方法网梢,該用例會優(yōu)先與fixture前置執(zhí)行
def test_browser_goods(login):
print("瀏覽商品--不需要登錄")
class Test_Demo:
def test_add_cart(self, login):
print("添加購物車--測試用例")
def test_add_address(self):
print("添加收貨地址--測試用例")
if __name__ == '__main__':
pytest.main()
test_02.py
文件中的用例。
# 如果該用例不傳入fixture方法赂毯,該用例會優(yōu)先與fixture前置執(zhí)行
def test_browser_goods():
print("第2次-瀏覽商品--不需要登錄")
class Test_Demo2:
def test_add_cart(self):
print("第2次-添加購物車--測試用例")
def test_add_address(self):
print("第2次-添加收貨地址--測試用例")
all.py
文件內(nèi)容:
import pytest
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果:只在該包內(nèi)執(zhí)行一次fixture前后置
scope=session 作用域的前置函數(shù)
瀏覽商品--不需要登錄
PASSED
test_01.py::Test_Demo::test_add_cart 添加購物車--測試用例
PASSED
test_01.py::Test_Demo::test_add_address 添加收貨地址--測試用例
PASSED
test_02.py::test_browser_goods 第2次-瀏覽商品--不需要登錄
PASSED
test_02.py::Test_Demo2::test_add_cart 第2次-添加購物車--測試用例
PASSED
test_02.py::Test_Demo2::test_add_address 第2次-添加收貨地址--測試用例
PASSED
scope=session 作用域的后置函數(shù)
"""
說明:
我把session作用域的Fixture寫在了
test_01.py
文件中战虏,其實應(yīng)該提取出來寫到conftest.py
文件中。(后面文章會說明)
all.py
文件就是為了執(zhí)行所有測試用例党涕,pytest.ini
文件中配置的模糊匹配查詢烦感,這樣兩個文件中的用例就都能執(zhí)行到了。這里就是一個練習(xí)膛堤,寫的十分不規(guī)范手趣,理解其意思就可以。
4骑祟、autouse參數(shù)說明
示例:實全部用例的前后置
很簡單回懦,只需要把Fixture中的autouse
參數(shù)設(shè)置為True即可。
Fixture裝飾器會自動執(zhí)行作用域范圍內(nèi)的所有用例的前后置次企。
import pytest
@pytest.fixture(autouse=True)
def login():
print("輸入賬號怯晕,密碼,進行登錄")
def test_add_cart(): # 不需要傳入fixture方法
print("添加購物車--需要登錄")
def test_add_address(): # 不需要傳入fixture方法
print("添加收貨地址--需要登錄")
if __name__ == '__main__':
pytest.main()
"""
執(zhí)行結(jié)果:
輸入賬號缸棵,密碼舟茶,進行登錄
添加購物車--需要登錄
PASSED輸入賬號,密碼,進行登錄
添加收貨地址--需要登錄
PASSED
"""