Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AdamEsterle's avatar

How do I TDD cron jobs?

How can I run tests for my cron jobs in general?

How can I run tests that will test the intervals? I.E. making sure it runs every 5 minutes or whatever?

0 likes
5 replies
lostdreamer_nl's avatar

There is no build in functionality for listing the scheduled tasks, but you can create it yourself using: https://gist.github.com/Ellrion/0d1f7cdad225eeccaeea

Testing it's functionality seems trivial to me, it's like testing a controller method.

In your test, you setup the world as needed, then run the job that needs to be tested, and then test if the output was what you needed it to be (email was send / DB entry has been inserted, etc)

2 likes
Youz's avatar

You need to test manually your code, if its work manually it should work via cron.

You may use some health check for cron to see if they works.

I think cron i a part of server side and you dont need to test if your cron is running in your tests for your code.

michimawan's avatar

what i do to TDD the cron job (not the interval as the interval is belongs to system or configuration), i will create a class to do the task then test it as usual, the cron just call an entry method of the class, e.g: run / init / etc

1 like
AdamEsterle's avatar

@michimawan That's what I am doing too and it is all tested. I guess I will cross my fingers regarding the interval part

Please or to participate in this conversation.