@@ -439,16 +439,19 @@ defmodule ExUnit.Runner do
439439 generate_test_seed ( seed , test , rand_algorithm )
440440 context = context |> Map . merge ( test . tags ) |> Map . put ( :test_pid , self ( ) )
441441 capture_log = Map . get ( context , :capture_log , capture_log )
442+ capture_io = Map . get ( context , :capture_io , false )
442443
443444 { time , test } =
444445 :timer . tc (
445446 maybe_capture_log ( capture_log , test , fn ->
446- context = maybe_create_tmp_dir ( context , test )
447-
448- case exec_test_setup ( test , context ) do
449- { :ok , context } -> exec_test ( test , context )
450- { :error , test } -> test
451- end
447+ maybe_capture_io ( capture_io , context , fn context ->
448+ context = maybe_create_tmp_dir ( context , test )
449+
450+ case exec_test_setup ( test , context ) do
451+ { :ok , context } -> exec_test ( test , context )
452+ { :error , test } -> test
453+ end
454+ end )
452455 end )
453456 )
454457
@@ -482,6 +485,32 @@ defmodule ExUnit.Runner do
482485 end
483486 end
484487
488+ defp maybe_capture_io ( true , context , fun ) do
489+ { :ok , gl } = StringIO . open ( "" )
490+ Process . group_leader ( self ( ) , gl )
491+ context = put_in ( context . capture_io , gl )
492+ test = fun . ( context )
493+ put_in ( test . capture_io , StringIO . flush ( gl ) )
494+ end
495+
496+ defp maybe_capture_io ( false , context , fun ) do
497+ fun . ( context )
498+ end
499+
500+ defp maybe_capture_io ( other , _context , _fun ) do
501+ raise ArgumentError , """
502+ invalid value for @tag :capture_io, expected one of:
503+
504+ @tag :capture_io
505+ @tag capture_io: true
506+ @tag capture_io: false
507+
508+ got:
509+
510+ @tag capture_io: #{ inspect ( other ) }
511+ """
512+ end
513+
485514 defp receive_test_reply ( test , test_pid , test_ref , timeout ) do
486515 receive do
487516 { ^ test_pid , :test_finished , test } ->
0 commit comments