2020-02-20 pytest框架的一些整理

2020-02-25  本文已影响0人  fuyang

1.使用pytest.fixture

#定义数据,数据必须是列表
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标记函数 data 表示需要标记的数据列表
@pytest.fixture(params= data,ids=id)
def username(request):
    return request.param

    #username,表示使用哪个fixture标记的函数
    def test_sign(self,cus_column):
      print(cus_column["password"], cus_column["username"])
      # 可以做网络请求
      pass

if __name__ == "__main__":
    # 生成报告目录文件
    pytest.main(['-s', '-v', 'xxx.py::test_sign'])

2.Pytest装饰器@pytest.mark.parametrize
代码1:

import pytest
# 单参数单值
@pytest.mark.parametrize('user', ['admain123'])
def test(user):
    print(user)

结果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:

# 单参数多值
@pytest.mark.parametrize('user',["18221124104","18200000000","18200000001"])
def test2(user):
    print(user)

结果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:

# 多参数多值
@pytest.mark.parametrize('user, pwd', [("18221124104",111111),("18200000000",111111)])
def test3(user, pwd):
    print(user, pwd)

结果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:

# 使用内置的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

结果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:

# 若要获得多个参数化参数的所有组合,可以堆叠装饰器
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print("测试组合数据x->%s,y->%s:" % (x, y))

结果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] 测试组合数据x->0,y->2:
PASSED
test_mark_param.py::test_foo[2-1] 测试组合数据x->1,y->2:
PASSED
test_mark_param.py::test_foo[3-0] 测试组合数据x->0,y->3:
PASSED
test_mark_param.py::test_foo[3-1] 测试组合数据x->1,y->3:
PASSED

=========================== 4 passed in 0.06 seconds ===========================

Process finished with exit code 0
上一篇下一篇

猜你喜欢

热点阅读