[GH-ISSUE #326] Example for writing a test using a simulated tcell.Screen #248

Closed
opened 2026-03-04 01:03:20 +03:00 by kerem · 1 comment
Owner

Originally created by @Bios-Marcel on GitHub (Jul 23, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/326

I have been trying to write a test using tcell.SimulationScreen, but somehow I never reach a state where i can actually check the state. Instead it just times out after a while.

I have already tried manually invoking events on the tview.Application instance and the tcell.SimulationScreen, sadly that doesn't help either. I don't quite understand what's going on here. Am I missunderstanding something?

This is my testcode:

package ui

import (
	"fmt"
	"testing"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func TestExample(t *testing.T) {
	simScreen := tcell.NewSimulationScreen("UTF-8")
	simScreen.Init()
	simScreen.SetSize(10, 10)

	app := tview.NewApplication()
	app.SetScreen(simScreen)

	text := tview.NewTextView()
	text.SetText("C1")
	app.SetRoot(text, true)
	app.SetAfterDrawFunc(func(screen tcell.Screen) {
		fmt.Println("Afterdraw")
		cellOne, _, _, _ := simScreen.GetContent(0, 0)
		cellTwo, _, _, _ := simScreen.GetContent(0, 1)
		if cellOne != 'C' {
			t.Errorf("Cell missmatch. Was '%v' instead of '%v'.", cellOne, 'C')
		}
		if cellTwo != '4' {
			t.Errorf("Cell missmatch. Was '%v' instead of '%v'.", cellTwo, '4')
		}
	})
	app.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
		fmt.Println("Beforedraw")
		return true
	})

	app.Run()
}

And this the result:

Running tool: /usr/local/go/bin/go test -timeout 30s github.com/Bios-Marcel/cordless/ui -run ^(TestExample)$

Beforedraw
panic: test timed out after 30s

goroutine 26 [running]:
testing.(*M).startAlarm.func1()
	/usr/local/go/src/testing/testing.go:1334 +0xdf
created by time.goFunc
	/usr/local/go/src/time/sleep.go:169 +0x44

goroutine 1 [chan receive]:
testing.(*T).Run(0xc00023ab00, 0xc0ae76, 0xb, 0xc81dc0, 0x480101)
	/usr/local/go/src/testing/testing.go:917 +0x381
testing.runTests.func1(0xc00023a900)
	/usr/local/go/src/testing/testing.go:1157 +0x78
testing.tRunner(0xc00023a900, 0xc0001ffe30)
	/usr/local/go/src/testing/testing.go:865 +0xc0
testing.runTests(0xc000152480, 0x12c4040, 0x3, 0x3, 0x0)
	/usr/local/go/src/testing/testing.go:1155 +0x2a9
testing.(*M).Run(0xc0004fdd00, 0x0)
	/usr/local/go/src/testing/testing.go:1072 +0x162
main.main()
	_testmain.go:46 +0x13e

goroutine 19 [syscall]:
os/signal.signal_recv(0x0)
	/usr/local/go/src/runtime/sigqueue.go:139 +0x9c
os/signal.loop()
	/usr/local/go/src/os/signal/signal_unix.go:23 +0x22
created by os/signal.init.0
	/usr/local/go/src/os/signal/signal_unix.go:29 +0x41

goroutine 7 [select]:
github.com/rivo/tview.(*Application).Run(0xc0004fdd80, 0x0, 0x0)
	/home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:204 +0x1bf
github.com/Bios-Marcel/cordless/ui.TestExample(0xc00023ab00)
	/home/marcel/code/cordless/ui/tview_test.go:38 +0x341
testing.tRunner(0xc00023ab00, 0xc81dc0)
	/usr/local/go/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:916 +0x35a

goroutine 8 [select]:
github.com/gdamore/tcell.(*simscreen).PollEvent(0xc00055a2a0, 0xc82df0, 0xc0002abc90)
	/home/marcel/go/pkg/mod/github.com/gdamore/tcell@v1.1.4/simulation.go:346 +0xaf
github.com/rivo/tview.(*Application).Run.func2(0xc0002abc90, 0xc0004fdd80)
	/home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:173 +0xa0
created by github.com/rivo/tview.(*Application).Run
	/home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:160 +0x108
FAIL	github.com/Bios-Marcel/cordless/ui	30.016s
Error: Tests failed.
Originally created by @Bios-Marcel on GitHub (Jul 23, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/326 I have been trying to write a test using tcell.SimulationScreen, but somehow I never reach a state where i can actually check the state. Instead it just times out after a while. I have already tried manually invoking events on the `tview.Application` instance and the `tcell.SimulationScreen`, sadly that doesn't help either. I don't quite understand what's going on here. Am I missunderstanding something? This is my testcode: ```go package ui import ( "fmt" "testing" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func TestExample(t *testing.T) { simScreen := tcell.NewSimulationScreen("UTF-8") simScreen.Init() simScreen.SetSize(10, 10) app := tview.NewApplication() app.SetScreen(simScreen) text := tview.NewTextView() text.SetText("C1") app.SetRoot(text, true) app.SetAfterDrawFunc(func(screen tcell.Screen) { fmt.Println("Afterdraw") cellOne, _, _, _ := simScreen.GetContent(0, 0) cellTwo, _, _, _ := simScreen.GetContent(0, 1) if cellOne != 'C' { t.Errorf("Cell missmatch. Was '%v' instead of '%v'.", cellOne, 'C') } if cellTwo != '4' { t.Errorf("Cell missmatch. Was '%v' instead of '%v'.", cellTwo, '4') } }) app.SetBeforeDrawFunc(func(screen tcell.Screen) bool { fmt.Println("Beforedraw") return true }) app.Run() } ``` And this the result: ``` Running tool: /usr/local/go/bin/go test -timeout 30s github.com/Bios-Marcel/cordless/ui -run ^(TestExample)$ Beforedraw panic: test timed out after 30s goroutine 26 [running]: testing.(*M).startAlarm.func1() /usr/local/go/src/testing/testing.go:1334 +0xdf created by time.goFunc /usr/local/go/src/time/sleep.go:169 +0x44 goroutine 1 [chan receive]: testing.(*T).Run(0xc00023ab00, 0xc0ae76, 0xb, 0xc81dc0, 0x480101) /usr/local/go/src/testing/testing.go:917 +0x381 testing.runTests.func1(0xc00023a900) /usr/local/go/src/testing/testing.go:1157 +0x78 testing.tRunner(0xc00023a900, 0xc0001ffe30) /usr/local/go/src/testing/testing.go:865 +0xc0 testing.runTests(0xc000152480, 0x12c4040, 0x3, 0x3, 0x0) /usr/local/go/src/testing/testing.go:1155 +0x2a9 testing.(*M).Run(0xc0004fdd00, 0x0) /usr/local/go/src/testing/testing.go:1072 +0x162 main.main() _testmain.go:46 +0x13e goroutine 19 [syscall]: os/signal.signal_recv(0x0) /usr/local/go/src/runtime/sigqueue.go:139 +0x9c os/signal.loop() /usr/local/go/src/os/signal/signal_unix.go:23 +0x22 created by os/signal.init.0 /usr/local/go/src/os/signal/signal_unix.go:29 +0x41 goroutine 7 [select]: github.com/rivo/tview.(*Application).Run(0xc0004fdd80, 0x0, 0x0) /home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:204 +0x1bf github.com/Bios-Marcel/cordless/ui.TestExample(0xc00023ab00) /home/marcel/code/cordless/ui/tview_test.go:38 +0x341 testing.tRunner(0xc00023ab00, 0xc81dc0) /usr/local/go/src/testing/testing.go:865 +0xc0 created by testing.(*T).Run /usr/local/go/src/testing/testing.go:916 +0x35a goroutine 8 [select]: github.com/gdamore/tcell.(*simscreen).PollEvent(0xc00055a2a0, 0xc82df0, 0xc0002abc90) /home/marcel/go/pkg/mod/github.com/gdamore/tcell@v1.1.4/simulation.go:346 +0xaf github.com/rivo/tview.(*Application).Run.func2(0xc0002abc90, 0xc0004fdd80) /home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:173 +0xa0 created by github.com/rivo/tview.(*Application).Run /home/marcel/go/pkg/mod/github.com/rivo/tview@v0.0.0-20190721135419-23dc8a0944e4/application.go:160 +0x108 FAIL github.com/Bios-Marcel/cordless/ui 30.016s Error: Tests failed. ```
kerem closed this issue 2026-03-04 01:03:20 +03:00
Author
Owner

@Bios-Marcel commented on GitHub (Jul 24, 2019):

So, I was pretty much doing an integration test of the app. What I found out you can do instead, is to draw the actual components onto a tcell.SimulationScreen directly. Obviously you won't have the EventLoop logic and so on, but you don't really wanna test tview itself, so that should be fine.

<!-- gh-comment-id:514629390 --> @Bios-Marcel commented on GitHub (Jul 24, 2019): So, I was pretty much doing an integration test of the app. What I found out you can do instead, is to draw the actual components onto a `tcell.SimulationScreen` directly. Obviously you won't have the EventLoop logic and so on, but you don't really wanna test tview itself, so that should be fine.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#248
No description provided.