11Code . require_file "../test_helper.exs" , __DIR__
22
33defmodule ExUnit.CallbacksTest do
4- use ExUnit.Case , async: true
5-
6- setup_all do
7- { :ok , [ context: :setup_all ] }
8- end
9-
10- setup do
11- { :ok , [ initial_setup: true ] }
12- end
13-
14- setup context do
15- assert context [ :initial_setup ]
16- assert context [ :context ] == :setup_all
17- { :ok , [ context: :setup ] }
18- end
19-
20- setup context do
21- if Process . get ( :ex_unit_callback ) do
22- raise "ex_unit_callback was not cleaned"
23- else
24- Process . put ( :ex_unit_callback , context [ :test ] )
25- end
26- :ok
27- end
28-
29- teardown context do
30- assert context [ :context ] == :setup
31- { :ok , context: :teardown }
32- end
33-
34- teardown context do
35- assert Process . get ( :ex_unit_callback ) == context [ :test ]
36- Process . delete ( :ex_unit_callback )
37- assert context [ :context ] == :teardown
38- :ok
39- end
40-
41- teardown_all context do
42- assert context [ :context ] == :setup_all
43- :ok
44- end
4+ use ExUnit.Case
455
466 import ExUnit.CaptureIO
477
48- test "callbacks can run custom code" do
49- assert Process . get ( :ex_unit_callback ) == :"test callbacks can run custom code"
50- end
51-
52- test "receives context from callback" , context do
53- assert context [ :context ] == :setup
54- end
55-
56- test "doesn't choke on setup errors" do
57- defmodule SetupTest do
8+ test "callbacks run custom code with context" do
9+ defmodule CallbacksTest do
5810 use ExUnit.Case
5911
60- setup _ do
61- :ok = error
12+ setup_all do
13+ { :ok , [ context: :setup_all ] }
6214 end
6315
64- test "ok" do
65- :ok
16+ setup do
17+ { :ok , [ initial_setup: true ] }
6618 end
6719
68- defp error , do: :error
69- end
70-
71- assert capture_io ( fn -> ExUnit . run end ) =~
72- "** (MatchError) no match of right hand side value: :error"
73- end
74-
75- test "doesn't choke on setup_all errors" do
76- defmodule SetupAllTest do
77- use ExUnit.Case , async: false
78-
79- setup_all _ do
80- :ok = error
20+ setup context do
21+ assert context [ :initial_setup ]
22+ assert context [ :context ] == :setup_all
23+ { :ok , [ context: :setup ] }
8124 end
8225
83- test "ok" do
84- :ok
26+ test "callbacks" , context do
27+ assert context [ :context ] == :setup
8528 end
86-
87- defp error , do: :error
8829 end
8930
90- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
91-
9231 assert capture_io ( fn -> ExUnit . run end ) =~
93- "** (MatchError) no match of right hand side value: :error "
32+ "1 tests, 0 failures "
9433 end
9534
96- test "doesn't choke on teardown errors" do
97- defmodule TeardownTest do
98- use ExUnit.Case , async: false
35+ test "doesn't choke on setup errors" do
36+ defmodule SetupTest do
37+ use ExUnit.Case
9938
100- teardown _ do
39+ setup _ do
10140 :ok = error
10241 end
10342
@@ -112,11 +51,11 @@ defmodule ExUnit.CallbacksTest do
11251 "** (MatchError) no match of right hand side value: :error"
11352 end
11453
115- test "doesn't choke on teardown_all errors" do
116- defmodule TeardownAllTest do
54+ test "doesn't choke on setup_all errors" do
55+ defmodule SetupAllTest do
11756 use ExUnit.Case , async: false
11857
119- teardown_all _ do
58+ setup_all _ do
12059 :ok = error
12160 end
12261
@@ -136,18 +75,13 @@ defmodule ExUnit.CallbacksTest do
13675 use ExUnit.Case , async: false
13776
13877 test "ok" do
139- on_exit fn ->
140- :ok = error
141- end
142-
78+ on_exit fn -> :ok = error end
14379 :ok
14480 end
14581
14682 defp error , do: :error
14783 end
14884
149- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
150-
15185 assert capture_io ( fn -> ExUnit . run end ) =~
15286 "** (MatchError) no match of right hand side value: :error"
15387 end
@@ -157,21 +91,22 @@ defmodule ExUnit.CallbacksTest do
15791 use ExUnit.Case , async: false
15892
15993 test "ok" do
160- on_exit fn ->
161- Process . exit ( self ( ) , :kill )
162- end
163-
94+ on_exit fn -> Process . exit ( self ( ) , :kill ) end
16495 :ok
16596 end
16697 end
16798
168- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
16999 assert capture_io ( fn -> ExUnit . run end ) =~
170100 ">) killed"
171101 end
172102
103+ defp no_formatters! do
104+ ExUnit . configure ( formatters: [ ] )
105+ on_exit fn -> ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] ) end
106+ end
107+
173108 test "runs multiple on_exit exits and overrides by ref" do
174- defmodule OnExitOverrideTest do
109+ defmodule OnExitSuccessTest do
175110 use ExUnit.Case , async: false
176111
177112 setup do
@@ -182,12 +117,16 @@ defmodule ExUnit.CallbacksTest do
182117 on_exit { :overridden , 1 } , fn ->
183118 IO . puts "on_exit 1 overridden -> not run"
184119 end
120+
121+ :ok
185122 end
186123
187124 setup_all do
188125 on_exit fn ->
189126 IO . puts "on_exit setup_all run"
190127 end
128+
129+ :ok
191130 end
192131
193132 test "ok" do
@@ -211,7 +150,7 @@ defmodule ExUnit.CallbacksTest do
211150 end
212151 end
213152
214- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
153+ no_formatters!
215154 output = capture_io ( fn -> ExUnit . run end )
216155
217156 assert output =~ """
@@ -224,6 +163,45 @@ defmodule ExUnit.CallbacksTest do
224163
225164 refute output =~ "not run"
226165 end
166+
167+ test "runs multiple on_exit on failure" do
168+ defmodule OnExitFailureTest do
169+ use ExUnit.Case , async: false
170+
171+ setup do
172+ on_exit fn ->
173+ IO . puts "on_exit setup run"
174+ end
175+
176+ :ok
177+ end
178+
179+ setup_all do
180+ on_exit fn ->
181+ IO . puts "on_exit setup_all run"
182+ end
183+
184+ :ok
185+ end
186+
187+ test "ok" do
188+ on_exit fn ->
189+ IO . puts "simple on_exit run"
190+ end
191+
192+ flunk "oops"
193+ end
194+ end
195+
196+ no_formatters!
197+ output = capture_io ( fn -> ExUnit . run end )
198+
199+ assert output =~ """
200+ simple on_exit run
201+ on_exit setup run
202+ on_exit setup_all run
203+ """
204+ end
227205end
228206
229207defmodule ExUnit.CallbacksNoTests do
@@ -236,12 +214,4 @@ defmodule ExUnit.CallbacksNoTests do
236214 setup do
237215 raise "Never run"
238216 end
239-
240- teardown do
241- raise "Never run"
242- end
243-
244- teardown_all do
245- raise "Never run"
246- end
247217end
0 commit comments