【pytest】 パラメータ化@pytest.mark.parametrize

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 ==============================

おすすめ

転載: blog.csdn.net/oDianZi1234567/article/details/132958505