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

mstdmstd's avatar

How to run set of tests in cron ?

Hello.

In my Laravel 5.7 app I make several http tests and they work as I excepect when I run them manualy. If there is a way/tool/plugin to run set of tests (say in cron) and got results of them(or better results having errors) and notify admin of the site by email ?

Thanks!

0 likes
4 replies
Talinon's avatar

If you are speaking about unit testing, I don't know of any tool to handle this. Although, I imagine you could just make use of the onNotSuccessfulTest method, which should fire for every failed test.

Something like:


    protected function onNotSuccessfulTest(\Throwable $e)
    {

        Mail::send('emails.testFailed', ['exception' => $e], function($message)
        {
            $message->to('[email protected]', 'Admin Name')->subject('Test Failed');
        });        
    }    


You can handle all the formatting for the email within the view. You could place the above code in your BaseTest class, or into a Trait which you could use only on the test files you want this functionality.

Then just set up your cron job to run the test suite.

You'd also probably want to consider adding an environment check, so you're not constantly sending out emails upon failures when writing tests.

1 like
shez1983's avatar

generally speaking you can try/catch a piece of code and in catch you can LOG that code.. then use slack integration to send you information..

mstdmstd's avatar

I do not see onNotSuccessfulTest mentioned here https://laravel.com/docs/5.7/http-tests ?

Googling I found onNotSuccessfulTest mentioned in phpunit, but I hardly remember it, I worked with it many years ago.

I made some tests and looks like if I define method

    protected function onNotSuccessfulTest(\Throwable $e)
    {
        \Log::info( ' HomepageTest Error::'. $e->getMessage() ); // does not work
        ...

Then in case of error in my tests erros are not shown in console and in console I see that all tests are ok? That is something not good for me?

While debugging I on my local laptop I pefer something simpler in debuggin not email

But calling method \Log::info( I got error

Error: Call to undefined method Illuminate\Support\Facades\Log::info()

that was strange, as far as I remember that method is common. not in tests? can you advice some simple debugging way?


Thanks!
D9705996's avatar

If you want to get an email when your tests fail you could write a small bash script and schedule via cron

#!/bin/bash

cd /path/to/project
 
phpunit >/tmp/test_results.txt 2>&1

RESULT=$?

if [ $RESULT -ne 0 ]; then
  cat /tmp/test_results.txt | mail -s "Tests have failed" [email protected]
fi

rm -f /tmp/test_results.txt 

Then add the script to your crontab e,g run daily at 1am

0 1 * * * /your/script.sh

Just remember and make your script executable e.g. chmod 750 /your/script.sh you will also need to make sure you install and configure the mail client software. The script will need tweaked your local environment, e.g. file paths, email addresses, etc.

1 like

Please or to participate in this conversation.