檢視原始碼 ct_property_test (common_test v1.27.5)
在 Common Test 中支援執行基於屬性的測試。
這個模組有助於在 Common Test
框架中執行基於屬性的測試。假設已安裝一個(或多個)屬性測試工具
。
此模組的想法是讓 Common Test
測試套件使用該工具定義的特殊屬性測試套件來呼叫屬性測試工具。測試會收集在應用程式的 test
目錄中。test
目錄有一個子目錄 property_test
,其中收集了屬性測試所需的所有內容。假設使用常見的 Erlang 應用程式目錄結構。
一個典型的使用 ct_property_test
的 Common Test
測試套件組織如下
-module(my_prop_test_SUITE).
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
all() -> [prop_ftp_case].
init_per_suite(Config) ->
ct_property_test:init_per_suite(Config).
%%%---- test case
prop_ftp_case(Config) ->
ct_property_test:quickcheck(
ftp_simple_client_server:prop_ftp(),
Config
).
並且屬性測試模組(在此範例中為 ftp_simple_client_server.erl
)幾乎是一個常見的屬性測試模組(更多範例請參閱使用者指南)
-module(ftp_simple_client_server).
-export([prop_ftp/0...]).
-include_lib("common_test/include/ct_property_test.hrl").
prop_ftp() ->
?FORALL( ....
摘要
函式
傳回在 Cmnd
序列中產生的指令(函式呼叫)清單,不包含模組、引數和其他詳細資訊。
初始化並擴展 Config
以進行基於屬性的測試。
傳回測試案例中指令呼叫的數量。
使用 PropEr、QuickCheck 或其他類似屬性測試工具中的彙總函式,呈現有狀態 (statem) 屬性測試的結果。
呼叫所選工具的函式以執行 Property
。由於歷史原因,它通常被稱為 quickcheck,這也是為什麼這個模組 (ct_property_test
) 中使用這個名稱。
傳回包含循序和並行部分資訊的清單。
類型
-type arguments() :: [term()].
-type command() :: set_command() | init_command().
-type command_list() :: [command()].
-type dynamic_state() :: term().
-type function_name() :: atom().
-type history() :: [term()].
-type init_command() :: {init, symbolic_state()}.
-type parallel_testcase() :: {command_list(), [command_list()]}.
-type set_command() :: {set, symbolic_var(), symbolic_call()}.
-type statem_result() :: ok | term().
-type symbolic_call() :: {call, module(), function_name(), arguments()}.
-type symbolic_state() :: term().
-type symbolic_var() :: {var, pos_integer()}.
函式
-spec cmnd_names(Cs) -> Result when Cs :: command_list() | parallel_testcase(), Result :: [function_name()].
傳回在 Cmnd
序列中產生的指令(函式呼叫)清單,不包含模組、引數和其他詳細資訊。
如需更多資訊,請參閱:present_result/5
。
-spec init_per_suite(Config) -> Config | {skip, Reason} | {fail, Reason} when Config :: proplists:proplist(), Reason :: string().
初始化並擴展 Config
以進行基於屬性的測試。
此函式會調查是否支援 QuickCheck、PropEr 或 Triq,並使用找到的第一個工具來編譯屬性。它應該在 CommonTest 測試套件的 init_per_suite/1
函式中呼叫。
要檢查的工具及其順序可以使用 CommonTest 設定 Config
中的選項 {prop_tools, list(eqc|proper|triq)}
來設定。預設值為 [eqc, proper, triq]
,其中 eqc
是第一個搜尋到的工具。
如果找不到任何工具的支援,此函式會傳回 {skip, Explanation}
。
如果發生其他錯誤,此函式會傳回 {fail, Explanation}
。
如果找到支援,則會將包含所選工具主模組名稱(eqc
、proper
或 triq
)的選項 {property_test_tool,ToolModule}
新增到清單 Config
,然後傳回該清單。
假設屬性測試位於名為 property_test
的子目錄中。該目錄中找到的所有 Erlang 檔案都會使用其中一個巨集 'EQC'
、'PROPER'
或 'TRIQ'
來編譯,具體取決於首先找到的工具。這可以使部分 Erlang 屬性測試程式碼使用巨集指令 -ifdef(Macro).
或 -ifndef(Macro).
來包含或排除。
property_test
子目錄中的檔案可以(或應該)包含 ct_property_test 包含檔案
-include_lib("common_test/include/ct_property_test.hrl").
這個包含的檔案會
- 包含正確的工具包含檔案
- 將巨集
'MOD_eqc'
設定為所選工具的正確模組名稱。也就是說,巨集'MOD_eqc'
會設定為eqc
、proper
或triq
。
-spec num_calls(Cs) -> Result when Cs :: command_list() | parallel_testcase(), Result :: [non_neg_integer()].
傳回測試案例中指令呼叫的數量。
如需更多資訊,請參閱:present_result/5
。
-spec present_result(Module, Cmds, Triple, Config) -> boolean() when Module :: module(), Cmds :: command_list() | parallel_testcase(), Triple :: {H, Sf, Result}, H :: history(), Sf :: dynamic_state(), Result :: statem_result(), Config :: proplists:proplist().
-spec present_result(Module, Cmds, Triple, Config, Options0) -> boolean() when Module :: module(), Cmds :: command_list() | parallel_testcase(), Triple :: {H, Sf, Result}, H :: history(), Sf :: dynamic_state(), Result :: statem_result(), Config :: proplists:proplist(), Options0 :: proplists:proplist().
使用 PropEr、QuickCheck 或其他類似屬性測試工具中的彙總函式,呈現有狀態 (statem) 屬性測試的結果。
假設它在 quickcheck/2
呼叫的屬性內呼叫
...
RunResult = run_parallel_commands(?MODULE, Cmds),
ct_property_test:present_result(?MODULE, Cmds, RunResult, Config)
...
有關用法和預設輸出的範例,請參閱使用者指南。
StatisticsSpec
是元組的清單
{Title::string(), CollectFun::fun/1}
{Title::string(), FrequencyFun::/0, CollectFun::fun/1}
每個元組都會按照其在清單中的位置產生一個表格。
Title
將是一個結果表格的標題CollectFun
會以一個引數呼叫:Cmds
。它應該傳回要計數的值的清單。存在以下預先定義的函式ct_property_test:cmnd_names/1
傳回在Cmnd
序列中產生的指令(函式呼叫)清單,不包含模組、引數和其他詳細資訊。ct_property_test:num_calls/1
傳回指令清單長度的清單ct_property_test:sequential_parallel/1
傳回包含來自Tool:parallel_commands/1,2
的循序和並行部分資訊的清單
FrequencyFun/0
會傳回 fun/1,它應該以項目清單作為輸入,並傳回將作為表格列印的 iolist。預設情況下,會計算每個項目的數量,並列印每個項目的百分比。例如,清單 [a,b,a,a,c] 可以傳回["a 60%\n","b 20%\n","c 20%\n"]
將由
print_fun
列印。預設的print_fun
會將其列印為a 60% b 20% c 20%
預設的 StatisticsSpec
為
對於循序命令
[{"Function calls", fun cmnd_names/1}, {"Length of command sequences", fun print_frequency_ranges/0, fun num_calls/1}]
對於並行命令
[{"Distribution sequential/parallel", fun sequential_parallel/1}, {"Function calls", fun cmnd_names/1}, {"Length of command sequences", fun print_frequency_ranges/0, fun num_calls/1}]
-spec quickcheck(Property, Config) -> true | {fail, Reason} when Property :: term(), Config :: proplists:proplist(), Reason :: term().
呼叫所選工具的函式以執行 Property
。由於歷史原因,它通常被稱為 quickcheck,這也是為什麼這個模組 (ct_property_test
) 中使用這個名稱。
結果會以適合 Common Test
測試套件的形式回傳。
此函數旨在測試套件中的測試案例中呼叫。
-spec sequential_parallel(Cs) -> Result when Cs :: command_list() | parallel_testcase(), Result :: [atom()].
傳回包含循序和並行部分資訊的清單。
如需更多資訊,請參閱:present_result/5
。