简体   繁体   English

dbUnit 数据库测试失败

[英]Database test faillure with dbUnit

I'm trying to execute some databasetests with php unit but I keep getting the following errors:我正在尝试使用 php 单元执行一些数据库测试,但我不断收到以下错误: 以下错误:

My composer.json look like this:我的 composer.json 看起来像这样:

{
    "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:我使用 sql 自动加载器来加载主要的 php 类,如下所示:

<?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.在我看来,奇怪的是常规 phpunit 测试确实运行,但 dbUnit 测试却没有。 I tried several upgrades of composer, phpUnit and dbUnit downloaded from packagist but without succes.我尝试了从 packagist 下载的 composer、phpUnit 和 dbUnit 的几次升级,但没有成功。

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 .当您运行composer install ,composer 生成文件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.该文件包含自动加载项目依赖项所需的所有内容,因此根据我在屏幕截图中看到的内容,您需要require_once它以及您自己的。


As a side note: phpunit has an option --bootstrap=filename , which is executed before all tests run.附带说明:phpunit 有一个选项--bootstrap=filename ,它在所有测试运行之前执行。 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';之后你可以创建类似 phpunit_bootstrap.php 的东西,把你的require_once 'autoload.php'; here and composers vendor/autoload.php so you can run tests like这里和作曲家 vendor/autoload.php 这样你就可以运行测试

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

without need to insert require_once 'autoload.php';无需插入require_once 'autoload.php'; in every test file.在每个测试文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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