简体   繁体   中英

Database test faillure with dbUnit

I'm trying to execute some databasetests with php unit but I keep getting the following errors: 以下错误:

My composer.json look like this:

{
    "require": {
    "phpunit/phpunit": "4.8",
    "phpunit/dbunit": "^2.0"
}

This is the page I'm trying to perform the test on:

<?php 
require_once 'autoload.php';

class testDatabase extends \PHPUnit_Extensions_Database_TestCase {


public function getConnection() {
    $db = new PDO(
        "mysql:host=localhost;dbname=mrf", 
        "", "");
    return $this->createDefaultDBConnection($db, "bulletproof");
}

}
?>

I use the sql autoloader to load the main php classes and that looks like the following:

<?php 
spl_autoload_register(function($class) {
    require_once '../classes/' . $class . '.php';
});
?>

In my opinion, the strange thing is that the regular phpunit tests do run, but the dbUnit test do not. I tried several upgrades of composer, phpUnit and dbUnit downloaded from packagist but without succes.

Would be great if you could help me out, Thanks in advance.

When you run composer install , composer generates file PROJECT_ROOT/vendor/autoload.php . That file contains all stuff needed for autoloading your project dependencies, so based on what I see on the screenshot, you need to require_once it as well as your own.


As a side note: phpunit has an option --bootstrap=filename , which is executed before all tests run. You can run

phpunit --bootstrap=PROJECT_ROOT/vendor/autoload.php databaseTest.php

After that you can create something like phpunit_bootstrap.php, put your require_once 'autoload.php'; here and composers vendor/autoload.php so you can run tests like

phpunit --bootstrap=path/to/your/phpunit_bootstrap.php AnythingYouWantToTest.php

without need to insert require_once 'autoload.php'; in every test file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM