簡體   English   中英

如何讓phpunit從文件夾中的所有文件運行測試?

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

從我讀過的內容來看,似乎我應該能夠設置一個文件夾,例如tests /,用單元測試類放入一些文件,然后在該文件上運行phpunit並讓它找到並運行測試。

無論出於何種原因,在我的安裝中(在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

我希望通過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.

我希望其他人遇到這個問題,這只是一個簡單的修復,否則我只是做錯了什么?

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

你有一個完整的.php文件目錄,在你的情況下它們都包含測試用例。

但隨着您的測試套件的增長,您可能希望在測試目錄中包含其他php文件,不包含測試的文件,僅用於支持測試。 這些文件永遠不應該由PHPUnit本身執行。

這是一種非常常見的情況。

那么PHPUnit將如何知道它需要運行哪些文件以及哪些文件不運行? 檢查文件名后綴是執行此操作的一個選項 - 默認情況下,PHPUnit將名稱以Test.php結尾的所有內容視為測試並忽略其他所有內容

如果您真的想要,可以更改該行為 - 通過在您的tests目錄中使用以下內容創建名為phpunit.xml的文件

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

完成后,PHPUnit將運行文件名末尾帶有“.php”的所有文件(在此上下文中,文件擴展名被視為文件名的一部分)

但是,最好習慣慣例並相應地命名測試。

在文件夾上運行測試的簡單方法是在最后的所有測試中添加“Test.php”並運行phpunit指定你的文件夾,如下所示

phpunit .

要么

phpunit your_test_folder/.

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

至少在默認配置下,測試文件必須以“Test.php”結尾,例如。 fooTest.php,或測試運動員找不到它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM