Skip to content

Commit d14936b

Browse files
committed
[[ Tests ]] Centralise common test teardown
This pathc resets the state of the environment for common teardown tasks to what it was prior to a test at the end of the test to reduce the need for test teardown code.
1 parent 9eb7963 commit d14936b

File tree

2 files changed

+89
-22
lines changed

2 files changed

+89
-22
lines changed

docs/development/testing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,17 @@ on TestSetup
115115
end TestSetup
116116
````
117117

118+
118119
Tests may need to clean up temporary files or other resources after running. If a script test contains a handler called `TestTeardown`, this will be run after running each test command -- even if the test failed. N.b. `TestTeardown` won't be run if running the test command causes an engine crash.
119120

121+
Any new objects created on the test stack, `mainstacks`, `sockets`,
122+
`open processes` and `open files` are automatically cleared after each
123+
test and do not need to be included in the `TestTeardown` handler or
124+
teardown included in the test. Other global properties and variables
125+
should be reset in the test teardown. The test stack is deleted from
126+
memory and reloaded for the next test if multiple tests are being run
127+
within the same process as in the standalone test runner.
128+
120129
Crashes or uncaught errors from a test command cause the test to immediately fail.
121130

122131
### LiveCode Builder

tests/_testrunnerbehavior.livecodescript

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,24 @@ end TestRunnerInvokeLoadLibraries
160160

161161
-- Execute a test
162162
command TestRunnerInvokeTestCommand pScriptFile, pCommand
163-
local tStackName, tResult
163+
local tStackName, tResult, tState
164+
165+
put the mainstacks into tState["mainstacks"]
166+
put the openSockets into tState["opensockets"]
167+
put the openProcesses into tState["openprocesses"]
168+
put the openFiles into tState["openfiles"]
169+
170+
reset the templateStack
171+
reset the templateCard
172+
reset the templateGroup
173+
reset the templateButton
174+
reset the templateField
175+
reset the templateImage
176+
reset the templateGraphic
177+
reset the templatePlayer
178+
179+
set the tool to "browse"
180+
164181
-- This should auto-load the test script
165182
put the name of stack pScriptFile into tStackName
166183

@@ -183,36 +200,77 @@ command TestRunnerInvokeTestCommand pScriptFile, pCommand
183200
dispatch "TestSetup" to tStackName
184201
put the result into tResult
185202
catch tError
186-
TestAssert "setup test", false
187-
TestDiagnostic tError
188-
end try
203+
if word 1 of tError is "SKIP" then
204+
put tError into tResult
205+
else
206+
TestAssert "setup test", false
207+
TestDiagnostic tError
208+
end if
209+
end try
189210

190211
if word 1 of tResult is "SKIP" then
191212
TestSkip pCommand, word 2 to -1 of tResult
192213
else
193214
-- Run the actual test itself
194215
try
195216
dispatch pCommand to tStackName
196-
catch tError
197-
TestAssert "execute test:" && line 1 of tError, false
198-
TestDiagnostic tError
199-
-- don't exit here as test cleanup may still be useful
217+
catch tError
218+
if word 1 of tError is "SKIP" then
219+
TestSkip pCommand, word 2 to -1 of tError
220+
else
221+
TestAssert "execute test:" && line 1 of tError, false
222+
TestDiagnostic tError
223+
-- don't exit here as test cleanup may still be useful
224+
end if
200225
end try
201-
end if
202-
203-
-- Do common cleanup tasks
204-
try
205-
dispatch "TestTearDown" to tStackName
206-
catch tError
207-
TestAssert "tear down test", false
208-
TestDiagnostic tError
209-
end try
210-
226+
227+
-- Do common cleanup tasks
228+
try
229+
dispatch "TestTearDown" to tStackName
230+
231+
-- cleanup
232+
delete tStackName
233+
234+
local tNewState
235+
put the opensockets into tNewState
236+
repeat for each line tLine in tNewState
237+
if tLine is not among the lines of tState["opensockets"] then
238+
close socket tLine
239+
end if
240+
end repeat
241+
242+
put the openprocesses into tNewState
243+
repeat for each line tLine in tNewState
244+
if tLine is not among the lines of tState["openprocesses"] then
245+
close process tLine
246+
end if
247+
end repeat
248+
249+
put the openfiles into tNewState
250+
repeat for each line tLine in tNewState
251+
if tLine is not among the lines of tState["openfiles"] then
252+
close file tLine
253+
end if
254+
end repeat
255+
256+
put the mainstacks into tNewState
257+
repeat for each line tLine in tNewState
258+
if tLine is not among the lines of tState["mainstacks"] then
259+
delete stack tLine
260+
end if
261+
end repeat
262+
catch tError
263+
TestAssert "tear down test", false
264+
TestDiagnostic tError
265+
end try
266+
211267
-- Check for uncaught errors
212-
put TestGetUncaughtErrorDialog() into tError
213-
if tError is not empty then
214-
TestAssert "uncaught error dialog", line 1 of tError
215-
TestDiagnostic tError
268+
put TestGetUncaughtErrorDialog() into tError
269+
if tError is not empty then
270+
TestAssert "uncaught error dialog", line 1 of tError
271+
TestDiagnostic tError
272+
end if
273+
216274
end if
217275
end TestRunnerInvokeTestCommand
218276

0 commit comments

Comments
 (0)