PHPUnit类无法找到自己
PHPUnit和测试新手,一直遵循Teamtree House的指南。但我被困在这一点上,不知道是否有人能帮忙。以下是我的文件中的详细信息: phpunit.xml---在根目录中PHPUnit类无法找到自己,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,PHPUnit和测试新手,一直遵循Teamtree House的指南。但我被困在这一点上,不知道是否有人能帮忙。以下是我的文件中的详细信息: phpunit.xml---在根目录中 <phpunit backupGlobals="true" bootstrap="tests/bootstrap.php"> <!-- Blacklist the vendor folder --> <filter> <blacklist>
<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>
但是当我运行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
这真让我困惑,我根本找不到解决办法。如果有人有一些见解,我们将不胜感激 您的问题在于所有的附件。他们试图包含错误的文件 测试/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);
<?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"
);
}
}
<?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;
}
}
<?php
// Let's use absolute paths instead with the help of __DIR__ which
// will give us the path to the current folder.
require __DIR__ . '/../vendor/autoload.php'; // Up one folder where the vendor is
// Removed the include for PigLatinTest since PHPUnit will handle that.
// Again, let's use __DIR__ to solve the path issues.
$loader = new Twig_Loader_Filesystem(__DIR__ . '/../src');
$twig = new Twig_Environment($loader);
// If your PigLatin-class isn't loaded with autoloading (in your composer.json),
// let's include it in here. Again, with the help of __DIR__
require __DIR__ . '/../src/PigLatin.php';
我想你发布了两次boostrap而不是phpunit.xml。@MagnusEriksson-谢谢你。我修正了!您有两个不同的供应商文件夹吗?通常,您有一个供应商,您只需要包含一次(在引导文件中)。测试文件不需要包含内容。这就是引导的全部内容。@MagnusEriksson-nope只有一个供应商文件夹。一直在一行一行地复制教程行,但不确定它是否过时等。这100%有效。非常感谢你,也感谢你解释了一切。
<?php
// Let's use absolute paths instead with the help of __DIR__ which
// will give us the path to the current folder.
require __DIR__ . '/../vendor/autoload.php'; // Up one folder where the vendor is
// Removed the include for PigLatinTest since PHPUnit will handle that.
// Again, let's use __DIR__ to solve the path issues.
$loader = new Twig_Loader_Filesystem(__DIR__ . '/../src');
$twig = new Twig_Environment($loader);
// If your PigLatin-class isn't loaded with autoloading (in your composer.json),
// let's include it in here. Again, with the help of __DIR__
require __DIR__ . '/../src/PigLatin.php';