我是PHPUnit的新手。我正在开发现有系统,添加新功能。我想用PHPUnit来测试我制作的代码。但是,系统仅在服务器(CGI)环境(从浏览器访问)上运行,并且从命令行运行时一切都会中断。
是否可以设置PHPUnit来创建一个可以从Web浏览器调用的测试套件?
答案 0 :(得分:0)
就个人而言,我使用Windows任务管理器(cron可以在linux上做同样的事情)来生成我的单元测试并每晚将其发送到文本文件。
您可以直接在服务器上解析结果文件,然后通过电子邮件发送html输出,而不是像我一样直接在Web浏览器中显示它。因此,您可以像我一样每天早上检查本地计算机上的结果页面。这是启动解决方案的一些代码(在Windows / PHP上)。
在任务管理器中:
行动1 :C:\xampp\htdocs\PC_administration_interface\Controler\Script\launch_automatic_test.bat
行动2 :"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
参数操作2 :http://localhost/PC_administration_interface/view/unit_test.php?DISPLAY_RESULT=TRUE
Launch_automatic_test.bat
@echo off
call %~DP0\launch_xampp.bat
call %~DP0\..\..\..\..\xampp_shell.bat
cls
call %~DP0\run_unit_test.bat > "%~DP0\..\test_result.txt"
launch_xampp_bat
@echo off
tasklist | find /i "xampp-control.exe" && goto :eof
start /b "xampp-control.exe" "C:\xampp\xampp-control.exe"
以下是 run_unit_tets.bat
的示例@ECHO OFF
CLS
REM GEM_MECHANIC_TESTS
ECHO.
ECHO FILE : GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GEM_MECHANIC/CONTROLER/TEST/GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.
REM GEM_MECHANIC_INTERFACE_BUILDER_TESTS
ECHO.
ECHO FILE : HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
CALL PHPUNIT %~DP0/../../../GEM_MECHANIC/CONTROLER/TEST/TEST_INTERFACE_BUILDER/HTML_GEM_MECHANIC_MANAGER_TEST.PHP
ECHO.
ECHO.
然后我启动一个网页来解析我的结果并将其显示在网络浏览器中:
public static function fetchTestResult(&$errorMessage){
$echoString = '';
$failure = false;
$fileContent = '';
$errorMessage = '';
$dbManager = GeneralDbManager::getInstance();
if (file_exists(realpath(dirname(__FILE__)) . '/test_result.txt')) {
@$fileContent = file(realpath(dirname(__FILE__)) . '/test_result.txt');
if ($fileContent === false){
$errorMessage = $dbManager->getErrorMessage('UNIT_TEST_RESULT_LOG_ERR', "An error happened while reading the unit tests results log.");
}
else{
unlink(realpath(dirname(__FILE__)) . '/test_result.txt');
if (file_exists(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat')) {
unlink(realpath(dirname(__FILE__)) . '/script/temp_unit_test.bat');
}
$echoString = HtmlTagBuilder::createCustomTextArea("TA_UNIT_TEST_RESULT", TextAlign::ALIGN_LEFT, false, 1100, 500);
foreach ($fileContent as $line){
if (StringManager::stringStartWith($line, "FILE :")){
$failure = false;
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::TITLE_LINE);
}
elseif (StringManager::stringStartWith($line, "time:")){
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::WARNING_LINE);
}
elseif (StringManager::stringStartWith($line, "OK (")){
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::SUCCESS_LINE);
}
elseif ((StringManager::stringStartWith($line, "There ") and strpos($line, "failure") !== false)
or $failure === true){
$failure = true;
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
}
elseif (strpos(strtolower($line), "failure") !== false){
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::ERROR_LINE);
}
else{
$echoString .= HtmlTagBuilder::addLineCustomTextArea($line, CustomTextAreaLine::REGULAR_LINE);
}
}
$echoString .= '</DIV><br><br>';
}
}
else{
$errorMessage = $dbManager->getErrorMessage('UNIT_TEST_NO_RESULT_LOG_ERR', "You must run the unit test and generate the test log before displaying it.");
}
return $echoString;
}
附加说明:我目前正在获取有关CGI的一些信息,而且它似乎非常低效。您也可以考虑看看这篇文章(问题和答案):