1.使用pytest.fixture
#定義數(shù)據(jù)妓布,數(shù)據(jù)必須是列表
data = [{"username":"xue1","password":'123'},{"username":"xue2","password":'234'},{"username":"xue3","password":'345'},{"username":"xue4","password":'456'}]
def id(mark_vaule):
return "username,password".format(mark_vaule)
#fixture標記函數(shù) data 表示需要標記的數(shù)據(jù)列表
@pytest.fixture(params= data,ids=id)
def username(request):
return request.param
#username,表示使用哪個fixture標記的函數(shù)
def test_sign(self,cus_column):
print(cus_column["password"], cus_column["username"])
# 可以做網(wǎng)絡(luò)請求
pass
if __name__ == "__main__":
# 生成報告目錄文件
pytest.main(['-s', '-v', 'xxx.py::test_sign'])
2.Pytest裝飾器@pytest.mark.parametrize
代碼1:
import pytest
# 單參數(shù)單值
@pytest.mark.parametrize('user', ['admain123'])
def test(user):
print(user)
結(jié)果1:
/Users/fuyang/.virtualenvs/rpc_env/bin/python3 /Users/fuyang/PycharmProjects/rpc_env/test_mark_param.py
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.1, pluggy-0.13.1 -- /Users/fuyang/.virtualenvs/rpc_env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/fuyang/PycharmProjects/rpc_env
plugins: allure-pytest-2.7.1
collecting ... collected 1 item
test_mark_param.py::test[admain123] admain123
PASSED
=========================== 1 passed in 0.06 seconds ===========================
Process finished with exit code 0
代碼2:
# 單參數(shù)多值
@pytest.mark.parametrize('user',["18221124104","18200000000","18200000001"])
def test2(user):
print(user)
結(jié)果2:
/Users/fuyang/.virtualenvs/rpc_env/bin/python3 /Users/fuyang/PycharmProjects/rpc_env/test_mark_param.py
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.1, pluggy-0.13.1 -- /Users/fuyang/.virtualenvs/rpc_env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/fuyang/PycharmProjects/rpc_env
plugins: allure-pytest-2.7.1
collecting ... collected 3 items
test_mark_param.py::test2[18221124104] 18221124104
PASSED
test_mark_param.py::test2[18200000000] 18200000000
PASSED
test_mark_param.py::test2[18200000001] 18200000001
PASSED
=========================== 3 passed in 0.07 seconds ===========================
Process finished with exit code 0
代碼3:
# 多參數(shù)多值
@pytest.mark.parametrize('user, pwd', [("18221124104",111111),("18200000000",111111)])
def test3(user, pwd):
print(user, pwd)
結(jié)果3:
/Users/fuyang/.virtualenvs/rpc_env/bin/python3 /Users/fuyang/PycharmProjects/rpc_env/test_mark_param.py
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.1, pluggy-0.13.1 -- /Users/fuyang/.virtualenvs/rpc_env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/fuyang/PycharmProjects/rpc_env
plugins: allure-pytest-2.7.1
collecting ... collected 2 items
test_mark_param.py::test3[18221124104-111111] 18221124104 111111
PASSED
test_mark_param.py::test3[18200000000-111111] 18200000000 111111
PASSED
=========================== 2 passed in 0.07 seconds ===========================
Process finished with exit code 0
代碼4:
# 使用內(nèi)置的mark.fail標記為失敗的用例就不運行了
@pytest.mark.parametrize('user, pwd', [("18221124104", 111111), pytest.param("18200000000",111111, marks=pytest.mark.xfail)])
def test4(user, pwd):
print(user, pwd)
assert user == "18221124104"
assert pwd == 111111
結(jié)果4:
/Users/fuyang/.virtualenvs/rpc_env/bin/python3 /Users/fuyang/PycharmProjects/rpc_env/test_mark_param.py
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.1, pluggy-0.13.1 -- /Users/fuyang/.virtualenvs/rpc_env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/fuyang/PycharmProjects/rpc_env
plugins: allure-pytest-2.7.1
collecting ... collected 2 items
test_mark_param.py::test4[18221124104-111111] 18221124104 111111
PASSED
test_mark_param.py::test4[18200000000-111111] 18200000000 111111
XFAIL
===================== 1 passed, 1 xfailed in 0.10 seconds ======================
Process finished with exit code 0
代碼5:
# 若要獲得多個參數(shù)化參數(shù)的所有組合漫萄,可以堆疊裝飾器
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
print("測試組合數(shù)據(jù)x->%s,y->%s:" % (x, y))
結(jié)果5:
/Users/fuyang/.virtualenvs/rpc_env/bin/python3 /Users/fuyang/PycharmProjects/rpc_env/test_mark_param.py
============================= test session starts ==============================
platform darwin -- Python 3.7.4, pytest-5.0.1, py-1.8.1, pluggy-0.13.1 -- /Users/fuyang/.virtualenvs/rpc_env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/fuyang/PycharmProjects/rpc_env
plugins: allure-pytest-2.7.1
collecting ... collected 4 items
test_mark_param.py::test_foo[2-0] 測試組合數(shù)據(jù)x->0,y->2:
PASSED
test_mark_param.py::test_foo[2-1] 測試組合數(shù)據(jù)x->1,y->2:
PASSED
test_mark_param.py::test_foo[3-0] 測試組合數(shù)據(jù)x->0,y->3:
PASSED
test_mark_param.py::test_foo[3-1] 測試組合數(shù)據(jù)x->1,y->3:
PASSED
=========================== 4 passed in 0.06 seconds ===========================
Process finished with exit code 0