Nette\Tester and Tracy working hand by hand?

4 years ago

Honza Kuchař
Backer | 1650
+
0
-

Hi Traciers!

What is the best forkflow (e.g. what works for you the best) for debugging tests? Look into these two scenarious to get the sense of what I've written.

  1. When running tests on CI i want to know IF tests failed and which tests failed.

    solution: run tester tests. Done. Works by default.

  2. When I'm on dev machine I want to know WHY something failed. So let's look into Tracy's log folder. Nothing there. Why? Can I enable logging exceptions (of course) to tracy's log folder when running test?

4 years ago

Milo
Nette Core | 1148
+
+1
-

There are many possibilities. It depends on how you run the tests and which errors you want to log.

Both the Tester and Tracy installs own error and exception handlers. I'll assume that you want to completly preserve the Tester behaviour, and you want to log all errors and exceptions additionally.

# test's bootstrap.php

require __DIR__ . '/vendor/autoload.php';

# Standard setup
date_default_timezone_set('Europe/Prague');
Tester\Environment::setup();

# On dev-machine
if (development) {
    $_prevEx = set_exception_handler(function(\Exception $e) use (& $_prevEx) {
        Tracy\Debugger::log($e, Tracy\ILogger::EXCEPTION);
        if ($_prevEx !== NULL) {
            $_prevEx($e);
        }
    });

    $_prevEr = set_error_handler(function($severity, $message, $file, $line) use (& $_prevEr) {
        $e = new \ErrorException($message, 0, $severity, $file, $line);
        Tracy\Debugger::log($e, Tracy\ILogger::EXCEPTION);
        if ($_prevEr === NULL) {
            return FALSE;
        } else {
            call_user_func_array($_prevEr, func_get_args());
        }
    });
}

Maybe is better to enclose it into some own scope.

Or if you want to log the failed assertions only, there is a simpler way:

Tester\Assert::$onFailure = function(Tester\AssertException $e) {
    Tracy\Debugger::log($e, Tracy\ILogger::EXCEPTION);
    throw $e;
};

4 years ago

Filip Procházka
Moderator | 4693
+
0
-

It would be cool to have event on the Environment, so Tracy could be hooked “after” the default Tester error & exception handlers.

4 years ago

Milo
Nette Core | 1148
+
0
-

Agree. I'm thinking about that some time.

4 years ago

Aurielle
Member | 1283
+
0
-

+1, I had to copy handleException() and error and exception handlers to make logging possible…

4 years ago

Honza Kuchař
Backer | 1650
+
0
-

Thanks for helpful answer! Is anyone going to send PR?