2017-10-11 34 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',请注意),所有事情都再次开始。