title: Correct way of unit testing __repr__ with dict
body: <p>Say I have a class given by:</p>
  
  <pre class="lang-py prettyprint-override"><code>class MyClass:
      def __init__(self, **kwargs):
          self.kwargs = kwargs
      def __repr__(self):
          return "&lt;%s: %r&gt;" % (self.__class__.__name__, self.kwargs)
  </code></pre>
  
  <p><code>__repr__</code> isn't really important for core functionality, but occasionally gets called for logging, shows up in stack traces, and such, so I'd like to unit-test it.</p>
  
  <p>The problem is similar to <a href="https://stackoverflow.com/questions/15549429/how-do-i-test-dictionary-equality-with-pythons-doctest-package">that faced when using doctest</a>, but I'd rather keep any complexity (like sorting) in the test function and not in <code>__repr__</code>.</p>
  
  <p>Right now I'm using <code>eval</code> and <code>re</code> to extract the dictionary from the <code>repr()</code> call, but I wanted to check if there were non-<code>eval</code> alternatives out there that people used.</p>
  
  <pre class="lang-py prettyprint-override"><code>def test_repr():
      retval = repr(MyClass(a=1, b=2, c=3))
      match = re.match("^&lt;MyClass: ({.*})&gt;\Z", retval)
      assert match
      assert eval(match.group(1)) == dict(a=1, b=2, c=3)
  </code></pre>