Skip to content

Commit a4fc68a

Browse files
authored
sqlite3_test.go: Fix go test -run=...: Use standard sub-tests (#881)
Selecting only some tests with go test -run=... does not work, because some of the tests are executed using testing.RunTests(). That function is documented as "an internal function". This changes TestSuite to use the testing subtests feature instead. This has a behaviour change: the benchmarks now need to be selected at the command line with the standard go test -bench=. flag. This will also set up the test database twice when running benchmarks, rather than once.
1 parent 2b131e0 commit a4fc68a

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

sqlite3_test.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"time"
2828
)
2929

30-
func TempFilename(t *testing.T) string {
30+
func TempFilename(t testing.TB) string {
3131
f, err := ioutil.TempFile("", "go-sqlite3-test-")
3232
if err != nil {
3333
t.Fatal(err)
@@ -1888,28 +1888,21 @@ func BenchmarkCustomFunctions(b *testing.B) {
18881888
}
18891889

18901890
func TestSuite(t *testing.T) {
1891-
tempFilename := TempFilename(t)
1892-
defer os.Remove(tempFilename)
1893-
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
1894-
if err != nil {
1895-
t.Fatal(err)
1896-
}
1897-
defer d.Close()
1891+
initializeTestDB(t)
1892+
defer freeTestDB()
18981893

1899-
db = &TestDB{t, d, SQLITE, sync.Once{}}
1900-
ok := testing.RunTests(func(string, string) (bool, error) { return true, nil }, tests)
1901-
if !ok {
1902-
t.Fatal("A subtest failed")
1894+
for _, test := range tests {
1895+
t.Run(test.Name, test.F)
19031896
}
1897+
}
19041898

1905-
if !testing.Short() {
1906-
for _, b := range benchmarks {
1907-
fmt.Printf("%-20s", b.Name)
1908-
r := testing.Benchmark(b.F)
1909-
fmt.Printf("%10d %10.0f req/s\n", r.N, float64(r.N)/r.T.Seconds())
1910-
}
1899+
func BenchmarkSuite(b *testing.B) {
1900+
initializeTestDB(b)
1901+
defer freeTestDB()
1902+
1903+
for _, benchmark := range benchmarks {
1904+
b.Run(benchmark.Name, benchmark.F)
19111905
}
1912-
db.tearDown()
19131906
}
19141907

19151908
// Dialect is a type of dialect of databases.
@@ -1924,14 +1917,37 @@ const (
19241917

19251918
// DB provide context for the tests
19261919
type TestDB struct {
1927-
*testing.T
1920+
testing.TB
19281921
*sql.DB
1929-
dialect Dialect
1930-
once sync.Once
1922+
dialect Dialect
1923+
once sync.Once
1924+
tempFilename string
19311925
}
19321926

19331927
var db *TestDB
19341928

1929+
func initializeTestDB(t testing.TB) {
1930+
tempFilename := TempFilename(t)
1931+
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
1932+
if err != nil {
1933+
os.Remove(tempFilename)
1934+
t.Fatal(err)
1935+
}
1936+
1937+
db = &TestDB{t, d, SQLITE, sync.Once{}, tempFilename}
1938+
}
1939+
1940+
func freeTestDB() {
1941+
err := db.DB.Close()
1942+
if err != nil {
1943+
panic(err)
1944+
}
1945+
err = os.Remove(db.tempFilename)
1946+
if err != nil {
1947+
panic(err)
1948+
}
1949+
}
1950+
19351951
// the following tables will be created and dropped during the test
19361952
var testTables = []string{"foo", "bar", "t", "bench"}
19371953

0 commit comments

Comments
 (0)