2014-04-19 89 views
2

我有执行此测试时运行特定测试的问题。当我一起运行所有测试时,此测试失败。从NetBeans内部或从命令行运行具有相同的结果。phpunit:单一测试运行时通过,当所有测试运行时都失败

与我的套件中的其他测试相比,这个测试是独一无二的,它从一个资源文件夹获取一个文件,然后将其复制到一个测试文件夹中(该文件夹在测试开始时创建并销毁最后)。

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

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

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

但是这个文件夹是以前创建的。值得注意的是,关于复制语句的错误仅显示第二个参数,并且此参数周围没有引号。

+2

你需要显示你有问题的测试,以及它如何失败,以获得帮助。这听起来像是测试污染(测试会影响全局状态,导致以后的测试失败,或者从假设状态开始,而不是在执行“测试”之前确保测试场景与预期一致)。 – AD7six

+0

究竟是什么意思呢? –

+0

@ AD7six我添加了有问题的代码 – Tornado

回答

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 \请求数\ 3 \ 1 \

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

感谢所有的支持。

+0

很高兴你听到你发现问题。现在事情正在发挥作用,这对于[开始重构]是一个很好的机会(http://martinfowler.com/articles/workflowsOfRefactoring/)。 :-) – Lilshieste

+0

你有一个好点,但实际上这个测试更多的是一个集成测试,而不是单元测试(在这里使用的所有其他函数(例如FileUtil :: MKDirs),其他更小的单元测试已经涵盖了这些测试)。 – Tornado

+0

你是对的,你应该保持这个测试。这个测试下面有一个等待出去的设计,虽然说明了你遇到的困难。但是,这是你的问题脱离主题,所以我为分心而道歉。 – Lilshieste

相关问题