[py-svn] [hpk42/pytest] Unexpected behavior when modifying parameterized value (issue #237)

James Laska issues-reply at bitbucket.org
Tue Dec 11 17:37:13 CET 2012


--- you can reply above this line ---

New issue 237: Unexpected behavior when modifying parameterized value
https://bitbucket.org/hpk42/pytest/issue/237/unexpected-behavior-when-modifying

James Laska:

Greetings,

With py.test-2.3.4, when adjusting the value of a parameterized test function argument, it appears the changes are made to subsequent calls to the same function.

With the following conftest.py ...
```
#!python

import pytest

releasevers = "6Server".split()
basearchs = "i386 x86_64".split()
clouds = "rhevm ec2".split()
image_list = list()
for releasever in releasevers:
    for basearch in basearchs:
        image_list.append(dict(
            name="rhel-{basearch}-{releasever}".format(releasever=releasever, basearch=basearch),
            profile="small-{basearch}".format(basearch=basearch),
            releasever=releasever,
            basearch=basearch))

def pytest_generate_tests(metafunc):

    if 'cloud' in metafunc.funcargnames:
        metafunc.parametrize("cloud", clouds)

    if 'image' in metafunc.funcargnames:
        metafunc.parametrize("image", image_list, ids=[i.get('name') for i in image_list])

```

Any the following test_images.py ...

```
#!python

def test_build(cloud, image):

    print "\n  BEFORE: profile=%s" % (image.get('profile'))

    # If rhevm, alter the profile
    if cloud == 'rhevm' and image.get('basearch','') == 'i386':
        image['profile'] = 'small-x86_64'
        print "  MODIFIED PROFILE"

    if cloud == 'rhevm':
        assert image.get('profile') == 'small-x86_64'
    else:
        assert image.get('profile') == 'small-%s' % image.get('basearch')
```

This may be something fundamental to python (call-by-reference) that I'm missing.  However, it appears that py.test is adjusting the value of image['profile'] for subsequent calls to the test function.  For example ..

```
py.test -s .
============================================================================================= test session starts =============================================================================================
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
collected 4 items 

test_images.py 
  BEFORE: profile=small-i386
  MODIFIED PROFILE
.
  BEFORE: profile=small-x86_64
.
  BEFORE: profile=small-x86_64
F
  BEFORE: profile=small-x86_64
.

================================================================================================== FAILURES ===================================================================================================
______________________________________________________________________________________ test_build[ec2-rhel-i386-6Server] ______________________________________________________________________________________

cloud = 'ec2', image = {'basearch': 'i386', 'name': 'rhel-i386-6Server', 'profile': 'small-x86_64', 'releasever': '6Server'}

    def test_build(cloud, image):
    
        print "\n  BEFORE: profile=%s" % (image.get('profile'))
    
        # If rhevm, alter the profile
        if cloud == 'rhevm' and image.get('basearch','') == 'i386':
            image['profile'] = 'small-x86_64'
            print "  MODIFIED PROFILE"
    
        if cloud == 'rhevm':
            assert image.get('profile') == 'small-x86_64'
        else:
>           assert image.get('profile') == 'small-%s' % image.get('basearch')
E           assert 'small-x86_64' == 'small-i386'
E             - small-x86_64
E             + small-i386

test_images.py:13: AssertionError
===================================================================================== 1 failed, 3 passed in 0.02 seconds ======================================================================================

```

If I first copy the test argument named 'image' using the 'copy.copy()' method, I am able to modify the object as needed without disruption to other generated calls to the same method.  

Please advise, is this a problem with pytest, or in how I'm generating the test arguments?


--

This is an issue notification from bitbucket.org. You are receiving
this either because you are the owner of the issue, or you are
following the issue.



More information about the pytest-commit mailing list