2017-10-11 106 views
1

,我有一個測試類,像這樣:Laravel測試DatabaseMigrations未運行,我使用Laravel並且PHPUnit會爲我的單元測試所有遷移

class ImportServiceTest extends BaseTestCase 
{ 
    use DatabaseMigrations; 

    public function setUp() 
    { 
     // Parent Setup 
     parent::setUp(); 
    } 

    public function tearDown() 
    { 
     parent::tearDown(); 
    } 
} 

然而,當我運行PHPUnit的,我的測試數據庫只遷移70表格,而不是正常的160.它也不遷移任何以2017年爲前綴的遷移。只有那些以2016開頭的遷移。

我想知道是否有人遇到過這個問題?

這裏是我的phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?> 

<phpunit backupGlobals="false" 
     backupStaticAttributes="false" 
     colors="true" 
     convertErrorsToExceptions="true" 
     convertNoticesToExceptions="true" 
     convertWarningsToExceptions="true" 
     processIsolation="false" 
     stopOnFailure="true" 
     stopOnError="true" 
     stopOnIncomplete="true" 
     stopOnSkipped="false" 
     stopOnRisky="false" 
     syntaxCheck="true" 
     timeoutForSmallTests="1" 
     timeoutForMediumTests="10" 
     timeoutForLargeTests="60" 
     verbose="true" 
     bootstrap="bootstrap/autoload.php"> 
    <testsuites> 
     <testsuite name="Application Test Suite"> 
      <testsuite name="Modules Test Suite"> 
       <directory suffix="Test.php">./Modules/*/Tests</directory> 
      </testsuite> 
      <!--<directory suffix="Test.php">./tests</directory>--> 
     </testsuite> 
    </testsuites> 
    <php> 
     <env name="APP_ENV" value="testing"/> 
     <env name="APP_DEBUG" value="true"/> 
     <env name="APP_LOG_LEVEL" value="debug"/> 
     <env name="DB_CONNECTION" value="testing"/> 
     <env name="APP_NAME" value="Central"/> 
     <env name="CACHE_DRIVER" value="array"/> 
     <env name="SESSION_DRIVER" value="array"/> 
     <env name="QUEUE_DRIVER" value="redis"/> 
     <env name="BROADCAST_DRIVER" value="redis"/> 
     <env name="REDIS_HOST" value="127.0.0.1"/> 
     <env name="REDIS_PORT" value="6379"/> 
    </php> 
    <filter> 
     <whitelist processUncoveredFilesFromWhitelist="false"> 
      <directory suffix=".php">./tests</directory> 
      <directory suffix=".php">./tests/coverage</directory> 
      <directory suffix=".php">./database/migrations</directory> 
      <directory suffix=".php">./src</directory> 
      <directory>./vendor</directory> 
      <exclude> 
       <directory suffix=".php">./tests</directory> 
       <directory suffix=".php">./tests/coverage</directory> 
       <directory suffix=".php">./src/resources</directory> 
       <directory>./vendor</directory> 
      </exclude> 
     </whitelist> 
    </filter> 
    <!--<logging>--> 
    <!--<log type="coverage-html"--> 
    <!--target="./tests/coverage/"--> 
    <!--lowUpperBound="35"--> 
    <!--highLowerBound="70"/>--> 
    <!--</logging>--> 
    <groups> 
     <exclude> 
      <group>performance</group> 
     </exclude> 
    </groups> 
</phpunit> 

,因爲我已經使用PHPUnit的前從來沒見過這種行爲我很爲難。

我的測試連接設置如下:

  # Testing Database Credentials - set in production environment or .env 
    'testing' => [ 
     'driver' => 'mysql', 
     'host' => env('DB_TESTING_HOST', 'localhost'), 
     'port' => env('DB_TESTING_PORT', '3306'), 
     'database' => env('DB_TESTING_DATABASE', 'testing'), 
     'username' => env('DB_TESTING_USERNAME', 'homestead'), 
     'password' => env('DB_TESTING_PASSWORD', 'secret'), 
     'charset' => 'utf8', 
     'collation' => 'utf8_unicode_ci', 
     'prefix' => '', 
     'strict' => true, 
     'engine' => null, 
    ], 

謝謝!

+0

運行'php artisan migrate --db = test_db_name'時會發生什麼? –

+0

「測試」連接上的設置是什麼? – Camilo

+0

從config/database.php中添加了測試連接設置 – liamjnorman

回答

1

我和你有同樣的問題,並在第一次被難住。但我解決了它。

之前,我給予解決,讓我解釋一下我的設置:

  1. 我不使用「內存」的SQLite數據庫進行測試。我起初是這樣,但是當我決定開始嚴格使用單獨的PostgreSQL數據庫來處理PHPUnit時,我遇到了這個問題。

  2. 我用遷移種子試驗數據的基礎上,應用ENV,這樣我可以保證的東西在一定的順序發生,所以我知道這在當時存在的列。

在我的情況是發生了什麼事是我的一個遷移是拋出一個從未顯示的異常。所以工匠只會停留在那個移徙中,而phpunit會繼續。我發現我爲我的'local'環境播種東西,但不是我的'testing'環境。確保'testing'環境適當播種後(不一定等同於'local',請注意),所有事情都再次開始。

相關問題