howto: run tests in browser

petrb
Member | 8
+
0
-

Could you please give a short example on how to run the tests in browser?

My current approach is to rename *.phpt to *.php and add <?php echo "<pre>"; ?> on the beginning to make test result readable in browser

Comments

greeny:

One question – why do you need to run tests in browser?

5 years ago
petrb:

not possible to use CLI on webservers. The tester docs is talking about supporting both CLI and browser, waiting for someone who knows how to use the second one

5 years ago
greeny:

Well then why dont you clone the project to your local machine and run tests there?

5 years ago
petrb:

because local machine environment is different than webservers. if we agree that we can enumerate all posibilities how to do it “the other way”, then how to do it as the question and manual says, with browser?

5 years ago
Jan Tvrdík
Nette guru | 2563
+
+3
-

You need to configure web server to process PHP in *.phpt files and then you can just open it in a browser. Because tests usually do not output anything you don't need to add <pre> or change content type to text/plain.

Milo
Nette Core | 1151
+
+2
-

As @JanTvrdík wrote, the best way is to configure Apache/Nginx to handle *.phpt too. If not possible, do it by some script with only require 'path/to/test.phpt';. But be careful about security. The purpose is just to run test file as an ordinary PHP script.

In test's bootstrap, you can distinguish between current environments and do setup (Content-Type for example):

if (getenv(Tester\Environment::RUNNER)) {
    # Running by Tester (e.g. vendor/bin/tester tests/MyTest.phpt)
} elseif (PHP_SAPI === 'cli') {
    # Running as ordinary CLI script (e.g. php tests/MyTest.phpt)
} else {
    # Browser
}