我敢肯定这是在某个地方注明的,但是我在旋转我的车轮,所以希望这里有人可以提供帮助。
我正在使用一个使用芹菜的烧瓶应用程序,并且正在使用Pytest进行测试。这是两个测试:
def test_celery_tasks_countdown_apply_async(celery_app, celery_worker):
r1 = add.apply_async((4, 4), countdown=2)
r2 = multiply.apply_async((4, 4), countdown=2)
assert r1.get(timeout=10) == 8
assert r2.get(timeout=10) == 16
def test_celery_tasks_countdown_signatures(celery_app, celery_worker):
r1 = add.s(4, 4).apply_async(countdown=2)
r2 = multiply.s(4, 4).apply_async(countdown=2)
assert r1.get(timeout=10) == 8
assert r2.get(timeout=10) == 16
实际任务是这样:
@shared_task
def add(x, y):
return x + y
@shared_task()
def multiply(x, y):
return x * y
如果我一个一个人运行它们,则通过它们。
pytest tests/test_tasks.py::test_celery_tasks_countdown_apply_async
pytest tests/test_tasks.py::test_celery_tasks_countdown_signatures
但是,如果我一起运行它们(通过调用整个 test_tasks.pys.py
文件),它们都失败了。
pytest tests/test_tasks.py
我还有其他一些工作(例如,对于 delay
)。而且,如果我从中删除 Countdown
选项,则它们都会通过,如果一起运行。
为什么使用 Countdown
选项并将这些测试一起运行会导致故障?
目前,我在 conftest.py
看起来像这样的固定装置:
pytest_plugins = ('celery.contrib.pytest', )
@pytest.fixture(scope='session')
def celery_config():
return {
'broker_url': 'redis://localhost:8001',
'result_backend': 'redis://localhost:8001',
'task_always_eager': False,
}
Update
我将这个问题留下来,因为我相信这是一个需要一些文档的有效问题。虽然我仍然不了解问题,也不知道如何在自动化测试的范围内解决该问题,但我成功地将我的任务在Pytest之外运行。
I'm sure this is noted somewhere, but I'm spinning my wheels, so hopefully someone on here can help.
I'm working with a Flask app that uses Celery and I'm testing with Pytest. Here are the two tests:
def test_celery_tasks_countdown_apply_async(celery_app, celery_worker):
r1 = add.apply_async((4, 4), countdown=2)
r2 = multiply.apply_async((4, 4), countdown=2)
assert r1.get(timeout=10) == 8
assert r2.get(timeout=10) == 16
def test_celery_tasks_countdown_signatures(celery_app, celery_worker):
r1 = add.s(4, 4).apply_async(countdown=2)
r2 = multiply.s(4, 4).apply_async(countdown=2)
assert r1.get(timeout=10) == 8
assert r2.get(timeout=10) == 16
The actual tasks are like so:
@shared_task
def add(x, y):
return x + y
@shared_task()
def multiply(x, y):
return x * y
These pass if I run them one by one.
pytest tests/test_tasks.py::test_celery_tasks_countdown_apply_async
pytest tests/test_tasks.py::test_celery_tasks_countdown_signatures
But if I run them together (by calling the whole test_tasks.py
file), they both fail.
pytest tests/test_tasks.py
I've got some other tests (eg, for delay
) that work. And if I remove the countdown
option from these, they both pass if run together.
Why does using the countdown
option and running these tests together cause failure?
Right now, my fixture in conftest.py
look like this:
pytest_plugins = ('celery.contrib.pytest', )
@pytest.fixture(scope='session')
def celery_config():
return {
'broker_url': 'redis://localhost:8001',
'result_backend': 'redis://localhost:8001',
'task_always_eager': False,
}
UPDATE
I'm leaving this question up as I believe it's a valid question that needs some documentation. While I still do not understand the issue or know how to resolve it within the confines of my automated tests, I have succeeded in getting my to run tasks locally outside of PyTest.
发布评论
评论(1)
这不是一个完整的答案,而是更多的解决方法,可以使结果保存到后端并完成测试。但是,实际任务未发送到队列,并且倒计时选项似乎被忽略了,这两者都不是我想要的。
参考在这里。
我不确定我是否想在生产中使用它们,所以这只是使我的测试通过。我仍然很想知道为什么这一切都会发生。
Not a complete answer but more of a workaround to get the results to save to the backend and tests to complete. But the actual tasks aren't sent to the queue and the countdown option seems to be ignored, both of which aren't what I'm looking for.
Reference here.
I'm not sure I want to use these in production, so this is just making my tests pass. I still would love to know why this is all happening.