github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/docs/_docs/02_testing-best-practices/timeouts-and-logging.md (about)

     1  ---
     2  layout: collection-browser-doc
     3  title: Timeouts and logging
     4  category: testing-best-practices
     5  excerpt: >-
     6    Long-running infrastructure tests may exceed timeouts or can be killed if they do not prompt logs.
     7  tags: ["testing-best-practices", "timeout", "error"]
     8  order: 205
     9  nav_title: Documentation
    10  nav_title_link: /docs/
    11  ---
    12  
    13  Go's package testing has a default timeout of 10 minutes, after which it forcibly kills your tests—even your cleanup
    14  code won't run! It's not uncommon for infrastructure tests to take longer than 10 minutes, so you'll almost always
    15  want to increase the timeout by using the `-timeout` option, which takes a `go` duration string (e.g `10m` for 10
    16  minutes or `1h` for 1 hour):
    17  
    18  ```bash
    19  go test -timeout 30m
    20  ```
    21  
    22  Note that many CI systems will also kill your tests if they don't see any log output for a certain period of time
    23  (e.g., 10 minutes in CircleCI). If you use Go's `t.Log` and `t.Logf` for logging in your tests, you'll find that these
    24  functions buffer all log output until the very end of the test (see https://github.com/golang/go/issues/24929 for more
    25  info). If you have a long-running test, this might mean you get no log output for more than 10 minutes, and the CI
    26  system will shut down your tests. Moreover, if your test has a bug that causes it to hang, you won't see any log output
    27  at all to help you debug it.
    28  
    29  Therefore, we recommend instead using Terratest's `logger.Log` and `logger.Logf` functions, which log to `stdout`
    30  immediately:
    31  
    32  ```go
    33  func TestFoo(t *testing.T) {
    34    logger.Log(t, "This will show up in stdout immediately")
    35  }
    36  ```
    37  
    38  Finally, if you're testing multiple Go packages, be aware that Go will buffer log output—even that sent directly to
    39  `stdout` by `logger.Log` and `logger.Logf`—until all the tests in the package are done. This leads to the same
    40  difficulties with CI servers and debugging. The workaround is to tell Go to test each package sequentially using the
    41  `-p 1` flag:
    42  
    43  ```bash
    44  go test -timeout 30m -p 1 ./...
    45  ```
    46  
    47  See the [Cleanup]({{site.baseurl}}/docs/testing-best-practices/cleanup/) for more information on how to setup robust clean up procedures in the face of test timeouts and instabilities.