Skip to content

Commit

Permalink
Fix cth_surefire
Browse files Browse the repository at this point in the history
- Support parallel test failures
- Close groups when end_per_suite is skipped or fails
- Include group name in error messages
  • Loading branch information
chrzaszcz committed Dec 21, 2022
1 parent 89bba0d commit 651bdd5
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions big_tests/src/cth_surefire.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,23 @@ post_end_per_testcase(_Suite,TC,Config,Result,State) ->
on_tc_fail(Suite,TC,Result,Proxy) when is_pid(Proxy) ->
_ = gen_server:call(Proxy,{?FUNCTION_NAME, [Suite, TC, Result]}),
Proxy;
on_tc_fail(_Suite,_TC, _Res, State = #state{test_cases = []}) ->
on_tc_fail(_Suite, _TestName, _Res, State = #state{test_cases = []}) ->
State;
on_tc_fail(Suite, _TC, Res, State) ->
TCs = State#state.test_cases,
TC = hd(TCs),
on_tc_fail(Suite, TestName, Res, State = #state{test_cases = TCs}) ->
TCName = tc_name_to_string(TestName),
{value, TC, RemainingTCs} = lists:keytake(TCName, #testcase.name, TCs),
State1 = do_tc_fail(Suite, TC, Res, State#state{test_cases = RemainingTCs}),
maybe_close_group(TestName, State1).

do_tc_fail(Suite, TC, Res, State = #state{test_cases = TCs}) ->
Line = case get_line_from_result(Suite, Res) of
undefined ->
TC#testcase.line;
L -> L
end,
NewTC = TC#testcase{
line = Line,
result =
{fail,lists:flatten(io_lib:format("~tp",[Res]))} },
State#state{ test_cases = [NewTC | tl(TCs)]}.
NewTC = TC#testcase{line = Line,
result = {fail,lists:flatten(io_lib:format("~tp",[Res]))}},
State#state{test_cases = [NewTC | TCs]}.

get_line_from_result(Suite, {_Error, [{__M,__F,__A,__I}|_] = StackTrace}) ->
case lists:filter(fun({Mod, _Func, _Arity, _Info}) ->
Expand All @@ -245,18 +247,11 @@ get_line_from_result(_, _) ->
on_tc_skip(Suite,TC,Result,Proxy) when is_pid(Proxy) ->
_ = gen_server:call(Proxy,{?FUNCTION_NAME, [Suite,TC,Result]}),
Proxy;
on_tc_skip(Suite,{ConfigFunc,_GrName}, Res, State) ->
on_tc_skip(Suite,ConfigFunc, Res, State);
on_tc_skip(Suite,Tc, Res, State0) ->
TcStr = atom_to_list(Tc),
State =
case State0#state.test_cases of
[#testcase{name=TcStr}|TCs] ->
State0#state{test_cases=TCs};
_ ->
State0
end,
do_tc_skip(Res, end_tc(Tc,[],Res,init_tc(set_suite(Suite,State),[]))).
on_tc_skip(Suite, TestName, Res, State = #state{test_cases = TCs}) ->
TCName = tc_name_to_string(TestName),
State1 = State#state{test_cases = lists:keydelete(TCName, #testcase.name, TCs)},
State2 = do_tc_skip(Res, end_tc(TCName, [], Res, init_tc(set_suite(Suite, State1), []))),
maybe_close_group(TestName, State2).

do_tc_skip(Res, State) ->
TCs = State#state.test_cases,
Expand All @@ -266,6 +261,16 @@ do_tc_skip(Res, State) ->
{skipped,lists:flatten(io_lib:format("~tp",[Res]))} },
State#state{ test_cases = [NewTC | tl(TCs)]}.

tc_name_to_string({FuncName, _GroupName}) ->
atom_to_list(FuncName);
tc_name_to_string(FuncName) ->
atom_to_list(FuncName).

maybe_close_group({end_per_group, Group}, State = #state{curr_group = [Group|Rest]}) ->
State#state{curr_group = Rest};
maybe_close_group(_, State) ->
State.

init_tc(State, Config) when is_list(Config) == false ->
State#state{ timer = ?now, tc_log = "" };
init_tc(State, Config) ->
Expand Down Expand Up @@ -385,10 +390,11 @@ to_xml(#testcase{ group = Group, classname = CL, log = L, url = U,
passed ->
[];
{skipped,Reason} ->
["<skipped type=\"skip\" message=\"Test ",N," in ",CL,
" skipped!\">", sanitize(Reason),"</skipped>"];
["<skipped type=\"skip\" message=\"Test ", N, " in ", CL, maybe_group(Group),
" skipped!\">", sanitize(Reason),"</skipped>"];
{fail,Reason} ->
["<failure message=\"Test ",N," in ",CL," failed!\" type=\"crash\">",
["<failure message=\"Test ", N, " in ", CL, maybe_group(Group),
" failed!\" type=\"crash\">",
sanitize(Reason),"</failure>"]
end,"</testcase>"];
to_xml(#testsuite{ package = P, hostname = H, errors = E, failures = F,
Expand All @@ -412,6 +418,11 @@ to_xml(#state{ test_suites = TestSuites, axis = Axis, properties = Props }) ->
["<testsuites>",properties_to_xml(Axis,Props),
[to_xml(TestSuite) || TestSuite <- TestSuites],"</testsuites>"].

maybe_group("") ->
[];
maybe_group(Group) ->
[" (group: ", Group, ")"].

properties_to_xml([],[]) ->
[];
properties_to_xml(Axis,Props) ->
Expand Down

0 comments on commit 651bdd5

Please sign in to comment.