對PHPUnit不熟悉並且測試並且一直遵循Teamtree House's Guide。但我堅持在這一點上,想知道是否有人可以幫忙。下面是我的文件細節:PHPUnit類找不到自己
phpunit.xml ---
<phpunit backupGlobals="true" bootstrap="tests/bootstrap.php">
<!-- Blacklist the vendor folder -->
<filter>
<blacklist>
<directory>vendor</directory>
</blacklist>
</filter>
<!-- Add the main testsuite -->
<testsuite>
<directory>tests</directory>
</testsuite>
</phpunit>
bootstrap.php中---在./tests/bootstrap.php
<?php
// Get autoloader
require './vendor/autoload.php';
// Get tests
require './tests/PigLatinTest.php';
// Initialise twig
$loader = new Twig_Loader_Filesystem('./src');
$twig = new Twig_Environment($loader);
根
PigLatinTest.php ---在./tests/PigLatinTest.php
<?php
require 'vendor/autoload.php';
require 'src/PigLatin.php';
class PigLatinTest extends PHPUnit\Framework\TestCase
{
/**
* @test PigLatin
*/
public function englishToPigLatinWorksCorrectly()
{
/**
* Given I have an english word
* If I pass that word to my PigLatin converter
* I get back the correctly transformed version
*/
$word = 'test';
$expectedResult = 'esttay';
$pigLatin = new PigLatin();
$result = $pigLatin->convert($word);
$this->assertEquals(
$expectedResult,
$result,
"PigLatin conversion did not work correctly"
);
}
}
PigLatin.php ---在./src/PigLatin.php
<?php
class PigLatin
{
public function convert($word)
{
// Remove first letter of the word
$first_letter = substr($word, 0, 1);
$new_word = substr($word, 1, strlen($word) - 1);
$new_word .= $first_letter . 'ay';
return $new_word;
}
}
當我運行命令PHPUnit的在我的終端,我得到了以下的輸出:
PHPUnit 6.2.3 by Sebastian Bergmann and contributors.
Time: 68 ms, Memory: 10.00MB
No tests executed!
但是當我運行phpunit PigLatinTest.php我得到以下錯誤:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'PigLatinTest' could not be found in 'PigLatinTest.php'. in phar:///usr/local/bin/phpunit/phpunit/Runner/StandardTestSuiteLoader.php:101
這實在讓我很困惑,而且我根本找不到SO的解決方案。如果有人有一些洞察力,將不勝感激!
我想你發佈了兩次boostrap而不是phpunit.xml。 –
@MagnusEriksson - 謝謝你。已修改! –
你有兩個不同的供應商文件夾?通常,你有一個供應商,你只需要在你的引導文件中包含_once_。測試文件不需要包含東西。這就是Bootstrap的全部內容。 –