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

     1  ---
     2  layout: collection-browser-doc
     3  title: Error handling
     4  category: testing-best-practices
     5  excerpt: >-
     6    Learn how to handle errors.
     7  tags: ["testing-best-practices", "terraform", "error"]
     8  order: 208
     9  nav_title: Documentation
    10  nav_title_link: /docs/
    11  ---
    12  
    13  Just about every method `foo` in Terratest comes in two versions: `foo` and `fooE` (e.g., `terraform.Apply` and
    14  `terraform.ApplyE`).
    15  
    16  - `foo`: The base method takes a `t *testing.T` as an argument. If the method hits any errors, it calls `t.Fatal` to
    17    fail the test.
    18  
    19  - `fooE`: Methods that end with the capital letter `E` always return an `error` as the last argument and never call
    20    `t.Fatal` themselves. This allows you to decide how to handle errors.
    21  
    22  You will use the base method name most of the time, as it allows you to keep your code more concise by avoiding
    23  `if err != nil` checks all over the place:
    24  
    25  ```go
    26  terraform.Init(t, terraformOptions)
    27  terraform.Apply(t, terraformOptions)
    28  url := terraform.Output(t, terraformOptions, "url")
    29  ```
    30  
    31  In the code above, if `Init`, `Apply`, or `Output` hits an error, the method will call `t.Fatal` and fail the test
    32  immediately, which is typically the behavior you want. However, if you are _expecting_ an error and don't want it to
    33  cause a test failure, use the method name that ends with a capital `E`:
    34  
    35  ```go
    36  if _, err := terraform.InitE(t, terraformOptions); err != nil {
    37    // Do something with err
    38  }
    39  
    40  if _, err := terraform.ApplyE(t, terraformOptions); err != nil {
    41    // Do something with err
    42  }
    43  
    44  url, err := terraform.OutputE(t, terraformOptions, "url")
    45  if err != nil {
    46    // Do something with err
    47  }
    48  ```
    49  
    50  As you can see, the code above is more verbose, but gives you more flexibility with how to handle errors.