From 03da5e0be061dfed832ca7eaaf5998fa7cf27640 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Wed, 5 Feb 2014 14:41:25 -0500 Subject: Add random_suite_order option to eunit command Option takes either 'true' or a numeric seed value. If true is passed, a random seed is generated and used. The numeric seed value is for repeatability. The idea here is to root out test suites that are order dependant, or that fail in the presence of certain orderings. --- src/rebar_eunit.erl | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index e04b28e..323e199 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -150,7 +150,7 @@ run_eunit(Config, CodePath, SrcErls) -> AllBeamFiles), OtherBeamFiles = TestBeamFiles -- [filename:rootname(N) ++ "_tests.beam" || N <- AllBeamFiles], - ModuleBeamFiles = BeamFiles ++ OtherBeamFiles, + ModuleBeamFiles = randomize_suites(Config, BeamFiles ++ OtherBeamFiles), %% Get modules to be run in eunit AllModules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- AllBeamFiles], @@ -234,6 +234,33 @@ get_suites(Config) -> end. %% +%% == randomize suites == +%% + +randomize_suites(Config, Modules) -> + case rebar_config:get_global(Config, random_suite_order, undefined) of + undefined -> + Modules; + "true" -> + Seed = crypto:rand_uniform(1, 65535), + randomize_suites1(Modules, Seed); + String -> + try list_to_integer(String) of + Seed -> + randomize_suites1(Modules, Seed) + catch + error:badarg -> + ?ERROR("Bad random seed provided: ~p~n", [String]), + ?FAIL + end + end. + +randomize_suites1(Modules, Seed) -> + random:seed(35, Seed, 1337), + ?CONSOLE("Randomizing suite order with seed ~b~n", [Seed]), + [X||{_,X} <- lists:sort([{random:uniform(), M} || M <- Modules])]. + +%% %% == get matching tests == %% get_tests(Config, SuitesProvided, ModuleBeamFiles, FilteredModules) -> -- cgit v1.1 From 2f6991cbd7ae249e0d24f991c37b2746fd78b1d6 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Thu, 6 Feb 2014 14:58:38 -0500 Subject: Add documentation --- src/rebar.erl | 2 ++ src/rebar_eunit.erl | 3 +++ 2 files changed, 5 insertions(+) (limited to 'src') diff --git a/src/rebar.erl b/src/rebar.erl index b213e6c..833e397 100644 --- a/src/rebar.erl +++ b/src/rebar.erl @@ -369,6 +369,8 @@ eunit [suite[s]=foo] Run eunit tests in foo.erl and no such test exists, run the test whose name starts with bar in the suite's _tests module + [random_suite_order=true] Run tests in a random order, either with + [random_suite_order=Seed] a random seed for the PRNG, or a specific one. ct [suite[s]=] [case=] Run common_test suites diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index 323e199..3fe1280 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -126,6 +126,9 @@ info_help(Description) -> " name starts with bar and, if no such test exists,~n" " run the test whose name starts with bar in the~n" " suite's _tests module)~n" + " random_suite_order=true (Run tests in random order)~n" + " random_suite_order=Seed (Run tests in random order,~n" + " with the PRNG seeded with Seed)~n" " compile_only=true (Compile but do not run tests)", [ Description, -- cgit v1.1