@@ -287,6 +287,7 @@ example](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/
287
287
* We use parallel processing for tests, so tests should be capable of running in parallel with one another.
288
288
* Use pytest's fixture for resource setup and teardown, instead of
289
289
having them in the test itself.
290
+ * Avoid infinite loops.
290
291
291
292
### Arrange, Act, Assert
292
293
@@ -347,8 +348,43 @@ is expected. Strive to verify the content of the output rather than the syntax.
347
348
For example, the test might verify that a string is included in the output,
348
349
without taking a dependency on where that string occurs in the output.
349
350
350
- ### Running tests
351
+ ### Avoid infinite loops
352
+
353
+ Never put potential infinite loops in the test code path. A typical
354
+ example is about gRPC's LongRunningOperations. Make sure you pass the
355
+ timeout parameter to the ` result() ` call.
356
+
357
+ Good:
358
+
359
+ ``` python
360
+ # will raise google.api_core.GoogleAPICallError after 60 seconds
361
+ operation.result(60 )
362
+ ```
363
+
364
+ Bad:
365
+
366
+ ``` python
367
+ operation.result() # this could wait forever.
368
+ ```
351
369
370
+ We recommend the timeout parameter to be around the number that gives
371
+ you more than 90% success rate. Don't put too long a timeout.
372
+
373
+ Now this test is inevitably flaky, so consider marking the test as
374
+ ` flaky ` as follows:
375
+
376
+ ``` python
377
+
378
+ @pytest.mark.flaky (max_runs = 3 , min_passes = 1 )
379
+ def my_flaky_test ():
380
+ # test that involves LRO poling with the timeout
381
+ ```
382
+
383
+ This combination will give you very high success rate with fixed test
384
+ execution time (0.999 success rate and 180 seconds operation wait time
385
+ in the worst case in this example).
386
+
387
+ ### Running tests
352
388
353
389
Automated testing for samples in ` python-docs-samples ` is managed by
354
390
[ nox] ( https://nox.readthedocs.io ) . Nox allows us to run a variety of tests,
@@ -387,7 +423,6 @@ To run a specific test from a specific following:
387
423
nox -s py-3.7 -- snippets_test.py:test_list_blobs
388
424
```
389
425
390
-
391
426
### Test Environment Setup
392
427
393
428
Because all tests are system tests that use live resources, running tests
0 commit comments