@@ -165,3 +165,53 @@ class MyTest < Minitest::Test
165165 end
166166end
167167```
168+
169+ ## Testing startup tasks
170+
171+ When a functions server is starting up, it calls startup tasks automatically.
172+ In the testing environment, when you call a function using the
173+ {FunctionsFramework::Testing#call_http} or
174+ {FunctionsFramework::Testing#call_event} methods, the testing environment will
175+ also automatically execute any startup tasks for you.
176+
177+ You can also call startup tasks explicitly to test them in isolation, using the
178+ {FunctionsFramework::Testing#run_startup_tasks} method. Pass the name of a
179+ function, and the testing module will execute all defined startup blocks, in
180+ order, as if the server were preparing that function for execution.
181+ {FunctionsFramework::Testing#run_startup_tasks} returns the resulting globals
182+ as a hash, so you can assert against its contents.
183+
184+ If you use {FunctionsFramework::Testing#run_startup_tasks} to run the startup
185+ tasks explicitly, they will not be run again when you call the function itself
186+ using {FunctionsFramework::Testing#call_http} or
187+ {FunctionsFramework::Testing#call_event}. However, if startup tasks have
188+ already been run implicitly by {FunctionsFramework::Testing#call_http} or
189+ {FunctionsFramework::Testing#call_event}, then attempting to run them again
190+ explicitly by calling {FunctionsFramework::Testing#run_startup_tasks} will
191+ result in an exception.
192+
193+ There is currently no way to run a single startup block in isolation. If you
194+ have multiple startup blocks defined, they are always executed together.
195+
196+ Following is an example test that runs startup tasks explicitly and asserts
197+ against the effect on the globals.
198+
199+ ``` ruby
200+ require " minitest/autorun"
201+ require " functions_framework/testing"
202+
203+ class MyTest < Minitest ::Test
204+ include FunctionsFramework ::Testing
205+
206+ def test_startup_tasks
207+ load_temporary " app.rb" do
208+ globals = run_startup_tasks " my_function"
209+ assert_equal " foo" , globals[:my_global ]
210+
211+ request = make_get_request " https://example.com/foo"
212+ response = call_http " my_function" , request
213+ assert_equal 200 , response.status
214+ end
215+ end
216+ end
217+ ```
0 commit comments