Golang Function Driven Tests
7y agoen
Source
jdx.devGolang Function Driven Testsjdx.devDave Cheney had a fantastic post on improving test structure in Go code with table-driven tests. I want to expand on it just a tiny bit to show how table-driven tests can be made more concise with functions. If you haven’t read his article, read that before continuing. The end result of Dave’s post is the following example: func TestSplit (t * testing.T) { tests := map [ string ] struct { input string sep string want [] string }{ "simple" : {input: "a/b/c" , sep: "/" , want: [] string { "a" , "b" , "c" }}, "wrong sep" : {input: "a/b/c" , sep: "," , want: [] string { "a/b/c" }}, "no sep" : {input: "abc" , sep: "/" , want: [] string { "abc" }}, "trailing sep" : {input: "a/b/c/" , sep: "/" , want: [] string { "a" , "b" , "c" }}, } for name, tc := range tests { got := Split (tc.input, tc.sep) if !reflect. DeepEqual (tc.want, got) { t. Fatalf ( "%s: expected: %v, got: %v" , name, tc.want, got) } } } This is a nice way to reduce code duplication, but closing over a new function we can go one step further
You might also wanna read
Go 1.25 Testing/Synctest Package: Simplifying Asynchronous Code Testing
The article discusses the testing/synctest package in Go 1.25, which has graduated from experimental to general availability. It explains th
Proposal: Adding Generic Methods to the Go Programming Language
This article proposes adding generic methods to Go, which would allow methods to declare their own type parameters (not just inherit them fr
i need support to golang' language
community.notepad-plus-plus.org·3y ago
Go's lack of stack traces in errors reduces log usefulness for debugging
This article discusses a fundamental issue with error handling in Go programming language: unlike most other languages, Go errors do not con
Applying "Parse, Don't Validate" and Type-Driven Design Principles in Rust Programming
This article explores the "Parse, don't Validate" programming pattern and type-driven design principles in the context of Rust programming.
Freer Monads, More Extensible Effects [pdf]
okmij.org·7mo ago

Comments
Sign in to join the conversation.
No comments yet. Be the first.