簡體   English   中英

使用通用測試的模塊的Erlang測試(非導出/私有)功能

[英]Erlang Testing (Non Exported / Private) function of module using common test

我在Erlang中有一個模塊,其中的函數不是由Erlang導出的。 如何使用通用測試框架測試/調用這些函數?

是不可能的。 你可以使用-ifdef(TEST). 預處理器條件,僅在編譯測試時導出這些函數。

根據您的工具,您可能需要在編譯模塊時顯式提供該TEST宏。 您可以使用{d,'TEST'}編譯器選項或-DTEST編譯標志來實現。

使用Common Test很棘手,但可以使用嵌入式EUnit測試用例來測試模塊中的私有函數。 然后,您可以使用Common Test測試公共接口。 當您運行rebar test時, Rebar將自動發現嵌入式rebar test

這是一個例子:

-module(example).

-export([public/1]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

%% This function will be tested externally using Common Test
public(Foo) ->
    private(Foo + 42).

%% This function is not reachable to CT, so it will be tested using EUnit.
private(Bar) ->
    Bar * 2.

%%% Tests
-ifdef(TEST).

private_test() ->
    ?assertEqual(10, private(5)),
    ?assertEqual(0, private(0)).

-endif.

另外,如果您在使用EUnit進行測試時需要模擬一個模塊(或其中的一部分),您可能會發現Meck符合您的喜好。

有關EUnit的溫和介紹,請參閱Learn You Some Erlang 章節

您可以將私有函數放在它們自己的模塊中,該模塊將導出所有這些函數。 原始模塊可以導入它們並且它們將保持私有,並且您的測試框架可以直接調用導入私有模塊。

以防有人遇到這個問題。 包括eunit.hrl文件定義TEST,除非在include之前定義了NOTEST。 -include_lib( “eunit.hrl”)。 參考: http//www.erlang.org/download/eunit.hrl

暫無
暫無

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

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