Fri Aug 25 12:04:44 2006

Ticket #84 (assigned)

Generator tests with mutable arguments = bad


Priority: normal Reporter: jpellerin
Severity: normal Assigned to: jpellerin (accepted)
Component: nose Status: assigned
Version:   Resolution:  
Milestone: 0.10 Keywords:  

Description by jpellerin:

From Kevin Dangoor:

A coworker found an interesting bug in test generators. Apparently,
py.test does the same thing. Maybe this is architecturally difficult
to contend with. Here's the test:

#! /usr/bin/env python

def test_list_loop():
   def check_list_loop(ls, len_expected):
       print ls
       assert len(ls) == len_expected
   ls = list()
   for i in range(0, 6):
       ls.append(i)
       yield check_list_loop, ls, i + 1

It appears that each yielded test is not run immediately, but is
collected up and *then* run. In this particular case, that fails
because a mutable object is involved (and mutated as part of the test
running). Changing that last line to

       yield check_list_loop, ls[:], i + 1

makes it work, because you're dealing with copies of the list each time.

Changelog

Mon Oct 9 18:08:02 2006: Modified by jpellerin

  • milestone changed from 0.9.1 to 0.9.2

Tue Nov 21 09:30:47 2006: Modified by jpellerin

  • milestone changed from 0.9.2 to 0.10
  • status changed from new to assigned