title: Testing regexes in Python using py.test
body: <p>Regexes are still something of a dark art to me, but I think that's one of those things that just takes practice.  As such, I'm more concerned with being able to produce py.test functions that show me where my regexes are failing.  My current code is something like this:</p>
  
  <pre><code>my_regex = re.compile("&lt;this is where the magic (doesn't)? happen(s)?&gt;")
  
  def test_my_regex():
      tests = ["an easy test that I'm sure will pass",
               "a few things that may trip me up",
               "a really pathological, contrived example",
               "something from the real world?"]
  
      test_matches = [my_regex.match(test) for test in tests]
  
      for i in range(len(tests)):
          print("{}: {!r}".format(i, tests[i]))
          assert test_matches[i] is not None
  </code></pre>
  
  <p>for which the output when I run <code>py.test myfile.py</code> is something like</p>
  
  <pre><code>0: "an easy..."
  1: "a few things..."
  2: "a really pathological..."
  </code></pre>
  
  <p>where the last one is the first (only?) one to have not passed the test.</p>
  
  <p>I suppose I could do something like an</p>
  
  <pre><code>assertSequenceEqual(test_matches, [not None]*len(test_matches))
  </code></pre>
  
  <p>but that seems gross, and I was under the impression that <code>&lt;object&gt; is not None</code> is the preferred way of checking that an object isn't <code>None</code> rather than <code>&lt;object&gt; != None</code>.</p>