Skip to content

Mutation in parameterized value is seen across repeated calls of the test-case #7514

@ankostis

Description

@ankostis

(couldn't find any relevant issue but #237)
I'm running a parameterized test-case with a mutable parameter param_dict that gets modified in the test-case code
(the param has a single value, for simplicity).
The test-case runs twice due to some other parameter, run_twice.
The modification leaks to the next test-case run;
is that on purpose?

import pytest

@pytest.fixture(params=[0, 1])
def run_twice(request):
    pass


@pytest.mark.parametrize("param_dict", [[1]])  # this parameter leaks across runs
def test_mutate_params(param_dict, run_twice):
    assert param_dict == [1]
    # mutation seen from subsequent test-case run.
    del param_dict[0]

...which passes only the 1st time:

platform linux -- Python 3.8.3, pytest-5.3.5, py-1.9.0, pluggy-0.13.1
rootdir: ../pytest-mutate_params
plugins: cov-2.8.1, xonsh-0.9.18
collected 2 items                                                                                                                                             

test_mutate_param.py .F                                                                                                                                 [100%]

================================== FAILURES ==================================
______________________ test_mutate_params[1-param_dict0] _____________________

param_dict = [], fixt = None

    @pytest.mark.parametrize("param_dict", [[1]])
    def test_mutate_params(param_dict, run_twice):
>       assert param_dict == [1]
E       assert [] == [1]
E         Right contains one more item: 1
E         Use -v to get the full diff

test_mutate_param.py:12: AssertionError
========================= 1 failed, 1 passed in 0.03s =========================

Environment

$ pip list | grep pytest
pytest                             5.3.5
pytest-cov                         2.8.1

Linux, but i guess it doesn't make a difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: parametrizerelated to @pytest.mark.parametrizetype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions