3

Django で機能テストを実行したいと考えています。

テスト中に Celery タスクを無視するにはどうすればよいですか?

class TestsFunctional(TestCase):

    def test_ignore_task(self):
        response = my_method()
        self.assertEqual(201, response)



def my_method():
    #just want to ignore tasks
    from celery import chain
    chain(tasks.long_task.s(), tasks.another_task.s()).apply_async()
    return 201


@task(default_retry_delay=10, max_retries=None)
def long_task():
    try:
        #infinite on localhost
    except socket.error:
        logger.warning("Service not reachable")
        long_task.retry()
    except Exception as e:
        logger.exception(e)

私のsettings.pyで

TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
4

2 に答える 2

2

Kevin Stone の回答を拡張するには、パッチデコレータとMagicMockを次のように使用して、テストの Celery 部分をモックできます。

テストコード

from unittest.mock import MagicMock, patch

@patch('reference.to.your.long_task', new=MagicMock())
class TestsFunctional(TestCase):

  def test_ignore_task(self):
      response = my_method()
      self.assertEqual(201, response) 

アプリケーションコード

def my_method():
  from celery import chain
  chain(tasks.long_task.s(), tasks.another_task.s()).apply_async()
  return 201

@task(default_retry_delay=10, max_retries=None)
def long_task():
  # Long running process

(YMMV)

于 2016-03-07T08:18:38.147 に答える
1

単体テスト中にスキップしたい関数に追加するデコレーターを作成しました。

https://gist.github.com/kevinastone/7295567

他のオプションはmock、タスクを使用してモックアウトすることです。

于 2013-11-03T22:22:37.663 に答える