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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4047199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:48:45  来源:igfitidea点击:

How can i get phpunit to run tests from all files in a folder?

phpphpunitpear

提问by jsdalton

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.

从我读过的内容来看,似乎我应该能够设置一个文件夹,例如 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 安装相当标准的 phpunit,按照这些说明http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard,在 OS X Snow 上豹。

$ 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?

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

回答by Anti Veeranna

It is not a bug, it is a feature.

这不是一个错误,它是一个功能。

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.phpas 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

如果您真的想要,您可以更改该行为 - 通过在您的测试目录中创建一个名为 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.

但最好习惯于约定并相应地命名您的测试。

回答by Gonzalo Cao

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/.

回答by jsdalton

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,或者测试运行程序找不到它们。