Nette tester: Out of the box testing server for end-to-end tests

5 years ago

mystik
Member | 77
+
+1
-

I currently work on implemntation of testign server which allows you to do end-to-end tests of application. Of course you can use Selenium for this, but it is quite slow and unnecessary for basic tests. My implementation use PHP built in CLI server.

Example usage:

<?php

$server = new TestingServer(__DIR__ . "/www/index.php");
$server->run();
$server->getBaseUrl(); // -> http://localhost:8801
$server->getPort(); // -> 8801

//tests

$server->stop();
?>

Running server handle all requests send to given base url. Port is automatically selected from available ports.

For tests I currently use Guzzle. Example:

<?php

$server = new TestingServer(__DIR__ . "/www/index.php");
$server->run();

$client = new GuzzleHttp\Client();

$res = $client->get($server->getBaseUrl() . "/some/url");

Assert::equal(200, $res->getStatusCode());
Assert::contains("something", $res->getBody());

$server->stop();

?>

I also created custom ClientTestCase which combine this to simplify this to:

<?php

class ExampleTest extends ClientTestCase {

  public function getScript() {
    return __DIR__."/../www/index.php";
  }

  public function testShow() {
    $response = $this->get("test/show");
    Assert::equal(200, $response->getStatusCode());
    Assert::contains("something", (string)$response->getBody());
  }

}

?>

Any interest to make this part of Nette tester or stand alone extension? I found it post useful for REST API testing.

Last edited by mystik (2015-01-23 15:29)

5 years ago

Milo
Nette Core | 1149
+
0
-

This is useful thing, not only for the API tests. It is handy for streams or cURL wrappers tests. I'm using a very similiar approach to test my Http client (not published yet). @DavidGrudl opened a related PR (https://github.com/…ter/pull/141) already.

5 years ago

Jan Endel
Member | 1024
+
0
-

Nice, but do you hear about Codeception? It`s look pretty similiar.

Last edited by Jan Endel (2015-01-23 17:06)

5 years ago

Filip Procházka
Moderator | 4693
+
0
-

I've written my own (naive wrapper around native PHP http server) HttpServer, also using it to testing. It's very useful indeed :)

But I would love to see yours!

Last edited by Filip Procházka (2015-01-23 21:10)

5 years ago

enumag
Member | 2128
+
+1
-

Not only it looks similar, Codeception actually uses Guzzle internally.

5 years ago

mystik
Member | 77
+
+1
-

@FilipProcházka Well my implementation looks quite simillar :-) But I will make it ready for publishing and you can look at it if you like.

5 years ago

Milo
Nette Core | 1149
+
+1
-

Here is an another simple HTTP server for tests which I'm using for testing the HTTP clients.

5 years ago

mystik
Member | 77
+
+1
-

@FilipProcházka My implementation is here: https://gist.github.com/…ddcc195036da