github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/docs/DeveloperGenerateTest.md (about)

     1  # Generating Tests
     2  
     3  Before generating tests you should be aware of the following environment variables that are used to render new test files from a template:
     4  
     5  | Variable | Required | Explanation | Default Value |
     6  |---|---|---|---|
     7  | `TEMPLATE_SUITE_PACKAGE_NAME` | yes | The name of the package directory and spec file created in `tests/` and the name used when creating the suite file in `cmd/`  | EMPTY  |
     8  | `TEMPLATE_SPEC_FILE_NAME` | no | It will override the name of the test spec file and the text within the `Describe/It` containers. Useful when you are looking to test a different component within a service. Refer to `tests/build/` for a good example.    | `template` |
     9  | `TEMPLATE_APPEND_FRAMEWORK_DESCRIBE_FILE` | no | By default, when the test spec file is generated, the root `Describe` container function is appended to the `pkg/framework/describe.go` file, using the value of either `TEMPLATE_SUITE_PACKAGE_NAME` or `TEMPLATE_SPEC_FILE_NAME`, so it can be utilized in the current e2e test suite. In some cases, due the type of testing, you may not want to perform this action. | `true`  |
    10  | `TEMPLATE_JOIN_SUITE_SPEC_FILE_NAMES` | no | It will join the values of `TEMPLATE_SUITE_PACKAGE_NAME` and `TEMPLATE_SPEC_FILE_NAME` to be used for the `pkg/framework/describe.go` root `Describe` container function and in all text within the Gingko container text. An example might be that a `tests/chaos` package directory is created and you want the test spec file to be named per component, i.e. `build.go`, but you want the test functions and container text to be more descriptive so that they say `ChaosBuildSuiteDescribe`. | `false` |
    11  
    12   
    13   ## Generating Gingko Test Spec File
    14    
    15   ```bash
    16     $ make local/template/generate-test-spec
    17     ./mage -v local:generateTestSpecFile
    18  	Running target: Local:GenerateTestSpecFile
    19  	I1129 10:34:56.777665   11171 magefile.go:360] TEMPLATE_SPEC_FILE_NAME not set. Defaulting test spec file to value of `chaos`.
    20  	I1129 10:34:56.777923   11171 magefile.go:379] Creating new test package directory and spec file tests/chaos/chaos.go.
    21  	exec: go "fmt" "tests/chaos/chaos.go"
    22  	tests/chaos/chaos.go
    23  
    24   ```
    25   As noted above, this command will create a new package under the `tests/`directory and a test spec file `<SpecFile>.go` for you. It will contain some basic imports but more importantly it will generate a basic structured Ginkgo spec like the one below. You can update as necessary and build upon it.
    26  
    27   ```golang
    28   package chaos
    29  
    30  /* This was generated from a template file. Please feel free to update as necessary */
    31  
    32  import (
    33  	. "github.com/onsi/ginkgo/v2"
    34  	. "github.com/onsi/gomega"
    35  
    36  	"github.com/redhat-appstudio/e2e-tests/pkg/framework"
    37  	//framework imports edit as required
    38  )
    39  
    40  var _ = framework.ChaosSuiteDescribe("Chaos tests", Label("Chaos"), func() {
    41  
    42  
    43  	defer GinkgoRecover()
    44  	var err error
    45  	var f *framework.Framework
    46  	// use 'f' to access common controllers or the specific service controllers within the framework
    47  	BeforeAll(func() {
    48  		// Initialize the tests controllers
    49  		f, err = framework.NewFramework()
    50  		Expect(err).NotTo(HaveOccurred())
    51  	})
    52  
    53  
    54  	Describe("Chaos scenario to test", Label("Chaos"), func() {
    55  		// Declare variables here.
    56  
    57  		BeforeEach(func() {
    58  
    59  			// Initialize variables here.
    60  			// Assert setup here.
    61  
    62  		})
    63  
    64  		It("chaos does some test action", func() {
    65  
    66  			// Implement test and assertions here
    67  
    68  		})
    69  
    70  	})
    71  
    72  })
    73  
    74   ```
    75  
    76   ## Generating Ginkgo Test Suite File
    77  
    78   ```bash
    79     $ make local/template/generate-test-suite 
    80     ./mage -v local:generateTestSuiteFile
    81     Running target: Local:GenerateTestSuiteFile
    82     I1128 13:21:30.908854    5645 magefile.go:311] Creating new test suite file cmd/chaos_test.go.
    83     exec: go "fmt" "cmd/chaos_test.go"
    84     cmd/chaos_test.go
    85  ```
    86  
    87  This command will help setup a test suite file within the `cmd/` directory. It will do the test package import based on the value of `TEMPLATE_SUITE_PACKAGE_NAME`. So using the example above it will assume there is a `tests/chaos` package to import as well. It uses a simplified version of the `cmd/e2e_test.go` as a template to allow you to leverage the existing functionality built into the framework like webhooks eventing and Polarion formatted XML test case generation. Edit this file as you feel necessary.
    88  
    89  NOTE: You may not need to generate this file. This is useful when you want to move a type of testing into a separate suite that wouldn't go into the existing e2e test suite package. i.e. chaos testing. We have a current example with the existing `cmd/loadsTest.go` which are used to run the AppStudio Load tests.