Example application test

We return to the GoMail code of Chapter 10, Fyne – Material-design-based GUI and create a new file, compose_test.go. As the tests are in the same package, we can test internal function definitions rather than relying on exported APIs—this is common with UI code as long as the application is not large enough to warrant separate packages or libraries. We start by adding the test imports; testing is required for go test code and github.com/stretchr/testify/assert provides helpful assertion functionality. We also import the client email library created for our GoMail examples and finally the Fyne test package, fyne.io/fyne/test:

package main

import (
"testing"

"fyne.io/fyne/test"

"github.com/PacktPublishing/Hands-On-GUI-Application-Development-in-Go/client"
"github.com/stretchr/testify/assert"
)

Now we can add a test method using the recommended naming pattern of Test<type>_<function>(); normally, the function would be a function name, but here we refer to the button title or its action. In the first part of the function, we set up the compose window for testing by calling newCompose() and passing it a test application (returned from test.NewApp()). We then prepare the state for our test—we record the size of the server outbox and set up an OnClosed handler that will report when the window is closed. Finally, we simulate typing an email address into the compose.to field using test.Type():

func TestCompose_Send(t *testing.T) {
server := client.NewTestServer()
compose := newCompose(test.NewApp(), server)
ui := compose.loadUI()

pending := len(server.Outbox)
closed := false
ui.SetOnClosed(func() {
closed = true
})
address := "[email protected]"
test.Type(compose.to, address)

...
}

Once the setup code is complete, we can implement the main test. This starts by using test.Tap() to tap the compose.send button, which should cause an email to be sent. We first verify that the window was closed after the email send completes (the OnClosed handler we added records this). Then we check that there is one more email in the server.Outbox than before.

If these tests pass, we will move to the final check. The email that was sent is extracted from the outbox so we can examine its content. With one final assertion, we verify that the email address matched what we typed into the To input box:

func TestCompose_Send(t *testing.T) {
...

test.Tap(compose.send)
assert.True(t, closed)
assert.Equal(t, pending + 1, len(server.Outbox))

email := server.Outbox[len(server.Outbox)-1]
assert.Equal(t, address, email.ToEmailString())
}

Running the preceding test will load the user interface in memory, execute the setup code, and run the tests, and then exit with the results. We run the following test with -v to see each test that is run rather than just a summary. You will notice that testing in this way takes very little time (go test reports 0.00 seconds for the test and 0.004 seconds in total); therefore, many more tests could be run on a regular basis to verify the application's behavior:

Running the user interface test took very little time

When running the tests, you may notice that this test does not cause any window to be displayed on your computer screen. This is a design feature of many test frameworks for GUI toolkits  it is much faster to run the application without displaying it for test purposes. This is often called headless mode and is very useful when running automated tests as part of a continuous integration process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.84.169