本文整理汇总了Golang中testing.T.Run方法的典型用法代码示例。如果您正苦于以下问题:Golang T.Run方法的具体用法?Golang T.Run怎么用?Golang T.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类testing.T
的用法示例。
在下文中一共展示了T.Run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFalse
func TestFalse(t *testing.T) {
t.Run("should not end testing if the condition is falsey", func(t *testing.T) {
tester := new(mockTester)
assert.False(tester, false, "")
if tester.errorfCalls != 0 {
t.Errorf(
"Expected Errorf not to have been called, it had been called %d times.",
tester.errorfCalls,
)
}
})
t.Run("should end testing if the condition is truthy", func(t *testing.T) {
tester := new(mockTester)
assert.False(tester, true, "")
if tester.errorfCalls != 1 {
t.Errorf(
"Expected Errorf to have been called once, it had been called %d times.",
tester.errorfCalls,
)
}
})
}
示例2: TestExamples
func TestExamples(t *testing.T) {
flag.Parse()
if !*examplesTest && os.Getenv("CI") == "" {
t.Skip("skipping examples tests, pass --examples to execute it")
return
}
defer deleteTempFolders()
examples, err := filepath.Glob(examplesFolder())
if err != nil {
t.Errorf("error finding tests: %s", err)
}
for _, example := range examples {
_, name := filepath.Split(filepath.Dir(example))
if ignored[name] {
continue
}
t.Run(name, func(t *testing.T) {
testExample(t, name, example)
})
}
}
示例3: TestParseHexColor
func TestParseHexColor(t *testing.T) {
for _, tc := range []struct {
hex string
expected color.Color
}{
{
hex: "F84",
expected: color.NRGBA{R: 0xff, G: 0x88, B: 0x44, A: 0xff},
},
{
hex: "F842",
expected: color.NRGBA{R: 0x88, G: 0x44, B: 0x22, A: 0xff},
},
{
hex: "FC8642",
expected: color.NRGBA{R: 0xfc, G: 0x86, B: 0x42, A: 0xff},
},
{
hex: "FC864210",
expected: color.NRGBA{R: 0x86, G: 0x42, B: 0x10, A: 0xfc},
},
} {
t.Run(tc.hex, func(t *testing.T) {
res, err := parseHexColor(tc.hex)
if err != nil {
t.Fatal(err)
}
if res != tc.expected {
t.Fatalf("unexpected result for \"%s\": got %#v, want %#v", tc.hex, res, tc.expected)
}
})
}
}
示例4: TestScanln
func TestScanln(t *testing.T) {
for _, r := range readers {
t.Run(r.name, func(t *testing.T) {
testScan(t, r.f, Fscanln)
})
}
}
示例5: TestHandlerAdd
func TestHandlerAdd(t *testing.T) {
tests := []struct {
description string
params *articles.HandlerAddParams
code int
}{
{"No Title", &articles.HandlerAddParams{}, http.StatusBadRequest},
{"Title filled with spaces", &articles.HandlerAddParams{Title: " "}, http.StatusBadRequest},
{"As few params as possible", &articles.HandlerAddParams{Title: "My Super Article"}, http.StatusCreated},
{"Duplicate title", &articles.HandlerAddParams{Title: "My Super Article"}, http.StatusCreated},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
rec := callHandlerAdd(t, tc.params)
assert.Equal(t, tc.code, rec.Code)
if rec.Code == http.StatusCreated {
var a articles.Article
if err := json.NewDecoder(rec.Body).Decode(&a); err != nil {
t.Fatal(err)
}
assert.NotEmpty(t, a.ID)
assert.NotEmpty(t, a.Slug)
assert.Equal(t, tc.params.Title, a.Title)
if err := a.FullyDelete(); err != nil {
t.Fatal(err)
}
}
})
}
}
示例6: TestRefreshAsync
func TestRefreshAsync(t *testing.T) {
t.Parallel()
for _, scenario := range refreshAsyncScenarios {
t.Run(scenario.description, func(t *testing.T) {
discovery := dnsdisco.NewDiscovery(scenario.service, scenario.proto, scenario.name)
discovery.SetRetriever(scenario.retriever)
discovery.SetHealthChecker(scenario.healthChecker)
finish := discovery.RefreshAsync(scenario.refreshInterval)
defer close(finish)
time.Sleep(scenario.refreshInterval + (50 * time.Millisecond))
target, port := discovery.Choose()
if target != scenario.expectedTarget {
t.Errorf("mismatch targets. Expecting: “%s”; found “%s”", scenario.expectedTarget, target)
}
if port != scenario.expectedPort {
t.Errorf("mismatch ports. Expecting: “%d”; found “%d”", scenario.expectedPort, port)
}
if errs := discovery.Errors(); !reflect.DeepEqual(errs, scenario.expectedErrors) {
t.Errorf("mismatch errors. Expecting: “%#v”; found “%#v”", scenario.expectedErrors, errs)
}
})
}
}
示例7: TestReplace
func TestReplace(t *testing.T) {
tests := []struct {
where string
start int
end int
what string
result string
}{
{"0123456789", 3, 5, "abcdef", "012abcdef56789"},
{"0123456789", 3, 5, "ab", "012ab56789"},
{"0123456789", 3, 3, "abcd", "012abcd3456789"},
{"0123456789", 0, 2, "abcd", "abcd23456789"},
{"0123456789", 0, 0, "ab", "ab0123456789"},
{"0123456789", 10, 10, "ab", "0123456789ab"},
{"0123456789", 8, 10, "ab", "01234567ab"},
{"0123456789", 5, 5, "", "0123456789"},
{"0123456789", 3, 8, "", "01289"},
{"0123456789", 3, 8, "ab", "012ab89"},
{"0123456789", 0, 5, "a", "a56789"},
{"0123456789", 5, 10, "ab", "01234ab"},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
result := replace([]byte(test.where), test.start, test.end, []byte(test.what))
if test.result != string(result) {
t.Errorf("want '%v', got '%v'", test.result, string(result))
}
})
}
}
示例8: TestDeterministic
// Test if two runs produce identical results
// even when writing different sizes to the Writer.
func TestDeterministic(t *testing.T) {
t.Parallel()
for i := 0; i <= 9; i++ {
t.Run(fmt.Sprint("L", i), func(t *testing.T) { testDeterministic(i, t) })
}
t.Run("LM2", func(t *testing.T) { testDeterministic(-2, t) })
}
示例9: TestDiff_DeepCopy
func TestDiff_DeepCopy(t *testing.T) {
cases := map[string]*Diff{
"empty": &Diff{},
"basic diff": &Diff{
Modules: []*ModuleDiff{
&ModuleDiff{
Path: []string{"root"},
Resources: map[string]*InstanceDiff{
"aws_instance.foo": &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"num": &ResourceAttrDiff{
Old: "0",
New: "2",
},
},
},
},
},
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
dup := tc.DeepCopy()
if !reflect.DeepEqual(dup, tc) {
t.Fatalf("\n%#v\n\n%#v", dup, tc)
}
})
}
}
示例10: TestStateDeepCopy
func TestStateDeepCopy(t *testing.T) {
cases := []struct {
One, Two *State
F func(*State) interface{}
}{
// Version
{
&State{Version: 5},
&State{Version: 5},
func(s *State) interface{} { return s.Version },
},
// TFVersion
{
&State{TFVersion: "5"},
&State{TFVersion: "5"},
func(s *State) interface{} { return s.TFVersion },
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) {
actual := tc.F(tc.One.DeepCopy())
expected := tc.F(tc.Two)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, actual, expected)
}
})
}
}
示例11: testOrm
func testOrm(t *testing.T, o *Orm, name string) {
tests := []func(*testing.T, *Orm){
testCodecs,
testAutoIncrement,
testTime,
testSaveDelete,
testLoadSaveMethods,
testLoadSaveMethodsErrors,
testData,
testInnerPointer,
testTransactions,
testFuncTransactions,
testCompositePrimaryKey,
testReferences,
testQueryAll,
testDefaults,
testMigrations,
testSaveUnchanged,
testQueryTransform,
}
for _, v := range tests {
t.Run(testName(name, v), func(t *testing.T) {
clearRegistry(o)
v(t, o)
})
}
}
示例12: TestGenerator
func TestGenerator(t *testing.T) {
for _, s := range []string{"gen_anyof_good_bad"} {
t.Run(s, func(t *testing.T) {
testHelper(t, "data/"+s)
})
}
}
示例13: TestEqual
func TestEqual(t *testing.T) {
t.Run("should not end testing if the expected equals the actual", func(t *testing.T) {
tester := new(mockTester)
assert.Equal(tester, 42, 42)
if tester.errorfCalls != 0 {
t.Errorf(
"Expected Errorf not to have been called, it had been called %d times.",
tester.errorfCalls,
)
}
})
t.Run("should end testing if the expected does not equal the actual", func(t *testing.T) {
tester := new(mockTester)
assert.Equal(tester, 24, 42)
if tester.errorfCalls != 1 {
t.Errorf(
"Expected Errorf to have been called once, it had been called %d times.",
tester.errorfCalls,
)
}
})
}
示例14: TestParseSysRCOutput
func TestParseSysRCOutput(t *testing.T) {
table := []struct {
Input string
K string
V string
}{
{
Input: "keyrate: fast\n",
K: "keyrate",
V: "fast",
},
{
Input: "dumpdev: \n",
K: "dumpdev",
V: "",
},
}
for _, item := range table {
t.Run(item.Input, func(t *testing.T) {
k, v, err := parseSysRCOutput(item.Input)
if err != nil {
t.Error(err)
}
if k != item.K || v != item.V {
t.Errorf("expected: k=%q, v=%q, got: k=%q, v=%q", item.K, item.V, k, v)
}
})
}
}
示例15: TestMedia
func TestMedia(t *testing.T) {
cases := []struct {
input []int64
expected []int64
}{
{
[]int64{1, 2, 3, 4, 5, 6},
[]int64{1, 1, 2, 2, 3, 3},
},
{
[]int64{10, 30, 20, 31, 32, 33, 40, 1, 2},
[]int64{10, 10, 20, 20, 30, 30, 31, 30, 30},
},
}
for i, tc := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
m := &Median{}
for i, n := range tc.input {
m.Add(n)
t.Logf("Added %d Median: %d Struct: %v", n, m.Value(), m)
if m.Value() != tc.expected[i] {
t.Errorf("got median %d wanted %d", m.Value(), tc.expected[i])
}
}
})
}
}