Unit Testing a Plugin functionality with cron

I’m assuming your referring to a server based cron job. I’m not sure if your familiar with it but you could also use WP Cron. It’s an api that built into core. It’s a little easier to work with than a true cron job IMHO. It does have a mild caveat however. Which could also be it’s strength.

The scheduled events do not fire based on the servers internal clock alone. A user or a visitor of some sort needs to visit the site before any scheduled events are fired.

After the user / visitor visits WP Cron checks it’s timers and fires the ones that are due.

http://code.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress–wp-23119

You could use this to your advantage in your phpunit tests. This could become even a more viable solution if you’re also using a headless browser such as selenium web driver http://docs.seleniumhq.org/ to test front end functionality as well as the backend.

Leave a Comment