Skip to content

Commit 5e07218

Browse files
authored
Add ExUnit.run/1 to rerun test modules (#11788)
1 parent 3ba5381 commit 5e07218

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

lib/ex_unit/lib/ex_unit.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,25 @@ defmodule ExUnit do
350350
Runs the tests. It is invoked automatically
351351
if ExUnit is started via `start/1`.
352352
353+
From Elixir v1.14, it accepts an optional list of modules to run
354+
as part of the suite. This is often used to rerun modules already
355+
loaded in memory.
356+
353357
Returns a map containing the total number of tests, the number
354358
of failures, the number of excluded tests and the number of skipped tests.
355359
"""
356-
@spec run() :: suite_result()
357-
def run do
360+
@spec run(list(atom)) :: suite_result()
361+
def run(additional_modules \\ []) do
362+
for module <- additional_modules do
363+
module_attributes = module.__info__(:attributes)
364+
365+
if Keyword.get(module_attributes, :ex_unit_async) do
366+
ExUnit.Server.add_async_module(module)
367+
else
368+
ExUnit.Server.add_sync_module(module)
369+
end
370+
end
371+
358372
_ = ExUnit.Server.modules_loaded()
359373
options = persist_defaults(configuration())
360374
ExUnit.Runner.run(options, nil)

lib/ex_unit/lib/ex_unit/case.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ defmodule ExUnit.Case do
295295

296296
Enum.each(attributes, &Module.register_attribute(module, &1, accumulate: true))
297297

298+
persisted_attributes = [:ex_unit_async]
299+
Enum.each(persisted_attributes, &Module.register_attribute(module, &1, persist: true))
300+
298301
attributes = [
299302
before_compile: ExUnit.Case,
300303
after_compile: ExUnit.Case,

lib/ex_unit/test/ex_unit_test.exs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,52 @@ defmodule ExUnitTest do
3232
end) =~ "\n0 failures\n"
3333
end
3434

35+
test "supports rerunning given modules" do
36+
defmodule SampleAsyncTest do
37+
use ExUnit.Case, async: true
38+
39+
test "true" do
40+
assert false
41+
end
42+
end
43+
44+
defmodule SampleSyncTest do
45+
use ExUnit.Case
46+
47+
test "true" do
48+
assert false
49+
end
50+
end
51+
52+
defmodule IgnoreTest do
53+
use ExUnit.Case
54+
55+
test "true" do
56+
assert false
57+
end
58+
end
59+
60+
configure_and_reload_on_exit([])
61+
62+
assert capture_io(fn ->
63+
assert ExUnit.run() == %{
64+
failures: 3,
65+
skipped: 0,
66+
total: 3,
67+
excluded: 0
68+
}
69+
end) =~ "\n3 tests, 3 failures\n"
70+
71+
assert capture_io(fn ->
72+
assert ExUnit.run([SampleSyncTest, SampleAsyncTest]) == %{
73+
failures: 2,
74+
skipped: 0,
75+
total: 2,
76+
excluded: 0
77+
}
78+
end) =~ "\n2 tests, 2 failures\n"
79+
end
80+
3581
test "prints aborted runs on sigquit", config do
3682
Process.register(self(), :aborted_on_sigquit)
3783
line = __ENV__.line + 5

0 commit comments

Comments
 (0)