php 让 PHPUnit 工作 - 包含路径设置不正确?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4091862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting PHPUnit Working - Include Path not set correctly?
提问by Jarrod Nettles
I'm trying to get PHPUnit working on my development environment but I've hit a bit of a roadblock when it comes to including PHPUnit in my scripts. I know that I need to set the include path on PHP but every combination I've tried fails without the compiler seeing the PHPUnit_Framework_TestCase class.
我正在尝试让 PHPUnit 在我的开发环境中工作,但在将 PHPUnit 包含在我的脚本中时遇到了一些障碍。我知道我需要在 PHP 上设置包含路径,但是我尝试过的每个组合都失败了,编译器没有看到 PHPUnit_Framework_TestCase 类。
I just ran updates on PHP and PEAR and PHPUnit is installed on the computer because I can access it through the command line just fine.
我只是在 PHP 和 PEAR 上运行更新,并且 PHPUnit 安装在计算机上,因为我可以通过命令行访问它就好了。
PHPUnit is installed at /usr/share/php/PHPunit
PHPUnit 安装在/usr/share/php/PHPunit
Pear is at /usr/share/php/PEAR
Pear 位于/usr/share/php/PEAR
Is there something I'm missing? This is my first time trying to use PHPUnit or even something from PEAR for that matter. I'm on Ubuntu 10.10. Any help would be appreciated.
有什么我想念的吗?这是我第一次尝试使用 PHPUnit 甚至 PEAR 的某些东西。我在 Ubuntu 10.10 上。任何帮助,将不胜感激。
Edit - There is nothing in the include path in my PHP ini. Right now the code is just
编辑 - 我的 PHP ini 的包含路径中没有任何内容。现在的代码只是
<?php
class Stacktest extends PHPUnit_Framework_TestCase
{
}
I have no idea what to include or what to set in the include path because it seems that for all the info on the web about PHPUnit, this little bit of information is critically absent.
我不知道在包含路径中包含什么或设置什么,因为对于网络上关于 PHPUnit 的所有信息,这一点信息似乎严重缺失。
采纳答案by Poelinca Dorin
If you installed phpunit correctly (through PEAR), then there is no need for an include file; you just need to change the way you use it to test php files (e.g. you use it to test if the file worked by going to the browser type localhost). With phpunit you can use the command line; chapter 5gives the same example using the command line (I would assume it's a standard). So if you got it installed correctly you can do this:
如果您正确安装了 phpunit(通过 PEAR),则不需要包含文件;您只需要更改使用它来测试 php 文件的方式(例如,您可以通过转到浏览器类型 localhost 来使用它来测试文件是否有效)。使用 phpunit,您可以使用命令行;第 5 章使用命令行给出了相同的示例(我认为它是标准的)。因此,如果您正确安装了它,则可以执行以下操作:
File ExampleTest.php, located at the root of localhost (for me this is /var/www):
class ExampleTest extends PHPUnit_Framework_TestCase { public function testOne() { $this->assertTrue(FALSE); } }
Open a console (terminal on Mac or Linux, command prompt on Win), navigate to your localhost document root (where you saved ExampleTest.php) and type the following:
phpunit --verbose ExampleTest.php
You should see:
PHPUnit 3.4.13 by Sebastian Bergmann. F Time: 1 second, Memory: 6.50Mb There was 1 failure: 1) ExampleTest::testOne Failed asserting that <boolean:false> is true. /var/www/ExampleTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
文件 ExampleTest.php,位于 localhost 的根目录(对我来说这是 /var/www):
class ExampleTest extends PHPUnit_Framework_TestCase { public function testOne() { $this->assertTrue(FALSE); } }
打开控制台(Mac 或 Linux 上的终端,Win 上的命令提示符),导航到您的 localhost 文档根目录(您保存 ExampleTest.php 的位置)并键入以下内容:
phpunit --verbose ExampleTest.php
你应该看到:
PHPUnit 3.4.13 by Sebastian Bergmann. F Time: 1 second, Memory: 6.50Mb There was 1 failure: 1) ExampleTest::testOne Failed asserting that <boolean:false> is true. /var/www/ExampleTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
Notes: All the above assumes you installed phpunit correctly (as stated in chapter 3) and you restarted apache after that .
注意:以上所有假设您正确安装了 phpunit(如第 3 章所述),然后您重新启动了 apache。
if you really want to run tests in your browser use the following code
如果您真的想在浏览器中运行测试,请使用以下代码
# error reporting
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
# include TestRunner
require_once 'PHPUnit/TextUI/TestRunner.php';
# our test class
class ExampleTest extends PHPUnit_Framework_TestCase
{
public function testOne()
{
$this->assertTrue(FALSE);
}
}
# run the test
$suite = new PHPUnit_Framework_TestSuite('ExampleTest');
PHPUnit_TextUI_TestRunner::run($suite);
Edit
编辑
Just spotted Ubuntu 10.10 in your question. For Ubuntu install I recommend this: in terminal do:
刚刚在您的问题中发现了 Ubuntu 10.10。对于 Ubuntu 安装,我建议这样做:在终端中执行:
sudo pear uninstall phpunit/PHPUnit
sudo apt-get install phpunit
sudo /etc/init.d/apache2 restart
Note: Don't issue the first line if you didn't install phpunit through pear. The last line seems to be needed (in my case at least).
注意:如果不是通过 pear 安装 phpunit,请不要发出第一行。似乎需要最后一行(至少在我的情况下)。
sudo /etc/init.d/apache2 reload # Or
sudo service apache2 restart
回答by xrstf
As of PHPUnit 3.5, you have to include the autoloader yourself:
从 PHPUnit 3.5 开始,您必须自己包含自动加载器:
require 'PHPUnit/Autoload.php'; // PEAR should be in your include_path
回答by Jeff Davis
Add this line to your php.ini
:
将此行添加到您的php.ini
:
include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/PHPunit:/usr/share/php/PEAR"
Save the file and restart apache.
保存文件并重新启动 apache。
Also, make sure you editing the correct php.ini
. Try using locate php.ini
to find all the places it might be hiding. It's usually under the /usr
directory somewhere.
另外,请确保您编辑正确的php.ini
. 尝试使用locate php.ini
找到它可能隐藏的所有地方。它通常位于/usr
某个目录下。
回答by Travis Webb
Make sure every time to use include() or require(), you prefix the actual file name with dirname(__FILE__)
. This ensures that the file you are including is at the path you specify relative to the actual file that the include is in. By deftault, PHP includes relative to the file that is invoked to start the program.
确保每次使用 include() 或 require() 时,在实际文件名前加上dirname(__FILE__)
. 这可确保您包含的文件位于您指定的相对于包含文件所在的实际文件的路径中。默认情况下,PHP 包含相对于被调用以启动程序的文件。
回答by JTHouseCat
I was able to get it working by using set_include_path at the top of my script (see example here http://www.siteconsortium.com/h/p1.php?id=php002), and then I also had to include since I was running a Selenium test, but it worked.
我能够通过在我的脚本顶部使用 set_include_path 来让它工作(参见这里的示例http://www.siteconsortium.com/h/p1.php?id=php002),然后我也必须包括因为我正在运行 Selenium 测试,但它有效。
require_once ('PHPUnit/Autoload.php');
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';