內容

名稱

prove - 透過 TAP 測試架構執行測試。

用法

prove [options] [files or directories]

選項

布林選項

-v,  --verbose         Print all test lines.
-l,  --lib             Add 'lib' to the path for your tests (-Ilib).
-b,  --blib            Add 'blib/lib' and 'blib/arch' to the path for
                       your tests
-s,  --shuffle         Run the tests in random order.
-c,  --color           Colored test output (default).
     --nocolor         Do not color test output.
     --count           Show the X/Y test count when not verbose
                       (default)
     --nocount         Disable the X/Y test count.
-D   --dry             Dry run. Show test that would have run.
-f,  --failures        Show failed tests.
-o,  --comments        Show comments.
     --ignore-exit     Ignore exit status from test scripts.
-m,  --merge           Merge test scripts' STDERR with their STDOUT.
-r,  --recurse         Recursively descend into directories.
     --reverse         Run the tests in reverse order.
-q,  --quiet           Suppress some test output while running tests.
-Q,  --QUIET           Only print summary results.
-p,  --parse           Show full list of TAP parse errors, if any.
     --directives      Only show results with TODO or SKIP directives.
     --timer           Print elapsed time after each test.
     --trap            Trap Ctrl-C and print summary on interrupt.
     --normalize       Normalize TAP output in verbose output
-T                     Enable tainting checks.
-t                     Enable tainting warnings.
-W                     Enable fatal warnings.
-w                     Enable warnings.
-h,  --help            Display this help
-?,                    Display this help
-V,  --version         Display the version
-H,  --man             Longer manpage for prove
     --norc            Don't process default .proverc

帶有參數的選項

-I                     Library paths to include.
-P                     Load plugin (searches App::Prove::Plugin::*.)
-M                     Load a module.
-e,  --exec            Interpreter to run the tests ('' for compiled
                       tests.)
     --ext             Set the extension for tests (default '.t')
     --harness         Define test harness to use.  See TAP::Harness.
     --formatter       Result formatter to use. See FORMATTERS.
     --source          Load and/or configure a SourceHandler. See
                       SOURCE HANDLERS.
-a,  --archive out.tgz Store the resulting TAP in an archive file.
-j,  --jobs N          Run N test jobs in parallel (try 9.)
     --state=opts      Control prove's persistent state.
     --statefile=file  Use `file` instead of `.prove` for state
     --rc=rcfile       Process options from rcfile
     --rules           Rules for parallel vs sequential processing.

備註

.proverc

如果存在 ~/.proverc./.proverc,它們將會被讀取,並且在命令列選項之前處理它們所包含的任何選項。.proverc 中的選項指定方式與命令列選項相同

# .proverc
--state=hot,fast,save
-j9

可以使用 --rc 選項指定其他選項檔案。--norc 選項會停用預設的選項檔案處理。

在 Windows 和 VMS 中,選項檔案的名稱為 _proverc,而不是 .proverc,並且只會在目前目錄中尋找它。

STDIN 讀取

如果檔案中有測試清單(或 URL,或任何您想要測試的內容),您可以使用 '-' 將它們新增到您的測試中

prove - < my_list_of_things_to_test.txt

請參閱此發行版的 examples 目錄中的 README

預設測試目錄

如果沒有提供任何檔案或目錄,prove 會尋找符合 t/*.t 樣式的檔案。

彩色測試輸出

使用 TAP::Formatter::Color 的彩色測試輸出為預設值,但如果輸出不是到終端機,則會停用色彩。您可以透過新增 --color 參數來覆寫此設定。

色彩支援需要 Term::ANSIColor,而且在 Windows 平台上,也需要 Win32::Console::ANSI。如果沒有安裝必要的模組,將不會有彩色輸出。

結束代碼

如果測試失敗,prove 將會以非零狀態結束。

給測試的參數

可以提供參數給測試。為此,請使用冒號 '::' 將它們與 prove 自身的參數分開。例如

prove -v t/mytest.t :: --url http://example.com

會以「--url http://example.com」選項執行 t/mytest.t。執行多個測試時,它們會收到相同的引數。

--exec

通常,您只要傳遞 Perl 測試清單,測試套件就會知道如何執行它們。但是,如果您的測試不是用 Perl 編寫,或者您希望所有測試都以完全相同的方式呼叫,請使用 -e--exec 開關

prove --exec '/usr/bin/ruby -w' t/
prove --exec '/usr/bin/perl -Tw -mstrict -Ilib' t/
prove --exec '/path/to/my/customer/exec'

--merge

如果您需要確保診斷資訊相對於測試結果以正確順序顯示,可以使用 --merge 選項將測試指令碼的 STDERR 合併到其 STDOUT 中。

這可保證 STDOUT(測試結果顯示的位置)和 STDERR(診斷資訊顯示的位置)保持同步。測試套件將顯示您的測試在 STDERR 上發出的任何診斷資訊。

注意事項:這有點笨拙。特別注意,如果 STDERR 上出現的任何內容看起來像測試結果,測試套件會混淆。只有在您了解後果且可以承擔風險時,才使用此選項。

--trap

--trap 選項將嘗試在測試執行期間攔截 SIGINT (Ctrl-C) 並顯示測試摘要,即使執行中斷也是如此

--state

您可以要求 prove 記住先前測試執行的狀態,並根據儲存的狀態選擇和/或排序要執行的測試。

--state 開關需要一個引數,該引數必須是以下一個或多個選項的逗號分隔清單。

last

執行與上次儲存狀態時相同的測試。例如,這使得重新建立已洗牌測試的順序成為可能。

# Run all tests in random order
$ prove -b --state=save --shuffle

# Run them again in the same order
$ prove -b --state=last
failed

僅執行上次執行時失敗的測試。

# Run all tests
$ prove -b --state=save

# Run failures
$ prove -b --state=failed

如果您還指定 save 選項,新通過的測試將從後續執行中排除。

# Repeat until no more failures
$ prove -b --state=failed,save
passed

僅執行上次通過的測試。這有助於確保沒有引入任何新問題。

all

按正常順序執行所有測試。可以指定多個選項,因此要先執行上次失敗的所有測試

$ prove -b --state=failed,all,save
hot

先執行最近失敗的測試。會儲存每個測試的最後失敗時間。hot 選項會讓測試按最近失敗順序執行。

$ prove -b --state=hot,save

從未失敗的測試不會被選取。若要先執行最近失敗的所有測試,請使用

$ prove -b --state=hot,all,save

也可以這樣指定選項組合

$ prove -b --state=adrian
todo

執行任何有待辦事項的測試。

slow

按從最慢到最快的順序執行測試。這與 -j 並行測試開關結合使用時很有用,可確保最慢的測試先開始執行。

$ prove -b --state=slow -j9
fast

按從最快到最慢的順序執行測試。

new

根據測試腳本的修改時間,按從最新到最舊的順序執行測試。

old

按從最舊到最新的順序執行測試。

fresh

執行自上次測試執行以來已修改的那些測試腳本。

save

在結束時儲存狀態。狀態儲存在當前目錄中名為 .prove (Windows 和 VMS 上為 _prove) 的檔案中。

--state 開關可以使用多次。

$ prove -b --state=hot --state=all,save

--rules

--rules 選項用於控制哪些測試將按順序執行,哪些將並行執行(如果指定了 --jobs 選項)。可以多次指定選項,順序很重要。

最實用的用法可能是指定某些測試「不適合平行執行」。由於提及包含 --rules 的檔案並不會讓它被選取為測試執行,因此你可以「設定並忘記」.proverc 檔案中的一些規則偏好設定。然後你就能充分利用平行測試的效能優勢,同時某些例外仍然會平行執行。

--rules 範例

# All tests are allowed to run in parallel, except those starting with "p"
--rules='seq=t/p*.t' --rules='par=**'

# All tests must run in sequence except those starting with "p", which should be run parallel
--rules='par=t/p*.t'

--rules 解析

--rules Glob 式樣式樣對應

我們實作了自己的 Glob 式樣樣式對應,適用於 --rules。以下是支援的樣式

** is any number of characters, including /, within a pathname
* is zero or more characters within a filename/directory name
? is exactly one character within a filename/directory name
{foo,bar,baz} is any of foo, bar or baz.
\ is an escape character

平行與順序執行規則的更進階規格

如果你需要更進階的管理,以決定哪些內容平行執行,哪些順序執行,請參閱 TAP::HarnessTAP::Parser::Scheduler 中相關的「規則」文件。如果無法直接透過 prove 執行,你可以撰寫自己的 harness,以直接存取這些功能。

@INC

prove 引入了「傳遞給執行 prove 的 perl 的選項」和「傳遞給執行測試的 perl 的選項」之間的區別;這種區別是經過設計的。因此,執行測試的 perl 會從預設的 @INC 開始。額外的函式庫目錄可以透過 PERL5LIB 環境變數、PERL5OPT 中的 -Ifoo 或 prove-Ilib 選項新增。

Taint 模式

通常,當 Perl 程式以 taint 模式執行時,PERL5LIB 環境變數的內容不會出現在 @INC 中。

由於在測試期間經常使用 PERL5LIB 將建置目錄新增至 @INC,因此 prove 會將在 PERL5LIB 中找到的任何目錄名稱傳遞為 -I 切換。這樣做的實際效果是,即使在污點模式下執行 prove,也會遵循 PERL5LIB

格式化程式

您可以載入自訂 TAP::Parser::Formatter

prove --formatter MyFormatter

來源處理程式

您可以載入自訂 TAP::Parser::SourceHandler,以變更剖析器詮釋特定 TAP 來源 的方式。

prove --source MyHandler --source YetAnother t

如果您想要提供設定檔給來源,可以使用

prove --source MyCustom \
      --source Perl --perl-option 'foo=bar baz' --perl-option avg=0.278 \
      --source File --file-option extensions=.txt --file-option extensions=.tmp t
      --source pgTAP --pgtap-option pset=format=html --pgtap-option pset=border=2

每個 --$source-option 選項都必須指定一個以 = 分隔的鍵值對。如果選項可以採用多個值,只要像上述 extensions= 範例一樣指定多次即可。如果選項應該是雜湊參照,請指定值為第二個以 = 分隔的對,如上述 pset= 範例(使用反斜線跳脫 =)。

所有 --sources 都會合併成一個雜湊,並傳遞給 「TAP::Harness 中的 new」sources 參數。

請參閱 TAP::Parser::IteratorFactory,以取得有關如何將設定檔傳遞給 SourceHandlers 的更多詳細資訊。

外掛程式

可以使用 -Pplugin 語法載入外掛程式,例如

prove -PMyPlugin

這將搜尋名為 App::Prove::Plugin::MyPlugin 的模組,或如果找不到,則搜尋 MyPlugin。如果找不到外掛程式,prove 會抱怨並結束。

您可以透過附加 =arg1,arg2,etc 至外掛程式名稱,將引數傳遞給外掛程式

prove -PMyPlugin=fou,du,fafa

請查看個別外掛程式文件,以取得更多詳細資訊。

可用的外掛程式

如需取得可用的外掛程式清單,請查看 CPAN

http://search.cpan.org/search?query=App%3A%3AProve+Plugin

撰寫外掛程式

請參閱 App::Prove 中的「PLUGINS」