From 7037b6524a3221a02bc8f28c71151c4c5cb335a2 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Tue, 8 May 2012 14:56:27 +0200 Subject: Fix close of suite when end_per_suite is skipped --- lib/common_test/doc/src/ct_hooks_chapter.xml | 2 +- lib/common_test/src/cth_surefire.erl | 42 +++++++++++++++++----------- 2 files changed, 26 insertions(+), 18 deletions(-) (limited to 'lib/common_test') diff --git a/lib/common_test/doc/src/ct_hooks_chapter.xml b/lib/common_test/doc/src/ct_hooks_chapter.xml index 1dbb841fb0..014507c886 100644 --- a/lib/common_test/doc/src/ct_hooks_chapter.xml +++ b/lib/common_test/doc/src/ct_hooks_chapter.xml @@ -453,7 +453,7 @@ terminate(State) -> Captures all test results and outputs them as surefire XML into a file. The file which is created is by default called junit_report.xml. The name can be by setting the path option for this hook. e.g. - -ct_hooks cth_surefix [{path,"/tmp/report.xml"}] + -ct_hooks cth_surefire [{path,"/tmp/report.xml"}] Surefire XML can forinstance be used by Jenkins to display test results. diff --git a/lib/common_test/src/cth_surefire.erl b/lib/common_test/src/cth_surefire.erl index c42f956b3a..cb74bf0d81 100644 --- a/lib/common_test/src/cth_surefire.erl +++ b/lib/common_test/src/cth_surefire.erl @@ -49,9 +49,12 @@ init(Path, Opts) -> properties = proplists:get_value(properties,Opts,[]), timer = now() }. -pre_init_per_suite(Suite,Config,State) -> +pre_init_per_suite(Suite,Config,#state{ test_cases = [] } = State) -> {Config, init_tc(State#state{ curr_suite = Suite, curr_suite_ts = now() }, - Config) }. + Config) }; +pre_init_per_suite(Suite,Config,State) -> + %% Have to close the previous suite + pre_init_per_suite(Suite,Config,close_suite(State)). post_init_per_suite(_Suite,Config, Result, State) -> {Result, end_tc(init_per_suite,Config,Result,State)}. @@ -59,11 +62,7 @@ post_init_per_suite(_Suite,Config, Result, State) -> pre_end_per_suite(_Suite,Config,State) -> {Config, init_tc(State, Config)}. post_end_per_suite(_Suite,Config,Result,State) -> - NewState = end_tc(end_per_suite,Config,Result,State), - TCs = NewState#state.test_cases, - Suite = get_suite(NewState, TCs), - {Result, State#state{ test_cases = [], - test_suites = [Suite | State#state.test_suites]}}. + {Result, end_tc(end_per_suite,Config,Result,State)}. pre_init_per_group(Group,Config,State) -> {Config, init_tc(State#state{ curr_group = [Group|State#state.curr_group]}, @@ -118,26 +117,35 @@ end_tc(Name, _Config, _Res, State = #state{ curr_suite = Suite, name = Name, time = TimeTakes, failure = passed }| State#state.test_cases]}. - -get_suite(State, TCs) -> +close_suite(#state{ test_cases = [] } = State) -> + State; +close_suite(#state{ test_cases = TCs } = State) -> Total = length(TCs), Succ = length(lists:filter(fun(#testcase{ failure = F }) -> F == passed end,TCs)), Fail = Total - Succ, TimeTaken = timer:now_diff(now(),State#state.curr_suite_ts) / 1000000, - #testsuite{ name = atom_to_list(State#state.curr_suite), - package = State#state.package, - time = io_lib:format("~f",[TimeTaken]), - timestamp = now_to_string(State#state.curr_suite_ts), - errors = Fail, tests = Total, testcases = lists:reverse(TCs) }. - -terminate(State) -> + Suite = #testsuite{ name = atom_to_list(State#state.curr_suite), + package = State#state.package, + time = io_lib:format("~f",[TimeTaken]), + timestamp = now_to_string(State#state.curr_suite_ts), + errors = Fail, tests = Total, + testcases = lists:reverse(TCs) }, + State#state{ test_cases = [], + test_suites = [Suite | State#state.test_suites]}. + +terminate(State = #state{ test_cases = [] }) -> {ok,D} = file:open(State#state.filepath,[write]), io:format(D, "", []), io:format(D, to_xml(State), []), catch file:sync(D), - catch file:close(D). + catch file:close(D); +terminate(State) -> + %% Have to close the last suite + terminate(close_suite(State)). + + to_xml(#testcase{ group = Group, classname = CL, log = L, name = N, time = T, timestamp = TS, failure = F}) -> ["