7

我正在尝试使用 Behat 进行 BDD 测试。在 Jenkins 上运行构建时,我希望 Behat 在 Web 服务器中打开 PHP 构建,然后在运行测试后将其关闭。怎么做?

基本上我需要运行:

php -S localhost:8000

在我的 BDD 测试中,我尝试过:

/**
 * @Given /^I call "([^"]*)" with email and password$/
 */
public function iCallWithPostData($uri)
{
    echo exec('php -S localhost:8000');
    $client = new Guzzle\Service\Client();
    $request = $client->post('http://localhost:8000' . $uri, array(), '{"email":"a","password":"a"}')->send();
    $this->response = $request->getBody(true);
}

但是当运行 Behat 时,它会卡住而没有任何消息。

4

2 回答 2

4

Solved this myself. I have create two methods. I call the first one before running my BDD tests and the second one after I ran the tests:

private function _startDevelopmentServer($pidfile)
{
    $cmd = 'cd ../../public && php -S 127.0.0.1:8027 index.php';
    $outputfile = '/dev/null';
    shell_exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
    sleep(1);
}

private function _killDevelopmentServer($pidfile)
{
    if (file_exists($pidfile)) {
        $pids = file($pidfile);
        foreach ($pids as $pid) {
            shell_exec('kill -9 ' . $pid);
        }
        unlink($pidfile);
    }
}
于 2012-10-30T11:21:48.903 回答
4

只需将服务器作为构建过程的一部分启动即可。创建一个 ant 任务,它将在运行 behat 之前启动服务器,并在 behat 完成后将其杀死。

我已经成功地使用这种方法来启动和停止 selenium 服务器。

于 2012-10-26T11:04:10.913 回答