phpunit:运行单个测试时传递,运行所有测试时失败

时间:2014-04-19 21:08:46

标签: php unit-testing phpunit

我遇到的问题是,当我只执行此测试时,会运行特定的测试。当我一起运行所有测试时,此测试失败。从NetBeans或命令行运行具有相同的结果。

与我的套件中的其他测试相比,此测试的独特之处在于它从资源文件夹中获取文件,然后将其复制到测试文件夹(在测试开始时创建并在结束)。

我认为这是相对/绝对路径的问题,但是当我转储源文件夹和目标文件夹时它们是相同的。

这是有问题的代码(复制语句失败):

public function testRemoveRequirementFileValid()
{
    $dbName = 'TRHUtilTest';
    $reqID = 1;
    $userID = 14;
    $trhVersionID = 3;

    //create the database
    DBCreator::CreateProjectDatabaseAndTables($dbName);
    DB::setConnectionInfo($dbName);

    $fileName = 'example.png';
    $sourceFolder = dirname(__FILE__).'\TRHUtilResources\\';
    $targetLink = '../'.FolderDefine::GetFolder('TRH_RequirementLink', $dbName).$trhVersionID.'/'.$reqID.'/';
    FileUtil::MKDirs($targetLink);
    $targetFolder = FolderDefine::GetFolder('TRH_RequirementFile', $dbName).$trhVersionID.'\\'.$reqID.'\\';

    //2 debugging statements
    print PHP_EOL.'Source Folder: '.$sourceFolder.PHP_EOL;
    print PHP_EOL.'Target Folder: '.$targetFolder.PHP_EOL;
    copy($sourceFolder.$fileName, $targetFolder.$fileName);

    $file = new File();
    $file->setName($fileName);
    $file->setLocation($targetLink);
    $file->save();
    $fileID = $file->getID();

    //create the link between a requirement and a file
    TRHUtil::RequirementFileLink($reqID, $fileID, $userID);

    //assert that there is one record, then check the structure and then the contents
    $this->assertEquals(1, DB::getRecordCount($dbName.'.files_reqs'));
    $this->assertEquals(1, TRHUtil::RequirementFileLinkPresent($reqID, $dbName));
    $this->assertEquals(1, TRHUtil::RequirementFileLinkCheck($reqID, $fileID, $dbName));
    $this->assertEquals(1, count(TRHUtil::GetRequirementFileLinkID($reqID, $dbName)));

    TRHUtil::RemoveRequirementFile($reqID, $dbName, $trhVersionID);

    //assert that the records about this file are removed and that the folder where the file was staying is also removed
    $this->assertEquals(0, DB::getRecordCount($dbName.'.files_reqs'));
    $this->assertEquals(0, TRHUtil::RequirementFileLinkPresent($reqID, $dbName));
    $this->assertEquals(0, TRHUtil::RequirementFileLinkCheck($reqID, $fileID, $dbName));
    $this->assertEquals(0, TRHUtil::GetRequirementFileLinkID($reqID, $dbName));
    $this->assertEquals(0, is_dir($targetFolder));

    //remove the project folder
    $projectFolder = FolderDefine::GetFolder('ProjectRoot', $dbName);
    FileUtil::RRMDir($projectFolder);
}

调试语句然后给出:

Source Folder: D:\Inetpub\wwwroot\DTS\tests\util\TRHUtilResources\
Target Folder: D:\Inetpub\wwwroot\DTSfiles\TRHUtilTest\trh\reqs\3\1\

在NetBeans的输出窗口中,我还看到以下内容: copy(D:\Inetpub\wwwroot\DTSfiles\TRHUtilTest\trh\reqs\3\1\example.png): failed to open stream: No such file or directory

但是之前创建了此文件夹。值得注意的是,关于copy-statement的错误只显示了第二个参数,并没有围绕此参数的引号。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,这要归功于我在用户@ AD7six和@Lilshieste的最新评论后添加的一些额外的调试声明。

要创建目标文件夹,我使用了命令:

$targetLink = '../'.FolderDefine::GetFolder('TRH_RequirementLink', $dbName).$trhVersionID.'/'.$reqID.'/';
FileUtil::MKDirs($targetLink);

这意味着创建了以下文件夹:../../../ DTSfiles / TRHUtilTest / trh / reqs / 3/1 /

但是,它应该是:

$targetFolder = FolderDefine::GetFolder('TRH_RequirementFile', $dbName).$trhVersionID.'\\'.$reqID.'\\';
FileUtil::MKDirs($targetFolder);

对应于文件夹:D:\ Inetpub \ wwwroot \ DTSfiles \ TRHUtilTest \ trh \ reqs \ 3 \ 1 \

由于运行此单元测试的起始位置不同,因为所有测试所在的根文件夹不同(root-test-folder是两个目录级别更深),目标文件夹确实已创建,但是在错误的水平。

感谢大家的支持。