Featured image of post Using the GoFakeIt Library for  Data Generation + Unit Testing in Go

Using the GoFakeIt Library for Data Generation + Unit Testing in Go

In-Depth Introduction to the GoFakeIt Library for Sample Data Generation and Unit Testing in Go

In-Depth Introduction to the GoFakeIt Library for Sample Data Generation and Unit Testing in Go

Introduction

If you’ve ever tried writing unit tests in Go, you know the struggle of creating realistic but fake data manually. Enter GoFakeIt—a powerful library for generating random, structured, and realistic test data in Go.

GoFakeIt is perfect for:

  • Unit tests – No need to manually create test data.
  • Database seeding – Populating dev databases with realistic user data, addresses, and transactions.
  • Mock API responses – Simulating API endpoints with randomized data.

Official GitHub Repo: GoFakeIt by brianvoe


Why Use GoFakeIt?

FeatureDescription
Realistic DataGenerates names, emails, addresses, products, even lorem ipsum text!
Custom RulesDefine your own data generation logic.
LocalizationSupports multiple languages (e.g., English, Spanish, German).
PerformanceFast, even when generating thousands of records.
IntegrationWorks with Go testing frameworks, databases, and API mocking.

Getting Started with GoFakeIt

1. Installing GoFakeIt

Install GoFakeIt via go get:

1
go get github.com/brianvoe/gofakeit/v6

Import it into your Go project:

1
import "github.com/brianvoe/gofakeit/v6"

Generating Fake Data

2. Generating a Fake Name

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import (
	"fmt"
	"github.com/brianvoe/gofakeit/v6"
)

func main() {
	fmt.Println(gofakeit.Name())  // Outputs a random full name
}

3. Generating Fake Emails and Addresses

1
2
fmt.Println(gofakeit.Email())  // Random email
fmt.Println(gofakeit.Address().City)  // Random city

4. Creating a Custom Fake Struct

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"fmt"
	"github.com/brianvoe/gofakeit/v6"
)

type User struct {
	FirstName string `fake:"{firstname}"`
	LastName  string `fake:"{lastname}"`
	Email     string `fake:"{email}"`
	Age       int    `fake:"{number:18,90}"`
}

func main() {
	var user User
	gofakeit.Struct(&user)

	fmt.Printf("%+v
", user)
}

Generating Fake Data for Unit Tests

5. Using GoFakeIt in Go Unit Tests

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package main

import (
	"testing"
	"github.com/brianvoe/gofakeit/v6"
)

func TestFakeEmail(t *testing.T) {
	email := gofakeit.Email()
	if email == "" {
		t.Error("Generated email is empty!")
	}
}

6. Seeding a Fake Database

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"fmt"
	"github.com/brianvoe/gofakeit/v6"
)

type User struct {
	ID    int
	Name  string
	Email string
}

func main() {
	var users []User

	for i := 0; i < 100; i++ {
		users = append(users, User{
			ID:    i + 1,
			Name:  gofakeit.Name(),
			Email: gofakeit.Email(),
		})
	}

	fmt.Println(users[0]) // Print the first fake user
}

Comparing GoFakeIt with Alternatives

LibraryFeaturesEase of UseBest For
GoFakeItHighVery EasyGeneral fake data generation
Faker (Python)HighMediumPython-based applications
MimesisMediumMediumFaster than GoFakeIt but less features
Testify MockLowEasyMocking objects, not generating data

Alternative Approaches: Pros & Cons

ApproachProsCons
Using Static DataSimple & predictableHard to maintain
Hand-Coded Test DataControl over test casesRepetitive & time-consuming
Using GoFakeItFast & realisticRequires dependency
Testify MockingGreat for mocking functionsDoesn’t generate test data

  • GoFakeIt + Testify: Use GoFakeIt for random data, and Testify for mocking dependencies.
  • GoFakeIt + GORM: Use GoFakeIt to populate databases in Go applications.
  • GoFakeIt + Echo/Gin: Use GoFakeIt for creating mock API responses.

Key Ideas

  • GoFakeIt is the best library for generating fake data in Go.
  • It supports realistic names, emails, addresses, and even custom objects.
  • It’s perfect for unit testing, database seeding, and API mock responses.
  • Alternatives like Testify Mock serve different purposes.
  • Pair GoFakeIt with Testify for ultimate testing flexibility.

References

  1. GoFakeIt GitHub Repository
  2. GoFakeIt Documentation
  3. Testify Official Docs
  4. Go Database Integration with GORM
  5. Unit Testing in Go
  6. Mocking in Go