如何让phpunit从文件夹中的所有文件运行测试?

时间:2022-10-15 23:11:51

From what I've read, it seems like I should be able to set up a folder, e.g. tests/ , put a few files in it with unit test classes, and then run phpunit on that file and have it find and run the tests.

根据我的阅读,似乎我应该能够设置一个文件夹,例如测试/,用单元测试类放入一些文件,然后在该文件上运行phpunit,让它找到并运行测试。

For whatever reason, in my installation (on OS X), it thinks the folder tests/ is a file, or so it would seem:

无论出于何种原因,在我的安装中(在OS X上),它认为文件夹tests /是一个文件,或者看起来如此:

$ ls tests
test1.php test2.php
$ phpunit tests/test1.php
PHPUnit 3.5.3 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 5.00Mb

There was 1 failure:

1) FailingTest::testFail
Your test successfully failed!

/Users/****/tmp/tests/test1.php:4

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
$ phpunit tests/test2.php
PHPUnit 3.5.3 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.00Mb

OK (1 test, 1 assertion)
$ phpunit tests
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
  thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102

Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
  thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102

I have what I hope is a fairly standard installation of phpunit via PEAR, following these instructions http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard, on OS X Snow Leopard.

我希望通过PEAR按照这些说明http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard在OS X Snow上进行相当标准的phpunit安装。豹。

$ pear version
PEAR Version: 1.9.1
PHP Version: 5.3.2
Zend Engine Version: 2.3.0
Running on: **** 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
$ phpunit --version
PHPUnit 3.5.3 by Sebastian Bergmann.

I'm hoping someone else out there ran into this issue and it's just a simple fix, or else I'm just doing something wrong?

我希望其他人遇到这个问题,这只是一个简单的修复,否则我只是做错了什么?

3 个解决方案

#1


57  

It is not a bug, it is a feature.

它不是一个bug,它是一个功能。

You have a directory full of .php files, in your case they all contain testcases.

你有一个完整的.php文件目录,在你的情况下它们都包含测试用例。

But as your testsuite grows, you will likely want to have other php files inside tests directory, files that do not contains tests, that exist solely to support tests. Those files should never be executed by PHPUnit itself.

但随着您的测试套件的增长,您可能希望在测试目录中包含其他php文件,不包含测试的文件,仅用于支持测试。这些文件永远不应该由PHPUnit本身执行。

This is a very common scenario.

这是一种非常常见的情况。

So how would PHPUnit know which files it needs to run and which ones not? Checking the file name suffix is one option for doing it - by default PHPUnit considers everything with name ending with Test.php as being a test and ignores everything else.

那么PHPUnit将如何知道它需要运行哪些文件以及哪些文件不运行?检查文件名后缀是执行此操作的一个选项 - 默认情况下,PHPUnit将名称以Test.php结尾的所有内容视为测试并忽略其他所有内容。

You can change that behaviour if you really want to - by creating a file named phpunit.xml in your tests directory with the following content

如果您真的想要,可以更改该行为 - 通过在您的tests目录中使用以下内容创建名为phpunit.xml的文件

<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Name your suite'>
    <directory suffix='.php'>./</directory>
</testsuite>
</phpunit>

Once you have done that, PHPUnit will run all files with '.php' at the end of the file name (in this context file extension is considered to be part of the file name)

完成后,PHPUnit将运行文件名末尾带有“.php”的所有文件(在此上下文中,文件扩展名被视为文件名的一部分)

But it really is better to get used to the convention and name your tests accordingly.

但是,最好习惯惯例并相应地命名测试。

#2


22  

Annoying little quirk, but I figured it out.

恼人的小怪癖,但我想通了。

At least with the default configuration, test files have to end with "Test.php", eg. fooTest.php, or they are not found by the test runner.

至少在默认配置下,测试文件必须以“Test.php”结尾,例如。 fooTest.php,或测试运动员找不到它们。

#3


19  

The simpler way to running test on folder is to add "Test.php" at the end all of your tests and run phpunit specifing your folder like this

在文件夹上运行测试的简单方法是在最后的所有测试中添加“Test.php”并运行phpunit指定你喜欢的文件夹

phpunit .

or

要么

phpunit your_test_folder/.

#1


57  

It is not a bug, it is a feature.

它不是一个bug,它是一个功能。

You have a directory full of .php files, in your case they all contain testcases.

你有一个完整的.php文件目录,在你的情况下它们都包含测试用例。

But as your testsuite grows, you will likely want to have other php files inside tests directory, files that do not contains tests, that exist solely to support tests. Those files should never be executed by PHPUnit itself.

但随着您的测试套件的增长,您可能希望在测试目录中包含其他php文件,不包含测试的文件,仅用于支持测试。这些文件永远不应该由PHPUnit本身执行。

This is a very common scenario.

这是一种非常常见的情况。

So how would PHPUnit know which files it needs to run and which ones not? Checking the file name suffix is one option for doing it - by default PHPUnit considers everything with name ending with Test.php as being a test and ignores everything else.

那么PHPUnit将如何知道它需要运行哪些文件以及哪些文件不运行?检查文件名后缀是执行此操作的一个选项 - 默认情况下,PHPUnit将名称以Test.php结尾的所有内容视为测试并忽略其他所有内容。

You can change that behaviour if you really want to - by creating a file named phpunit.xml in your tests directory with the following content

如果您真的想要,可以更改该行为 - 通过在您的tests目录中使用以下内容创建名为phpunit.xml的文件

<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Name your suite'>
    <directory suffix='.php'>./</directory>
</testsuite>
</phpunit>

Once you have done that, PHPUnit will run all files with '.php' at the end of the file name (in this context file extension is considered to be part of the file name)

完成后,PHPUnit将运行文件名末尾带有“.php”的所有文件(在此上下文中,文件扩展名被视为文件名的一部分)

But it really is better to get used to the convention and name your tests accordingly.

但是,最好习惯惯例并相应地命名测试。

#2


22  

Annoying little quirk, but I figured it out.

恼人的小怪癖,但我想通了。

At least with the default configuration, test files have to end with "Test.php", eg. fooTest.php, or they are not found by the test runner.

至少在默认配置下,测试文件必须以“Test.php”结尾,例如。 fooTest.php,或测试运动员找不到它们。

#3


19  

The simpler way to running test on folder is to add "Test.php" at the end all of your tests and run phpunit specifing your folder like this

在文件夹上运行测试的简单方法是在最后的所有测试中添加“Test.php”并运行phpunit指定你喜欢的文件夹

phpunit .

or

要么

phpunit your_test_folder/.