Skip to content

Commit 37771ed

Browse files
liranliran
authored andcommitted
Initial commit: Go Testing Frameworks Examples v1.0.0
- Added 11 comprehensive testing framework examples - All tests passing (9/9 testable frameworks) - Complete documentation for each framework - GitHub Actions CI/CD pipeline - Interactive comparison table with live links - Production-ready code with comments
0 parents  commit 37771ed

39 files changed

+6107
-0
lines changed

.github/workflows/go.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: Go Tests
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
test:
11+
name: Test Go Frameworks
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
go-version: ['1.21', '1.22', '1.23']
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Go
23+
uses: actions/setup-go@v5
24+
with:
25+
go-version: ${{ matrix.go-version }}
26+
27+
- name: Display Go version
28+
run: go version
29+
30+
- name: Cache Go modules
31+
uses: actions/cache@v4
32+
with:
33+
path: |
34+
~/.cache/go-build
35+
~/go/pkg/mod
36+
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
37+
restore-keys: |
38+
${{ runner.os }}-go-${{ matrix.go-version }}-
39+
40+
- name: Download dependencies
41+
run: go mod download
42+
43+
- name: Verify dependencies
44+
run: go mod verify
45+
46+
- name: Run go fmt
47+
run: |
48+
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
49+
echo "Code is not formatted. Run 'gofmt -s -w .'"
50+
gofmt -s -l .
51+
exit 1
52+
fi
53+
54+
- name: Run go vet
55+
run: go vet ./...
56+
57+
- name: Test Built-in testing
58+
run: go test -v -race -coverprofile=coverage-builtin.txt -covermode=atomic ./01_builtin_testing/...
59+
60+
- name: Test Testify
61+
run: go test -v -race -coverprofile=coverage-testify.txt -covermode=atomic ./02_testify/...
62+
63+
- name: Install Ginkgo CLI
64+
run: go install github.com/onsi/ginkgo/v2/ginkgo@latest
65+
66+
- name: Test Ginkgo + Gomega
67+
run: |
68+
cd 03_ginkgo_gomega
69+
ginkgo -r -v --race --cover
70+
71+
- name: Test GoConvey
72+
run: go test -v -race ./04_goconvey/...
73+
74+
- name: Test GoMock
75+
run: go test -v -race ./05_gomock/...
76+
77+
- name: Test Godog
78+
run: go test -v ./06_godog/...
79+
80+
# Note: Gauge tests require Gauge to be installed
81+
# Uncomment if you have Gauge setup in CI
82+
# - name: Install Gauge
83+
# run: |
84+
# curl -SsL https://downloads.gauge.org/stable | sh
85+
# gauge install go
86+
#
87+
# - name: Test Gauge
88+
# run: |
89+
# cd 07_gauge
90+
# gauge run specs
91+
92+
- name: Test Gopter
93+
run: go test -v -race ./08_gopter/...
94+
95+
- name: Test Rapid
96+
run: go test -v -race ./09_rapid/...
97+
98+
# Note: Testcontainers tests require Docker
99+
# They are skipped in short mode
100+
- name: Test Testcontainers
101+
run: go test -v -short ./10_testcontainers_go/...
102+
103+
- name: Test httpexpect
104+
run: go test -v -race ./11_httpexpect/...
105+
106+
- name: Run all tests with coverage
107+
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
108+
109+
- name: Upload coverage to Codecov
110+
uses: codecov/codecov-action@v4
111+
with:
112+
files: ./coverage.txt
113+
flags: unittests
114+
name: codecov-umbrella
115+
fail_ci_if_error: false
116+
117+
lint:
118+
name: Lint
119+
runs-on: ubuntu-latest
120+
121+
steps:
122+
- name: Checkout code
123+
uses: actions/checkout@v4
124+
125+
- name: Set up Go
126+
uses: actions/setup-go@v5
127+
with:
128+
go-version: '1.23'
129+
130+
- name: Run golangci-lint
131+
uses: golangci/golangci-lint-action@v4
132+
with:
133+
version: latest
134+
args: --timeout=5m
135+
136+
build:
137+
name: Build
138+
runs-on: ubuntu-latest
139+
140+
steps:
141+
- name: Checkout code
142+
uses: actions/checkout@v4
143+
144+
- name: Set up Go
145+
uses: actions/setup-go@v5
146+
with:
147+
go-version: '1.23'
148+
149+
- name: Build
150+
run: go build -v ./...

.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Ignore IDE and editor files
2+
.vscode/
3+
.idea/
4+
*.swp
5+
*.swo
6+
*~
7+
.DS_Store
8+
9+
# Ignore build artifacts
10+
*.exe
11+
*.exe~
12+
*.dll
13+
*.so
14+
*.dylib
15+
*.test
16+
*.out
17+
18+
# Ignore coverage files
19+
*.coverprofile
20+
coverage.txt
21+
coverage.html
22+
*.cover
23+
24+
# Ignore dependency directories
25+
vendor/
26+
27+
# Ignore Gauge artifacts
28+
logs/
29+
reports/
30+
.gauge/
31+
32+
# Ignore test cache
33+
.cache/
34+
35+
# Ignore temporary files
36+
tmp/
37+
temp/
38+
39+
# Ignore Go workspace file
40+
go.work
41+
go.work.sum

01_builtin_testing/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Built-in Testing Package
2+
3+
Go's standard `testing` package provides the foundation for all testing in Go. It's part of the standard library, requires no external dependencies, and is the basis for all other testing frameworks.
4+
5+
## 📦 Installation
6+
7+
No installation needed - it's part of Go's standard library!
8+
9+
## 🎯 Features
10+
11+
- **Unit Testing**: Write test functions with `func TestXxx(t *testing.T)`
12+
- **Benchmarking**: Measure performance with `func BenchmarkXxx(b *testing.B)`
13+
- **Parallel Execution**: Run tests concurrently with `t.Parallel()`
14+
- **Subtests**: Organize tests hierarchically with `t.Run()`
15+
- **Table-Driven Tests**: Test multiple scenarios efficiently
16+
- **Test Coverage**: Built-in coverage analysis with `-cover` flag
17+
18+
## 📖 Usage
19+
20+
### Basic Test
21+
22+
```go
23+
func TestSum(t *testing.T) {
24+
result := Sum(2, 3)
25+
if result != 5 {
26+
t.Errorf("Expected 5, got %d", result)
27+
}
28+
}
29+
```
30+
31+
### Table-Driven Test
32+
33+
```go
34+
func TestSum(t *testing.T) {
35+
tests := []struct {
36+
name string
37+
a, b int
38+
expected int
39+
}{
40+
{"positive numbers", 2, 3, 5},
41+
{"negative numbers", -1, -2, -3},
42+
{"zero", 0, 0, 0},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
result := Sum(tt.a, tt.b)
48+
if result != tt.expected {
49+
t.Errorf("Sum(%d, %d) = %d; want %d",
50+
tt.a, tt.b, result, tt.expected)
51+
}
52+
})
53+
}
54+
}
55+
```
56+
57+
## 🚀 Running Tests
58+
59+
```bash
60+
# Run all tests
61+
go test
62+
63+
# Run with verbose output
64+
go test -v
65+
66+
# Run with coverage
67+
go test -cover
68+
69+
# Run specific test
70+
go test -run TestSum
71+
72+
# Run benchmarks
73+
go test -bench=.
74+
```
75+
76+
## 📊 Example Output
77+
78+
```
79+
=== RUN TestSum
80+
=== RUN TestSum/positive_numbers
81+
=== RUN TestSum/negative_numbers
82+
=== RUN TestSum/with_zero
83+
--- PASS: TestSum (0.00s)
84+
--- PASS: TestSum/positive_numbers (0.00s)
85+
--- PASS: TestSum/negative_numbers (0.00s)
86+
--- PASS: TestSum/with_zero (0.00s)
87+
PASS
88+
ok github.com/lirany1/go-testing-framework-examples/01_builtin_testing 0.002s
89+
```
90+
91+
## ✅ Pros
92+
93+
- ✅ Part of Go standard library (no dependencies)
94+
- ✅ Fast and lightweight
95+
- ✅ Simple and straightforward
96+
- ✅ Excellent IDE support
97+
- ✅ Built-in benchmarking and profiling
98+
- ✅ Native parallel test execution
99+
100+
## ❌ Cons
101+
102+
- ❌ No built-in assertion library (manual `if` checks)
103+
- ❌ Not BDD-style
104+
- ❌ Verbose for complex assertions
105+
- ❌ No built-in mocking (need external tools)
106+
107+
## 🔗 Resources
108+
109+
- [Official Documentation](https://pkg.go.dev/testing)
110+
- [Go Testing Tutorial](https://go.dev/doc/tutorial/add-a-test)
111+
- [Table-Driven Tests in Go](https://dave.cheney.net/2019/05/07/prefer-table-driven-tests)
112+
113+
## 💡 Best Practices
114+
115+
1. **Use Table-Driven Tests**: Test multiple scenarios efficiently
116+
2. **Name Tests Descriptively**: Use `TestFunctionName_Scenario` pattern
117+
3. **Test One Thing**: Each test should verify a single behavior
118+
4. **Use Subtests**: Organize related tests with `t.Run()`
119+
5. **Check Coverage**: Aim for meaningful coverage, not 100%

01_builtin_testing/calc/sum.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package calc
2+
3+
// Sum returns the sum of two integers.
4+
// This is a simple function used to demonstrate Go's built-in testing package.
5+
func Sum(a, b int) int {
6+
return a + b
7+
}
8+
9+
// Multiply returns the product of two integers.
10+
func Multiply(a, b int) int {
11+
return a * b
12+
}
13+
14+
// Divide returns the division of two integers.
15+
// Returns 0 if attempting to divide by zero.
16+
func Divide(a, b int) int {
17+
if b == 0 {
18+
return 0
19+
}
20+
return a / b
21+
}

0 commit comments

Comments
 (0)