title: Mocking (celery task) function attribute using unit-test
body: <p>I have some difficulties to create a unit-test for a function, which constitutes a celery task. The unit-test intends to test the functionality executed if an Exception is raised and the task has been retried for the last time, thus failing afterwards (<code>retries == max_retries</code>).</p>
  <p>As I test the function without celery running in the background, there are no retries and I thought of mocking the max_retry attribute by setting it to <code>0</code> to simulate the intended situation. However, that tends not to be as easy as I thought.</p>
  <p>Besides my initial approach (version 2), I also tried the suggestions in <a href="https://stackoverflow.com/questions/16867509/mock-attributes-in-python-mock">mock-attributes-in-python-mock</a> (version 1), which tends to be close to what I am trying to achieve. However, both attempts failed: either the complete function is mocked and the Exception is not raised (version 1) or the attribute is mocked, resulting in an <code>int</code>-<code>PropertyMock</code> comparison (version 2).</p>
  <p>Any help or directions for a solution are much appreciated.</p>
  <p>Test Case Version 1: Mocked function</p>
  <pre class="lang-py prettyprint-override"><code>
  @patch('afinad.ServiceBroker.tasks.task_to_test')
  def test_task_to_test(self, task_patch) -&gt; None:
  
      task_patch.max_retries = PropertyMock(return_value=0)
      tasks.task_to_test()
  </code></pre>
  <p>Test Case Version 2: <code>PropertyMock</code> comparison</p>
  <pre class="lang-py prettyprint-override"><code>
  @patch('tasks.task_to_test.max_retries')
  def test_task_to_test(self, task_patch) -&gt; None:
  
      task_patch.return_value = 0
      tasks.task_to_test()
  
  </code></pre>
  <p>The function to test:</p>
  <pre class="lang-py prettyprint-override"><code>
  @app.task(
      name='tasks.task_to_test',
      autoretry_for=(Exception,),
      retry_kwargs={'max_retries': 3}
  )
  def task_to_test() -&gt; None:
  
      try:
          raise Exception('Ups, something went wrong')
      except Exception as e:
          retries = task_to_test.request.retries
          max_retries = task_to_test.max_retries
          if retries == max_retries:
              print('Performing corrective action.')
          raise e
  </code></pre>