2014-12-05 59 views
1

我是PHPUnit的新手。我正在開發一個現有系統,添加新功能。我想使用PHPUnit來測試我所做的代碼。但是,系統僅在服務器(CGI)環境(從瀏覽器訪問)上運行,並且從命令行運行時所有內容都會中斷。在cgi環境下使用phpunit

是否可以將PHPUnit設置爲可以從Web瀏覽器調用的測試套件?

回答

0

就我個人而言,我使用Windows任務管理器(cron可以在Linux上執行相同的操作)來生成我的單元測試並將其每天晚上發送到文本文件。

不像我這樣直接在網絡瀏覽器中顯示它,你可以直接在你的服務器上解析結果文件,然後通過電子郵件發送html輸出。因此,您可以像我一樣每天早上簡單地在本地計算機上檢查結果頁面。以下是啓動解決方案的一些代碼(在Windows/PHP上)。

在任務管理器:

動作1C:\xampp\htdocs\PC_administration_interface\Controler\Script\launch_automatic_test.bat

行動2"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

參數動作2http://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的信息,它似乎效率很低。你也可以考慮看看這個帖子(問題和答案):

What is Common Gateway Interface (CGI)?