gpt4 book ai didi

unit-testing - Erlang - Eunit 设置函数不运行

转载 作者:行者123 更新时间:2023-11-28 20:24:18 25 4
gpt4 key购买 nike

我想编写测试,其中设置和清理功能不会针对每个测试执行,而是在测试开始和测试结束时执行一次。

所以我尝试了这种方式:

generator_test_() ->
{setup, fun setup/0, fun cleanup/1,
{inorder,
[
some_test1()
%...
]
}
}.

some_test1/0 在哪里

some_test1() ->
begin
Value1 = something1(),
Value2 = something2(),
[?_assertEqual(Value1, Value2)]
end.

安装程序不会以这种方式执行。

但是这样:

some_test1() ->
?_test(begin
Value1 = something1(),
Value2 = something2(),
[?_assertEqual(Value1, Value2)]
end).

some_test1() ->
[?_assertEqual(something1(), something2())].

一切都很好。

我不明白为什么它会起作用,我的问题是我不能在 ?_test(....) 中编写多个断言。

some_test1() ->
?_test(begin
Value1 = something1(),
Value2 = something2(),
[?_assertEqual(Value1, Value2),
?_assertEqual(Value1, Value2)]
end).

通过运行 eunit:test(my_module, [verbose]). 我看到只有 1 个测试通过了。

更新

示例 1

一个小例子:

-module(example).

-include_lib("eunit/include/eunit.hrl").

generator_test_() ->
{setup, fun setup/0, fun cleanup/1,
{inorder,
fun() -> some_test1() end
}
}.

some_test1() ->
begin
X = 3,
Y = 4,
%%% This is a test set/deep list as it
%%% contains two tests.
[?_assertEqual(X, Y),
?_assertEqual(ok, ok)]
end.

setup() ->
erlang:display("---SETUP CALLED---"),
"setup".

cleanup(_) ->
ok.

运行单元:

eunit:test(example, [verbose]).
======================== EUnit ========================
"---SETUP CALLED---"
module 'example'
example: generator_test_...ok
[done in 0.004 s]
=======================================================
Test passed.
ok

一个小修改:

some_test1() ->
begin
X = 3,
Y = 4,
%%% This is a test set/deep list as it
%%% containst two tests.
[?_assertEqual(X, Y),
?_assertEqual(it_doesnt_work, ok)]
end.

测试仍然通过。

例子2

不过

some_test1() ->
begin
X = 3,
Y = 4,
%%% These are just two simple tests
?assertEqual(X, Y),
?assertEqual(it_doesnt_work, ok)
end.

失败

eunit:test(example, [verbose]).
======================== EUnit ========================
"---SETUP CALLED---"
module 'example'
example: generator_test_...*failed*
in function example:'-some_test1/0-fun-0-'/1 (example.erl, line 17)
in call from example:some_test1/0 (example.erl, line 17)
**error:{assertEqual_failed,[{module,example},
{line,17},
{expression,"Y"},
{expected,3},
{value,4}]}


[done in 0.003 s]
=======================================================
Failed: 1. Skipped: 0. Passed: 0.
error

但是

示例 3

generator_test_() ->
{setup, fun setup/0, fun cleanup/1,
{inorder,
some_test1()
}
}.

some_test1() ->
[?_assertEqual(3, 3),
?_assertEqual(ok, ok)].

eunit:test(example, [verbose]).
======================== EUnit ========================
"---SETUP CALLED---"
module 'example'
example:13: some_test1...ok
example:14: some_test1...ok
[done in 0.005 s]
=======================================================
All 2 tests passed.
ok

现在我看到有 2(!) 个测试。

我可以忘记 block 语句并编写没有变量的测试,但我认为这可能读起来很糟糕,它应该与 block 一起工作。

最佳答案

documentation说:

A simple test object is one of the following:

A nullary functional value (i.e., a fun that takes zero arguments). Examples:

   fun () -> ... end
fun some_function/0
fun some_module:some_function/0

A tuple {test, ModuleName, FunctionName}, where ModuleName and FunctionName are atoms, referring to the function

ModuleName:FunctionName/0

A pair {LineNumber, SimpleTest}, where LineNumber is a nonnegative integer and SimpleTest is another simple test object. LineNumber

should indicate the source line of the test. Pairs like this are usually only created via ?_test(...) macros; see Basic macros.

因此您可以指定您的测试,例如:

generator_test_() ->
{setup, fun setup/0, fun cleanup/1,
{inorder,
[
fun() -> some_test1() end
%% OR
fun some_test1/0
]
}
}.

此外:

(Note that these macros also have corresponding forms which start with an "_" (underscore) character, as in ?_assert(BoolExpr), that create a "test object" instead of performing the test immediately. This is equivalent to writing ?_test(assert(BoolExpr)), etc.)

这就是您的 ?_* 测试生成器起作用的原因。

编辑

示例 2 和 3 是做事的好方法。示例 2 将一个简单的测试 fun 放入测试列表中。示例 3 生成 2 个测试的列表,因为

[?_assertEqual(a, b), ?_assertEqual(a == a)]

实际上扩展为

[fun() -> ?assertEqual(a, b) end, fun() -> ?assertEqual(a, a) end]

如果需要变量,请使用示例 2,否则使用示例 3。示例 1 没有给出预期的结果,因为“fun() -> some_test1() end”本身就是一个测试,它应该执行真正的断言 (?assert*) 而不是产生另一个测试 (?_assert*)。我希望现在一切都清楚了。

关于unit-testing - Erlang - Eunit 设置函数不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com