If you're using Flask-APScheduler and pytest, you can do something like:
import datetime
from apscheduler.job import Job
from apscheduler.triggers.cron import (
BaseField,
CronTrigger,
DayOfMonthField,
DayOfWeekField,
MonthField,
WeekField,
)
from app.core.extensions import scheduler
from ..tasks import a_scheduled_function
def test_is_scheduled_hourly():
"""
Test to verify that `a_scheduled_function` is scheduled correctly on the 2nd minute
of every hour using a cron trigger.
"""
# Retrieve scheduled job using its id
a_scheduled_function_job: Job = scheduler.get_job(id="a_scheduled_function")
assert a_scheduled_function_job is not None
assert a_scheduled_function_job.func == a_scheduled_function
trigger: CronTrigger = a_scheduled_function_job.trigger
assert isinstance(trigger, CronTrigger)
assert trigger.timezone == datetime.timezone.utc
assert trigger.end_date is None
assert trigger.fields == [
BaseField("year", "*"),
MonthField("month", "*"),
DayOfMonthField("day", "*"),
WeekField("week", "*"),
DayOfWeekField("day_of_week", "*"),
BaseField("hour", "*"),
BaseField("minute", "2"),
BaseField("second", "0"),
]
This is a sample test for a function that is scheduled to run every hour on the second minute.