【发布时间】:2021-10-13 03:32:24
【问题描述】:
测试总是返回 abc.is_online True。但 abc.is_online 应该为 False,因为 celery 任务在 60 秒后使 is_online 为 False。
错误消息:断言 True == False
其中 True =
@app.task(name="task.abc_last_active") #Celery task
def abc_last_active():
now = timezone.localtime()
for xyz in ABC.objects.all():
if not xyz.last_active:
continue
elapsed = now - xyz.last_active
if elapsed.total_seconds() >= settings.ABC_TIMEOUT: #60 Sec
xyz.is_online = False
xyz.save()
@pytest.fixture
def create_abc():
abc = ABC.objects.create(
phone="123234432",
location=Point(1, 4),
last_active=timezone.localtime() - timezone.timedelta(seconds=162),
is_online=True,
)
return abc
@pytest.mark.django_db
def test_inactive_abc_gets_deactivated(create_abc):
print(create_abc.is_online, "before deactivation")
abc_last_active()
print(create_abc.is_online, "after deactivation")
assert create_abc.is_online == False
【问题讨论】:
标签: django unit-testing django-rest-framework pytest pytest-django