2015-05-10 52 views
0

我在代碼覆蓋的速度方面遇到了問題。ZF2.4需要很長時間才能運行啓用代碼覆蓋的phpunit

我正在使用ZF2 2.4和由作曲家安裝的十幾個模塊。 當我運行沒有代碼覆蓋率的測試時,結果在5分鐘內。在詹金斯完整的構建需要15分鐘。

隨着代碼覆蓋啓用,整個構建了從0:00至早上5:30。

檢查結果,我意識到,服務/資源庫/控制器類的測試平均耗時2.10分鐘各。什麼是可怕的。

我對tdd開發非常陌生,可以達到100%的代碼覆蓋率,系統有三個模塊和一個10個實體。

我發現的唯一問題是我在創建模擬方面非常熟練,然後無法模擬所有的應用程序服務,例如Repository/Service/Controller正在實時添加,讀取,更新和刪除測試數據庫。

由於我的時間很短,我的主要問題是問題是否真的是不正確的,因爲如果代碼覆蓋率使用讀取哪些文件夾的元信息,當您開始進入供應商文件夾,學說等

所以這可能是主要問題?沒有嘲笑銀行的聯繫?

在我目前的技術知識中,它會對嘲笑產生很大的影響,但在這裏我工作到今天,沒有人使用過模擬,並試圖進行革命。

原理已經應用了互聯網的帖子和其他問題,這裏在stackoverflow中描述的所有技術,是否有任何新技術來加快代碼覆蓋?

有一本關於如何加速PHP代碼覆蓋率的完整說明的手冊。完整的解釋?

感謝收聽。

編輯:

Codeception套件模塊配置:

namespace: ColumnNotNull 
actor: Tester 
paths: 
    tests: test 
    log: build/coverage 
    data: test/_data 
    helpers: test/_support 
settings: 
    strict_xml: true 
    bootstrap: _bootstrap.php 
    colors: true 
    memory_limit: 1024M 
coverage: 
    whitelist: 
      include: 
       - src/* 
      exclude: 
       - src/ColumnNotNull/Fixture/* 
       - src/ColumnNotNull/Module.php* 
    blacklist: 
      include: 
       - build/* 
       - data/* 
       - language/* 
       - public/* 
       - schema/* 
       - test/* 
       - view/* 
       - src/ColumnNotNull/Fixture/* 

Codeception單位套件模塊配置:

class_name: UnitTester 
modules: 
    enabled: [Asserts, UnitHelper, Db] 
    config: 
     Db: 
      dsn: 'mysql:dbname=pibernews;host=localhost' 
      user: 'piber' 
      password: 'secret' 
      dump: data/column-not-null.mysql.sql 
coverage: 
    enabled: true 
    remote_enable : false 

Codeception項目套房

include: 
    - module/Column 
    - module/ColumnImage 
    - module/ColumnNotNull 
paths: 
    log: build 
settings: 
    colors: true 

與同事欺騙我可以從合併結果的不同模塊運行多個測試。

是一個項目,有三個模塊。通過驗收和功能測試。驗收+功能需要15分鐘才能運行,代碼覆蓋的單元需要5個小時。

沒有代碼覆蓋需要10分鐘來運行所有的單元測試。這是可以接受的。但是我想要縮短代碼覆蓋時間,因爲五個小時是不可接受的。

編輯2:

<?php 
// This is global bootstrap for autoloading 
include 'unit/GearBaseTest/ZendServiceLocator.php'; 

$zendServiceLocator = new \GearBaseTest\ZendServiceLocator(); 

_bootstrap.php文件

<?php 
namespace GearBaseTest; 

use Zend\Loader\AutoloaderFactory; 
use Zend\Mvc\Service\ServiceManagerConfig; 
use Zend\ServiceManager\ServiceManager; 
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; 

class ZendServiceLocator 
{ 
    public function __construct() 
    { 
     $this->chroot(); 

     $zf2ModulePaths = array(
      dirname(dirname(realpath(__DIR__ . '/../../'))) 
     ); 

     if (($path = $this->findParentPath('vendor'))) { 
      $zf2ModulePaths[] = $path; 
     } 

     if (($path = $this->findParentPath('module')) !== $zf2ModulePaths[0]) { 
      $zf2ModulePaths[] = $path; 
     } 

     $this->initAutoloader(); 

     $env = getenv('APP_ENV') ? : 'testing'; 


     $applicationConfig = include \GearBase\Module::getProjectFolder().'/config/application.config.php'; 

     $config = array(
      'module_listener_options' => array(
       'module_paths' => $zf2ModulePaths, 
       'config_glob_paths' => array(
        sprintf('config/autoload/{,*.}{global,%s,local}.php', $env) 
       ) 
      ), 
      'modules' => $applicationConfig['modules'] 
     ); 


     $serviceLocator = new ServiceManager(new ServiceManagerConfig()); 
     $serviceLocator->setService('ApplicationConfig', $config); 
     $serviceLocator->get('ModuleManager')->loadModules(); 
     $this->serviceLocator = $serviceLocator; 
    } 

    public function getServiceManager() 
    { 
     return $this->getServiceLocator()->get('ServiceManager'); 
    } 

    public function chroot() 
    { 
     $rootPath = dirname($this->findParentPath('module')); 
     chdir($rootPath); 
    } 

    protected function findParentPath($path) 
    { 
     $dir = __DIR__; 
     $previousDir = '.'; 
     while (! is_dir($dir . '/' . $path)) { 
      $dir = dirname($dir); 
      if ($previousDir === $dir) { 
       return false; 
      } 
      $previousDir = $dir; 
     } 
     return $dir . '/' . $path; 
    } 

    public function getEntityManager() 
    { 
     if (!isset($this->entityManager)) { 
      $this->entityManager = $this->getServiceLocator() 
      ->get('doctrine.entitymanager.orm_default'); 
     } 
     return $this->entityManager; 
    } 

    public function getServiceLocator() 
    { 
     return $this->serviceLocator; 
    } 

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
    { 
     if (!isset($this->serviceLocator)) { 
      $this->serviceLocator = $serviceLocator; 
     } 
     return $this->serviceLocator; 
    } 

    protected function initAutoloader() 
    { 
     $vendorPath = $this->findParentPath('vendor'); 

     $zf2Path = getenv('ZF2_PATH'); 

     if (! $zf2Path) { 
      if (defined('ZF2_PATH')) { 
       $zf2Path = ZF2_PATH; 
      } elseif (is_dir($vendorPath . '/ZF2/library')) { 
       $zf2Path = $vendorPath . '/ZF2/library'; 
      } elseif (is_dir($vendorPath . '/zendframework/zendframework/library')) { 
       $zf2Path = $vendorPath . '/zendframework/zendframework/library'; 
      } 
     } 

     if (! $zf2Path) { 
      throw new RuntimeException(
       'Unable to load ZF2. Run `php composer.phar install` or' . ' define a ZF2_PATH environment variable.' 
      ); 
     } 

     if (file_exists($vendorPath . '/autoload.php')) { 
      include $vendorPath . '/autoload.php'; 
     } 
     include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; 

     AutoloaderFactory::factory(array(
      'Zend\Loader\StandardAutoloader' => array(
       'autoregister_zf' => true, 
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__ 
       ) 
      ) 
     )); 
    } 
} 

ZendServiceLocator.php採集。

+0

你的問題不是很清楚。如果您正在運行包括供應商文件夾內容在內的所有代碼,我預計這需要非常長的時間。否則,也許你可以更多地解釋你的測試是如何設置的。恕我直言,你要謹慎使用模擬器,並且大多數測試不需要觸摸數據庫。 –

+0

我承認我很難表達這個問題,謝謝你幫助我。所以我的測試應該只測試模塊中的文件,但是當Repository在表格中插入一行時,會調用額外的文件示例「Doctrine \ Orm \ EntityManager」,因爲持久性和沖洗我沒有使用模擬。代碼覆蓋設置僅限於我需要的文件夾,我的主要問題是:單元測試使用的所有類也用於代碼覆蓋率,達到其最大深度,從而導致開銷信息並導致過高的延遲? –

+0

正如我所說,我的同事中沒有人使用代碼覆蓋率,而且我是我使用的第一個業務代碼,所以我明白通過使用代碼覆蓋範圍的受限設置足以不用供應商文件夾進行計算。爲最關鍵的測試創建模擬會花費很高的成本,例如確保這是延遲的主要原因。我設置到PHPUnit +代碼覆蓋率工作的設置是非常基本的,但是如果需要,我會忽略它們,哪些設置可能會在時間代碼覆蓋率方面出現問題?然後我在這裏發佈,我們可以逐個檢查。 –

回答

1

您只需要將模塊文件夾列入白名單。

+0

另外,單元測試不應該擊中數據庫。這也會減慢速度。如果您需要點擊數據庫,保存集成測試。 – Mark

相關問題