簡體   English   中英

phpUnit-測試類中不是測試的函數

[英]phpUnit - Functions in test class that aren't tests

我正在phpunit中的報告之間進行一些速度比較,因為我試圖找出一個優化問題。

我有一些功能不一定要測試,但是也不屬於項目的功能。 我使用它們是為了使我的測試小巧易讀。 我正在使用的函數對傳遞給我的參數執行cUrl操作。

因此,我正在運行兩個Urls(一個項目的兩個版本,一個版本以其原始形式,一個版本進行了優化),並查看它們是否返回彼此相等的文本。 我不會在應用程序本身中執行此操作。 我這樣做是因為它比嘗試找出正確的函數調用要快,因為該項目有點混亂。

所以我有一個像這樣的測試:

public function testOne(){

    $results = $this->testRange(13,1,2013,16,1,2013);
    $this->assertEquals($results['opt'], $results['non_opt']);

}//tests

還有我的兩個非測試功能:

protected function testRange($fromDay,
                          $fromMonth,
                          $fromYear,
                          $toDay,
                          $toMonth,
                          $toYear){

    $this->params['periodFromDay'] = $fromDay;
    $this->params['periodFromMonth'] = $fromMonth;
    $this->params['periodFromYear'] = $fromYear;
    $this->params['periodToDay'] = $toDay;
    $this->params['periodToMonth'] = $toMonth;
    $this->params['periodToYear'] = $toYear;

    $this->data['from']=$fromDay."-".$fromMonth."-".$fromYear;
    $this->data['to']=$toDay."-".$toMonth."-".$toYear;;

    return $this->testRunner();

}//testOneDay


protected function testRunner(){

    //include"test_bootstrap.php";
    $response = array();

    foreach($this->types as $key=>$type){

        $params = http_build_query($this->params);
        $url=$this->paths[$type];
        $curl_url = $url."?".$params;
        $ch = curl_init($curl_url);
        $cookieFile = "tmp/cookie.txt";

        if(!file_exists($cookieFile))
        {

            $fh = fopen($cookieFile, "w");
            fwrite($fh, "");
            fclose($fh);

        }//if

        curl_setopt($ch,CURLOPT_COOKIEFILE,$cookieFile);
        curl_setopt($ch,CURLOPT_COOKIEJAR,$cookieFile);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        $result[$type] = curl_exec($ch);

        $dump = "logs/report_results/".
                 $this->data['from']."_".
                 $this->data['to']."_".
                 $type.".txt";

        $fh = fopen($dump, "w");
        fwrite($fh, $result[$type]);
        fclose($fh);

    }//foreach

    return $result;

}//testRunner

我想知道

答:可以在測試文件中編寫函數,並讓phpunit忽略它們,或者是否有放置它們的合適位置。

B:有一種更明智的方式來處理這種事情。 我喜歡這種方法,但我願意接受建議。

PHPUnit將忽略名稱不以“ test *”開頭且沒有@Test注釋的任何方法,因此可以將任何內容放入私有幫助器函數中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM