Skip to content

Commit

Permalink
Omit excluded tests from test count in CLI summary (#14078)
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin authored Dec 23, 2024
1 parent 71ad8fa commit 92f1e37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
28 changes: 21 additions & 7 deletions lib/ex_unit/lib/ex_unit/cli_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule ExUnit.CLIFormatter do
def handle_cast({:suite_finished, times_us}, config) do
test_type_counts = collect_test_type_counts(config)

if test_type_counts > 0 && config.excluded_counter == test_type_counts do
if test_type_counts == 0 and config.excluded_counter > 0 do
IO.puts(invalid("All tests have been excluded.", config))
end

Expand Down Expand Up @@ -256,6 +256,10 @@ defmodule ExUnit.CLIFormatter do
end
end

defp update_test_counter(test_counter, %{state: {:excluded, _reason}}) do
test_counter
end

defp update_test_counter(test_counter, %{tags: %{test_type: test_type}}) do
Map.update(test_counter, test_type, 1, &(&1 + 1))
end
Expand Down Expand Up @@ -331,16 +335,13 @@ defmodule ExUnit.CLIFormatter do
## Printing

defp print_summary(config, force_failures?) do
formatted_test_type_counts = format_test_type_counts(config)
test_type_counts = collect_test_type_counts(config)
test_counter = test_counter_or_default(config, test_type_counts)
formatted_test_type_counts = format_test_type_counts(test_counter)
failure_pl = pluralize(config.failure_counter, "failure", "failures")

message =
"#{formatted_test_type_counts}#{config.failure_counter} #{failure_pl}"
|> if_true(
config.excluded_counter > 0,
&(&1 <> ", #{config.excluded_counter} excluded")
)
|> if_true(
config.invalid_counter > 0,
&(&1 <> ", #{config.invalid_counter} invalid")
Expand All @@ -349,6 +350,10 @@ defmodule ExUnit.CLIFormatter do
config.skipped_counter > 0,
&(&1 <> ", " <> skipped("#{config.skipped_counter} skipped", config))
)
|> if_true(
config.excluded_counter > 0,
&(&1 <> " (#{config.excluded_counter} excluded)")
)

cond do
config.failure_counter > 0 or force_failures? ->
Expand Down Expand Up @@ -384,15 +389,24 @@ defmodule ExUnit.CLIFormatter do
IO.puts(formatted)
end

defp format_test_type_counts(%{test_counter: test_counter} = _config) do
defp format_test_type_counts(test_counter) do
test_counter
|> Enum.sort()
|> Enum.map(fn {test_type, count} ->
type_pluralized = pluralize(count, test_type, ExUnit.plural_rule(test_type |> to_string()))

"#{count} #{type_pluralized}, "
end)
end

defp test_counter_or_default(_config, 0) do
%{test: 0}
end

defp test_counter_or_default(%{test_counter: test_counter} = _config, _test_type_counts) do
test_counter
end

defp collect_test_type_counts(%{test_counter: test_counter} = _config) do
Enum.reduce(test_counter, 0, fn {_, count}, acc ->
acc + count
Expand Down
29 changes: 15 additions & 14 deletions lib/ex_unit/test/ex_unit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule ExUnitTest do
assert capture_io(fn ->
assert ExUnit.async_run() |> ExUnit.await_run() ==
%{failures: 0, skipped: 0, total: 0, excluded: 0}
end) =~ "\n0 failures\n"
end) =~ "\n0 tests, 0 failures\n"
end

test "supports rerunning given modules" do
Expand Down Expand Up @@ -137,7 +137,7 @@ defmodule ExUnitTest do
assert result =~ """
Showing results so far...
0 failures
0 tests, 0 failures
"""
end

Expand Down Expand Up @@ -331,19 +331,19 @@ defmodule ExUnitTest do

{result, output} = run_with_filter([exclude: [even: true]], [ParityTest])
assert result == %{failures: 0, skipped: 0, excluded: 1, total: 4}
assert output =~ "\n4 tests, 0 failures, 1 excluded\n"
assert output =~ "\n3 tests, 0 failures (1 excluded)\n"

{result, output} = run_with_filter([exclude: :even], [ParityTest])
assert result == %{failures: 0, skipped: 0, excluded: 3, total: 4}
assert output =~ "\n4 tests, 0 failures, 3 excluded\n"
assert output =~ "\n1 test, 0 failures (3 excluded)\n"

{result, output} = run_with_filter([exclude: :even, include: [even: true]], [ParityTest])
assert result == %{failures: 1, skipped: 0, excluded: 2, total: 4}
assert output =~ "\n4 tests, 1 failure, 2 excluded\n"
assert output =~ "\n2 tests, 1 failure (2 excluded)\n"

{result, output} = run_with_filter([exclude: :test, include: [even: true]], [ParityTest])
assert result == %{failures: 1, skipped: 0, excluded: 3, total: 4}
assert output =~ "\n4 tests, 1 failure, 3 excluded\n"
assert output =~ "\n1 test, 1 failure (3 excluded)\n"
end

test "log capturing" do
Expand Down Expand Up @@ -508,16 +508,17 @@ defmodule ExUnitTest do

# Empty because it is already loaded
{result, output} = run_with_filter([exclude: :module], [])

assert result == %{failures: 0, skipped: 0, excluded: 2, total: 2}
assert output =~ "\n2 tests, 0 failures, 2 excluded\n"
assert output =~ "\n0 tests, 0 failures (2 excluded)\n"

{result, output} =
[exclude: :test, include: [module: "ExUnitTest.SecondTestModule"]]
|> run_with_filter([FirstTestModule, SecondTestModule])

assert result == %{failures: 1, skipped: 0, excluded: 1, total: 2}
assert output =~ "\n 1) test false (ExUnitTest.SecondTestModule)\n"
assert output =~ "\n2 tests, 1 failure, 1 excluded\n"
assert output =~ "\n1 test, 1 failure (1 excluded)\n"
end

test "raises on reserved tag :file in module" do
Expand Down Expand Up @@ -680,7 +681,7 @@ defmodule ExUnitTest do
end)

refute output =~ max_failures_reached_msg()
assert output =~ "\n6 tests, 0 failures, 1 excluded, 4 invalid, 1 skipped\n"
assert output =~ "\n5 tests, 0 failures, 4 invalid, 1 skipped (1 excluded)\n"
end

test "parameterized tests" do
Expand Down Expand Up @@ -787,7 +788,7 @@ defmodule ExUnitTest do
end)

assert output =~ max_failures_reached_msg()
assert output =~ "\n6 tests, 2 failures, 1 excluded, 1 skipped\n"
assert output =~ "\n5 tests, 2 failures, 1 skipped (1 excluded)\n"
end

test ":max_failures is not reached" do
Expand Down Expand Up @@ -818,7 +819,7 @@ defmodule ExUnitTest do
end)

refute output =~ max_failures_reached_msg()
assert output =~ "\n8 tests, 2 failures, 2 excluded, 1 skipped\n"
assert output =~ "\n6 tests, 2 failures, 1 skipped (2 excluded)\n"
end

test ":max_failures has been reached" do
Expand Down Expand Up @@ -852,7 +853,7 @@ defmodule ExUnitTest do
end)

assert output =~ max_failures_reached_msg()
assert output =~ "\n7 tests, 2 failures, 2 excluded, 2 skipped\n"
assert output =~ "\n5 tests, 2 failures, 2 skipped (2 excluded)\n"
end

# Excluded and skipped tests are detected before setup_all
Expand Down Expand Up @@ -886,7 +887,7 @@ defmodule ExUnitTest do
end)

assert output =~ max_failures_reached_msg()
assert output =~ "\n4 tests, 0 failures, 1 excluded, 2 invalid, 1 skipped\n"
assert output =~ "\n3 tests, 0 failures, 2 invalid, 1 skipped (1 excluded)\n"
end

test ":max_failures flushes all async/sync cases" do
Expand Down Expand Up @@ -1077,7 +1078,7 @@ defmodule ExUnitTest do
end)

assert output =~ "All tests have been excluded.\n"
assert output =~ "2 tests, 0 failures, 2 excluded\n"
assert output =~ "0 tests, 0 failures (2 excluded)\n"
end

test "tests are run in compile order (FIFO)" do
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/test/mix/tasks/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ defmodule Mix.Tasks.TestTest do
# Of the passing tests, 1 is tagged with `@tag :foo`.
# But only the failing test with that tag should run.
output = mix(["test", "--failed", "--only", "foo"])
assert output =~ "2 tests, 1 failure, 1 excluded"
assert output =~ "1 test, 1 failure (1 excluded)"

# Run again to give it a chance to record as passed
System.put_env("PASS_FAILING_TESTS", "true")
Expand Down Expand Up @@ -548,7 +548,7 @@ defmodule Mix.Tasks.TestTest do
Including tags: [location: {"test/bar_tests.exs", 5}]
"""

assert output =~ "4 tests, 0 failures, 3 excluded\n"
assert output =~ "1 test, 0 failures (3 excluded)\n"
end)
end
end
Expand Down

0 comments on commit 92f1e37

Please sign in to comment.