How to use Run method of got_test Package

Best Got code snippet using got_test.Run

Run Got automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

executor_test.go

Source: executor_test.go Github

copy
1package got_test
2
3import (
4	"bytes"
5	"errors"
6	"os/exec"
7	"strings"
8	"testing"
9
10	"github.com/google/go-cmp/cmp"
11	"github.com/tennashi/got"
12)
13
14func TestExecutor_Exec(t *testing.T) {
15	cfg := &got.ExecutorConfig{
16		IsDebug: true,
17	}
18
19	t.Run("success", func(t *testing.T) {
20		ioStream := newTestIOStream()
21		e := got.NewExecutor(ioStream, cfg)
22
23		e.AddEnv("TEST", "test")
24		err := e.Exec("sh", []string{"-c", "echo $TEST"})
25		if err != nil {
26			t.Fatalf("should not be error but: %v", err)
27		}
28
29		stdout := string(ioStream.Out.(*bytes.Buffer).Bytes())
30		if diff := cmp.Diff("test\n", stdout); diff != "" {
31			t.Fatalf("mismatch (-want, +got): %s\n", diff)
32		}
33
34		t.Logf("\n%s", ioStream.Err.(*bytes.Buffer).String())
35	})
36
37	t.Run("command not found", func(t *testing.T) {
38		ioStream := newTestIOStream()
39		e := got.NewExecutor(ioStream, cfg)
40
41		err := e.Exec("hoge", nil)
42		if err == nil {
43			t.Fatalf("should be error but not")
44		}
45
46		var execErr *exec.Error
47		if !errors.As(err, &execErr) {
48			t.Fatalf("want: *exec.Error, but got: %T", err)
49		}
50
51		if !strings.Contains(err.Error(), "executable file not found in $PATH") {
52			t.Fatalf("unexpected message: %v", err)
53		}
54
55		t.Logf("\n%s", ioStream.Err.(*bytes.Buffer).String())
56	})
57
58	t.Run("command failure", func(t *testing.T) {
59		ioStream := newTestIOStream()
60		e := got.NewExecutor(ioStream, cfg)
61
62		err := e.Exec("sh", []string{"-c", "hoge"})
63		if err == nil {
64			t.Fatalf("should be error but not")
65		}
66
67		var execErr *got.CommandExecutionError
68		if !errors.As(err, &execErr) {
69			t.Fatalf("want: *got.CommandExecutionError, but got: %T", err)
70		}
71
72		if !strings.Contains(err.Error(), "hoge: not found") {
73			t.Fatalf("unexpected message: %v", err)
74		}
75
76		t.Logf("\n%s", ioStream.Err.(*bytes.Buffer).String())
77	})
78
79}
80
Full Screen

setup_test.go

Source: setup_test.go Github

copy
1package got_test
2
3import (
4	"fmt"
5	"sync"
6	"testing"
7
8	"github.com/ysmood/got"
9	"github.com/ysmood/got/lib/gop"
10)
11
12var setup = got.Setup(func(g got.G) {
13	g.Parallel()
14})
15
16var _ got.Testable = &mock{}
17
18type mock struct {
19	sync.Mutex
20	t           *testing.T
21	failed      bool
22	skipped     bool
23	msg         string
24	cleanupList []func()
25	recover     bool
26}
27
28func (m *mock) Name() string     { return "mock" }
29func (m *mock) Skipped() bool    { return m.skipped }
30func (m *mock) Failed() bool     { return m.failed }
31func (m *mock) Helper()          {}
32func (m *mock) Cleanup(f func()) { m.cleanupList = append([]func(){f}, m.cleanupList...) }
33func (m *mock) SkipNow()         {}
34func (m *mock) Fail()            { m.failed = true }
35
36func (m *mock) FailNow() {
37	m.Lock()
38	defer m.Unlock()
39
40	m.failed = true
41	if !m.recover {
42		panic("fail now")
43	}
44	m.recover = false
45}
46
47func (m *mock) Logf(format string, args ...interface{}) {
48	m.Lock()
49	defer m.Unlock()
50
51	if m.msg != "" {
52		m.msg += "\n"
53	}
54
55	m.msg += fmt.Sprintf(format, args...)
56}
57
58func (m *mock) Run(name string, fn func(*mock)) {
59	fn(m)
60}
61
62func (m *mock) cleanup() {
63	for _, f := range m.cleanupList {
64		f()
65	}
66	m.cleanupList = nil
67}
68
69func (m *mock) check(expected string) {
70	m.t.Helper()
71	m.checkWithStyle(false, expected)
72}
73
74func (m *mock) checkWithStyle(visualizeStyle bool, expected string) {
75	m.Lock()
76	defer m.Unlock()
77
78	m.t.Helper()
79
80	if !m.failed {
81		m.t.Error("should fail")
82	}
83
84	msg := ""
85	if visualizeStyle {
86		msg = gop.VisualizeANSI(m.msg)
87	} else {
88		msg = gop.StripANSI(m.msg)
89	}
90
91	if msg != expected {
92		m.t.Errorf("\n\n[[[msg]]]\n\n%s\n\n[[[doesn't equal]]]\n\n%s\n\n", msg, expected)
93	}
94
95	m.failed = false
96	m.msg = ""
97}
98
Full Screen

printer_test.go

Source: printer_test.go Github

copy
1package got_test
2
3import (
4	"bytes"
5	"strings"
6	"testing"
7
8	"github.com/google/go-cmp/cmp"
9	"github.com/tennashi/got"
10)
11
12func TestTablePrinter_PrintInstallPackages(t *testing.T) {
13	cases := []struct {
14		input []got.InstalledPackage
15		want  []string
16		err   bool
17	}{
18		{
19			input: []got.InstalledPackage{
20				{
21					Path:     "github.com/tennashi/got",
22					Version:  "latest",
23					IsPinned: false,
24					Executables: []*got.Executable{
25						{
26							Name:    "got",
27							Path:    "/path/to/got",
28							Disable: false,
29						},
30					},
31				},
32				{
33					Path:     "github.com/tennashi/got-2",
34					Version:  "latest",
35					IsPinned: false,
36					Executables: []*got.Executable{
37						{
38							Name:    "got-2-1",
39							Path:    "/path/to/got-2-1",
40							Disable: false,
41						},
42						{
43							Name:    "got-2-2",
44							Path:    "/path/to/got-2-2",
45							Disable: false,
46						},
47					},
48				},
49				{
50					Path:     "github.com/tennashi/got-3",
51					Version:  "",
52					IsPinned: true,
53					Executables: []*got.Executable{
54						{
55							Name:    "got-3",
56							Path:    "/path/to/got-3",
57							Disable: false,
58						},
59					},
60				},
61			},
62			want: []string{
63				"NAME                       VERSION  INSTALLED EXECUTABLES  IS PINNED",
64				"github.com/tennashi/got    latest   got                    false",
65				"github.com/tennashi/got-2  latest   got-2-1,got-2-2        false",
66				"github.com/tennashi/got-3           got-3                  true",
67				"",
68			},
69			err: false,
70		},
71		{
72			input: []got.InstalledPackage{},
73			want: []string{
74				"NAME  VERSION  INSTALLED EXECUTABLES  IS PINNED",
75				"",
76			},
77			err: false,
78		},
79		{
80			input: nil,
81			want: []string{
82				"NAME  VERSION  INSTALLED EXECUTABLES  IS PINNED",
83				"",
84			},
85			err: false,
86		},
87	}
88
89	for _, tt := range cases {
90		t.Run("", func(t *testing.T) {
91			ioStream := newTestIOStream()
92			cfg := &got.TablePrinterConfig{
93				IsDebug: true,
94			}
95
96			p := got.NewTablePrinter(ioStream, cfg)
97
98			err := p.PrintInstalledPackages(tt.input)
99			if !tt.err && err != nil {
100				t.Fatalf("should not be error but: %v", err)
101			}
102			if tt.err && err == nil {
103				t.Fatalf("should be error but not")
104			}
105
106			got := ioStream.Out.(*bytes.Buffer).String()
107			if diff := cmp.Diff(strings.Join(tt.want, "\n"), got); diff != "" {
108				t.Fatalf("mismatch (-want, +got):\n%s", diff)
109			}
110		})
111	}
112}
113
114func TestTablePrinter_PrintExecutables(t *testing.T) {
115	cases := []struct {
116		input []*got.Executable
117		want  []string
118		err   bool
119	}{
120		{
121			input: []*got.Executable{
122				{
123					Name:    "got",
124					Path:    "/path/to/got",
125					Disable: false,
126				},
127				{
128					Name:    "got-2",
129					Path:    "/path/to/got-2",
130					Disable: true,
131				},
132			},
133			want: []string{
134				"NAME   PATH            DISABLED  ",
135				"got    /path/to/got    false",
136				"got-2  /path/to/got-2  true",
137				"",
138			},
139			err: false,
140		},
141		{
142			input: []*got.Executable{},
143			want: []string{
144				"NAME  PATH  DISABLED  ",
145				"",
146			},
147			err: false,
148		},
149		{
150			input: nil,
151			want: []string{
152				"NAME  PATH  DISABLED  ",
153				"",
154			},
155			err: false,
156		},
157	}
158
159	for _, tt := range cases {
160		t.Run("", func(t *testing.T) {
161			ioStream := newTestIOStream()
162			cfg := &got.TablePrinterConfig{
163				IsDebug: true,
164			}
165
166			p := got.NewTablePrinter(ioStream, cfg)
167
168			err := p.PrintExecutables(tt.input)
169			if !tt.err && err != nil {
170				t.Fatalf("should not be error but: %v", err)
171			}
172			if tt.err && err == nil {
173				t.Fatalf("should be error but not")
174			}
175
176			got := ioStream.Out.(*bytes.Buffer).String()
177			if diff := cmp.Diff(strings.Join(tt.want, "\n"), got); diff != "" {
178				t.Fatalf("mismatch (-want, +got):\n%s", diff)
179			}
180		})
181	}
182}
183
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger Run code on LambdaTest Cloud Grid

Execute automation tests with Run on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)