erlang 如何动态地生成具有公共测试的测试用例?

vh0rcniy  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(136)

对于Common Test测试套件,看起来测试用例必须是1:1的,并且原子对应于套件中的顶级函数。这是真的吗?
在这种情况下,我如何动态地生成测试用例?
具体来说,我希望读取一个目录,然后(并行地)对目录中的每个文件进行填充,然后与快照进行比较。
我用rpc:pmap得到了我想要的并行化,但是我不喜欢的是整个测试用例在第一个坏的Assert上失败。我想看看每次所有的文件都发生了什么。有办法做到这一点吗?

lqfhib0f

lqfhib0f1#

Short answer: No.
Long answer: No. I even tried using Ghost Functions

-module(my_test_SUITE).

-export [all/0].
-export [has_files/1].
-export ['$handle_undefined_function'/2].

all() -> [has_files | files() ].

has_files(_) ->
    case files() of
        [] -> ct:fail("No files in ~s", [element(2, file:get_cwd())]);
        _ -> ok
    end.

files() ->
    [to_atom(AsString) || AsString <- filelib:wildcard("../../lib/exercism/test/*.test")].

to_atom(AsString) ->
    list_to_atom(filename:basename(filename:rootname(AsString))).

'$handle_undefined_function'(Func, [_]) ->
    Func = file:consult(Func).

And… as soon as I add the undefined function handler, rebar3 ct start reporting…

All 0 tests passed.

Clearly common test is also using the fact that some functions are undefined to work. 🤷‍♂️

dgenwo3n

dgenwo3n2#

Data Directory

Each common test suite can have a "data" directory. This directory can contain anything you want. For example, a test suite mytest_SUITE , can have mytest_SUITE_data/ "data" directory. The path to data directory can be obtained from the Config parameter in test cases.

someTest(Config) ->
  DataDir = ?config(data_dir, Config),
  %% TODO: do something with DataDir
  ?assert(false). %% include eunit header file for this to work

Running tests in parallel

To run tests in parallel you need to use groups. Add a group/0 function to the test suite

groups() -> ListOfGroups.

Each member in ListOfGroups is a tuple, {Name, Props, Members} . Name is an atom, Props is list of properties for the groups, and Members is a list of test cases in the group. Setting Props to [parallel|OtherProps] will enable the test cases in the group to be executed in parallel.

Dynamic Test Cases

Checkout cucumberl project.

相关问题