Max Heiber
Max Heiber

Reputation: 15502

How can I dynamically generate test cases with common test?

With Common Test test suites, it looks like test cases must be 1:1 with atoms that correspond to top-level functions in the suite. Is this true?

In that case, how can I dynamically generate test cases?

In particular, I want to read a directory, and then, (in parallel) for each file in the directory, do stuff with the file and then compare against a snapshot.

I got the parallelization I wanted with rpc:pmap, but what I don't like is that the entire test case fails on the first bad assert. I want to see what happens with all the files, every time. Is there a way to do this?

Upvotes: 2

Views: 272

Answers (2)

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.

Upvotes: -1

Brujo Benavides
Brujo Benavides

Reputation: 1958

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. 🤷‍♂️

Upvotes: 3

Related Questions