0
点赞
收藏
分享

微信扫一扫

1999-2018年地级市一般公共预算收入、支出(教育事业费、科技支出)

河南妞 2023-09-24 阅读 37

1.创建   test_parametrize.py

通过

@pytest.mark.parametrize 方法设置参数
import pytest

import math

#pytest参数化
@pytest.mark.parametrize(
"base,exponent,expected", # 参数变量名称
# 每个元组都是一条测试用例测试数据
[
(2,2,4),
(3,3,9),
(1,9,1),
(0,9,0)
],
ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
)

def test_pow(base,exponent,expected):
print(base,exponent,expected)
assert math.pow(base,exponent)==expected

2.运行结果:pytest -s test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -v test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 0 items

======================================================================= no tests ran in 0.03s =======================================================================
ERROR: file or directory not found: test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 0 items

======================================================================= no tests ran in 0.04s =======================================================================
ERROR: file or directory not found: test_parametrize.py

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> cd parametrize
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items

test_parametrize.py .F..

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

@pytest.mark.parametrize(
"base,exponent,expected", # 参数变量名称
# 每个元组都是一条测试用例测试数据
[
(2,2,4),
(3,3,9),
(1,9,1),
(0,9,0)
],
ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
)

def test_pow(base,exponent,expected):
> assert math.pow(base,exponent)==expected
E assert 27.0 == 9
E + where 27.0 = <built-in function pow>(3, 3)
E + where <built-in function pow> = math.pow

test_parametrize.py:18: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 1.66s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items

test_parametrize.py 2 2 4
.3 3 9
F1 9 1
.0 9 0
.

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

@pytest.mark.parametrize(
"base,exponent,expected", # 参数变量名称
# 每个元组都是一条测试用例测试数据
[
(2,2,4),
(3,3,9),
(1,9,1),
(0,9,0)
],
ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
)

def test_pow(base,exponent,expected):
print(base,exponent,expected)
> assert math.pow(base,exponent)==expected
E assert 27.0 == 9
E + where 27.0 = <built-in function pow>(3, 3)
E + where <built-in function pow> = math.pow

test_parametrize.py:19: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 0.44s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

  运行  pytest -v test_parametrize.py

======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
plugins: anyio-3.5.0
collected 4 items

test_parametrize.py::test_pow[case1] PASSED [ 25%]
test_parametrize.py::test_pow[case2] FAILED [ 50%]
test_parametrize.py::test_pow[case3] PASSED [ 75%]
test_parametrize.py::test_pow[case4] PASSED [100%]

============================================================================= FAILURES ==============================================================================
__________________________________________________________________________ test_pow[case2] __________________________________________________________________________

base = 3, exponent = 3, expected = 9

@pytest.mark.parametrize(
"base,exponent,expected", # 参数变量名称
# 每个元组都是一条测试用例测试数据
[
(2,2,4),
(3,3,9),
(1,9,1),
(0,9,0)
],
ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
)

def test_pow(base,exponent,expected):
print(base,exponent,expected)
> assert math.pow(base,exponent)==expected
E assert 27.0 == 9
E + where 27.0 = <built-in function pow>(3, 3)
E + where <built-in function pow> = math.pow

test_parametrize.py:19: AssertionError
----------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------
3 3 9
====================================================================== short test summary info ======================================================================
FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
==================================================================== 1 failed, 3 passed in 1.79s ====================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

3.运行参数控制


pytest -s test_parametrize.py     其中 -s 用于关闭捕捉 

pytest -v test_parametrize.py   增加测试用例冗长

pytest --help  查看帮助

pytest  -q  test_parametrize.py 减少测试冗长

pytest -x   test_parametrize.py 如果出现一条测试用例失败,就停止

pytest  ./test_dir    运行测试目录 

pytest      指定特定类或是方法

 pytest   ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6

# 功能函数
def multiply(a, b):
return a * b


class TestMultiply:
# =====fixtures========
@classmethod
def setup_class(cls):
print("setup_class=========>")

@classmethod
def teardown_class(cls):
print("teardown_class=========>")

def setup_method(self, method):
print("setup_method----->>")

def teardown_method(self, method):
print("teardown_method-->>")

def setup(self):
print("setup----->")

def teardown(self):
print("teardown-->")

# =====测试用例========
def test_numbers_5_6(self):
print('test_numbers_5_6')
assert multiply(5, 6) == 30

def test_strings_b_2(self):
print('test_strings_b_2')
assert multiply('b', 2) == 'bb'
 pytest   ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item

fixture\test_fixtures_02.py . [100%]

通过 main 方法运行 

 

import pytest

if __name__=='__main__':
#pytest.main(['-s','./fixture'])
pytest.main(['-v', './fixture'])

 


============================== 6 passed in 0.06s ==============================
============================= test session starts =============================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
cachedir: .pytest_cache
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collecting ... collected 6 items

fixture/test_fixtures_01.py::test_mult_3_4 PASSED [ 16%]
fixture/test_fixtures_01.py::test_mult_a_4 PASSED [ 33%]
fixture/test_fixtures_011.py::test_multiply_3_4 PASSED [ 50%]
fixture/test_fixtures_011.py::test_multiply_a_3 PASSED [ 66%]
fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6 PASSED [ 83%]
fixture/test_fixtures_02.py::TestMultiply::test_strings_b_2 PASSED [100%]

============================== 6 passed in 0.06s ==============================

举报
0 条评论