Testing
Test views headlessly with MyGo's tester; no window, focus or desktop session.
go test ./... should never show a window. Test views with MyGo's ui.NewTester, which renders a builder off screen and drives it with input.
func TestSaveButton(t *testing.T) {
for _, dark := range []bool{false, true} {
saved := false
tt := ui.NewTester(func(c *ui.Context) {
c.SetTheme(theme.Palette(dark))
if components.Button(c, "Save", components.ButtonPrimary).Clicked() {
saved = true
}
}, 300, 100)
if err := tt.Click("Save"); err != nil {
t.Fatal(err)
}
if !saved {
t.Errorf("dark=%v: the click was not reported", dark)
}
}
}Build the same view the application builds, and apply the theme yourself: no shell is present.
Driving a view
- Find elements by their text or accessible label with
Find,HasText,TextsandFocused. - Drive them with
Click,RightClick,Key,TypeKey,Type,Scroll,Wheel,DropFiles,Move,PressandRelease.Clickreturns an error for an element outside the window, so size the tester to fit it. Scrollscrolls at once, as a touchpad does;Wheelis a notch of a mouse wheel, which scroll containers glide through as the clock advances.- Check the keyboard path too:
tt.Key(0, ui.KeyTab), thenui.KeySpaceorui.KeyEnter. - Inspect menus with
MenuandChooseMenuItem, and screen-reader output withAnnouncements.
The tester's clock stands still until tt.Advance(d) moves it, running the frames due meanwhile. Advance it for animations, delays and tooltips instead of sleeping. After changing state outside the builder, call tt.Frame() before asserting.
Layers
- Logic tests for preferences, policies and other decisions, as plain functions that need no MyGo runtime.
- Headless view tests with
ui.NewTester, the default for component and view behavior. - Native smoke tests, opt-in behind an environment variable, for what a tester cannot reach: window lifecycle, trays, the Dock and native menus. Create their windows hidden.
Headless tests do not prove pointer, input-method or assistive-technology behavior on a real desktop. Review interface changes in a desktop session on each platform you support.