Skip to content

Commit

Permalink
add subtest
Browse files Browse the repository at this point in the history
  • Loading branch information
bigwhite committed Mar 11, 2023
1 parent 07a31a2 commit 968bd3c
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
58 changes: 58 additions & 0 deletions subtest/add_sub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"bytes"
"fmt"
"runtime"
"strconv"
"testing"
)

var goroutineSpace = []byte("goroutine ")

func curGoroutineID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
// Parse the 4707 out of "goroutine 4707 ["
b = bytes.TrimPrefix(b, goroutineSpace)
i := bytes.IndexByte(b, ' ')
if i < 0 {
panic(fmt.Sprintf("No space found in %q", b))
}
b = b[:i]
n, err := strconv.ParseUint(string(b), 10, 64)
if err != nil {
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
}
return n
}

// 被测代码
func Add(a, b int) int {
return a + b
}

func TestAddWithSubtest(t *testing.T) {
//t.Log("g:", curGoroutineID())
cases := []struct {
name string
a int
b int
r int
}{
{"2+3", 2, 3, 5},
{"2+0", 2, 0, 2},
{"2+(-2)", 2, -2, 0},
//... ...
}

for _, caze := range cases {
t.Run(caze.name, func(t *testing.T) {
t.Log("g:", curGoroutineID())
got := Add(caze.a, caze.b)
if got != caze.r {
t.Errorf("got %d, want %d", got, caze.r)
}
})
}
}
82 changes: 82 additions & 0 deletions subtest/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"bytes"
"fmt"
"runtime"
"strconv"
"testing"
)

var goroutineSpace = []byte("goroutine ")

func curGoroutineID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
// Parse the 4707 out of "goroutine 4707 ["
b = bytes.TrimPrefix(b, goroutineSpace)
i := bytes.IndexByte(b, ' ')
if i < 0 {
panic(fmt.Sprintf("No space found in %q", b))
}
b = b[:i]
n, err := strconv.ParseUint(string(b), 10, 64)
if err != nil {
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
}
return n
}

// 被测代码
func Add(a, b int) int {
return a + b
}

// 测试代码
func TestAdd(t *testing.T) {
t.Log("g:", curGoroutineID())
t.Parallel()
got := Add(2, 3)
if got != 5 {
t.Errorf("Add(2, 3) got %d, want 5", got)
}
}

func TestAddZero(t *testing.T) {
t.Parallel()
got := Add(2, 0)
if got != 2 {
t.Fatalf("Add(2, 0) got %d, want 2", got)
}
}

func TestAddOppositeNum(t *testing.T) {
got := Add(2, -2)
t.Parallel()
if got != 0 {
t.Errorf("Add(2, -2) got %d, want 0", got)
}
}

func TestAddWithTable(t *testing.T) {
t.Log("g:", curGoroutineID())
t.Parallel()
cases := []struct {
name string
a int
b int
r int
}{
{"2+3", 2, 3, 5},
{"2+0", 2, 0, 2},
{"2+(-2)", 2, -2, 0},
//... ...
}

for _, caze := range cases {
got := Add(caze.a, caze.b)
if got != caze.r {
t.Errorf("%s got %d, want %d", caze.name, got, caze.r)
}
}
}
3 changes: 3 additions & 0 deletions subtest/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module demo

go 1.20

0 comments on commit 968bd3c

Please sign in to comment.