title: Better way to mock class attribute in python unit test
body: <p>I have a base class that defines a class attribute and some child classes that depend on it, e.g.</p>
  
  <pre><code>class Base(object):
      assignment = dict(a=1, b=2, c=3)
  </code></pre>
  
  <p>I want to unittest this class with different <em>assignments</em>, e.g. empty dictionary, single item, etc. This is extremely simplified of course, it's not a matter of refactoring my classes or tests</p>
  
  <p>The (pytest) tests I have come up with, eventually, that work are</p>
  
  <pre><code>from .base import Base
  
  def test_empty(self):
      with mock.patch("base.Base.assignment") as a:
          a.__get__ = mock.Mock(return_value={})
          assert len(Base().assignment.values()) == 0
  
  def test_single(self):
      with mock.patch("base.Base.assignment") as a:
          a.__get__ = mock.Mock(return_value={'a':1})
          assert len(Base().assignment.values()) == 1
  </code></pre>
  
  <p>This feels rather complicated and hacky - I don't even fully understand why it works (I am familiar with descriptors though). Does mock automagically transform class attributes into descriptors?</p>
  
  <p>A solution that would feel more logical does not work:</p>
  
  <pre><code>def test_single(self):
      with mock.patch("base.Base") as a:
          a.assignment = mock.PropertyMock(return_value={'a':1})
          assert len(Base().assignment.values()) == 1
  </code></pre>
  
  <p>or just</p>
  
  <pre><code>def test_single(self):
      with mock.patch("base.Base") as a:
          a.assignment = {'a':1}
          assert len(Base().assignment.values()) == 1
  </code></pre>
  
  <p>Other variants that I've tried don't work either (assignments remains unchanged in the test). </p>
  
  <p>What's the proper way to mock a class attribute? Is there a better / more understandable way than the one above?</p>