Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExUnit.run/1 to rerun test modules #11788

Merged
merged 4 commits into from May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/ex_unit/lib/ex_unit.ex
Expand Up @@ -350,11 +350,25 @@ defmodule ExUnit do
Runs the tests. It is invoked automatically
if ExUnit is started via `start/1`.

From Elixir v1.14, it accepts an optional list of modules to run
as part of the suite. This is often used to rerun modules already
loaded in memory.

Returns a map containing the total number of tests, the number
of failures, the number of excluded tests and the number of skipped tests.
"""
@spec run() :: suite_result()
def run do
@spec run(list(atom)) :: suite_result()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the spec change, do we need to add the since attribute?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question but I don't think so! We can mention this difference in the docs. :)

def run(additional_modules \\ []) do
for module <- additional_modules do
module_attributes = module.__info__(:attributes)

if Keyword.get(module_attributes, :ex_unit_async) do
ExUnit.Server.add_async_module(module)
else
ExUnit.Server.add_sync_module(module)
end
end

_ = ExUnit.Server.modules_loaded()
options = persist_defaults(configuration())
ExUnit.Runner.run(options, nil)
Expand Down
3 changes: 3 additions & 0 deletions lib/ex_unit/lib/ex_unit/case.ex
Expand Up @@ -294,6 +294,9 @@ defmodule ExUnit.Case do

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

persisted_attributes = [:ex_unit_async]
Enum.each(persisted_attributes, &Module.register_attribute(module, &1, persist: true))

attributes = [
before_compile: ExUnit.Case,
after_compile: ExUnit.Case,
Expand Down
46 changes: 46 additions & 0 deletions lib/ex_unit/test/ex_unit_test.exs
Expand Up @@ -32,6 +32,52 @@ defmodule ExUnitTest do
end) =~ "\n0 failures\n"
end

test "supports rerunning given modules" do
defmodule SampleAsyncTest do
use ExUnit.Case, async: true

test "true" do
assert false
end
end

defmodule SampleSyncTest do
use ExUnit.Case

test "true" do
assert false
end
end

defmodule IgnoreTest do
use ExUnit.Case

test "true" do
assert false
end
end

configure_and_reload_on_exit([])

assert capture_io(fn ->
assert ExUnit.run() == %{
failures: 3,
skipped: 0,
total: 3,
excluded: 0
}
end) =~ "\n3 tests, 3 failures\n"

assert capture_io(fn ->
assert ExUnit.run([SampleSyncTest, SampleAsyncTest]) == %{
failures: 2,
skipped: 0,
total: 2,
excluded: 0
}
end) =~ "\n2 tests, 2 failures\n"
kw7oe marked this conversation as resolved.
Show resolved Hide resolved
end

test "prints aborted runs on sigquit", config do
Process.register(self(), :aborted_on_sigquit)
line = __ENV__.line + 5
Expand Down