title: Testing a Generator in Python
body: <p>I programmed a very simple generator in Python 3.4:</p>
  
  <pre><code>class Gen:
    def __init__(self, xml_lines, attribs):
      #...
  
    def _get_values(self, xml_line):
      # get only specific "attribs" from a line in a "xml"
  
    def values(self):
      for line in self.xml_lines:
        yield self._get_values(line)
  </code></pre>
  
  <p>The code works when I use a for loop to consume the generated values:</p>
  
  <pre><code>reader = Gen(...):
  for v in reader.values():
     print(v)
  </code></pre>
  
  <p>But I'm now trying to create a unit test, and for that, I'd need to get each value at a time (outside a loop). I'm trying something like this:</p>
  
  <pre><code>import unittest
  #...
  reader = Gen(...):
  v = reader.values()
  self.assertIsNone(v)
  </code></pre>
  
  <p>When I try that, I always get a</p>
  
  <pre><code>AssertionError: &lt;generator object next_values at 0x7fe2a5f9e3f0&gt; is not None
  </code></pre>
  
  <p>So, when I call the <strong>values</strong> myself, it return something (is it a pointer?) instead of the generated value.</p>
  
  <p>As I'm considering I'm using a basic generator pattern, my question is a little broader then my own code: what's the proper way to test a Python Generator?</p>