Skip to content

Sahil2k07/Golang-Unit-Tests

Repository files navigation

Golang-Unit-Tests

This project demonstrates how to write unit tests in Golang using the stretchr/testify package. It simulates a simple blog-like structure with users and posts, and tests the application logic in isolation using mocks.

🛠️ Technologies Used

  • Golang – The main programming language
  • Echo – A fast, minimalistic web framework for building REST APIs
  • GORM – An ORM library for Golang to interact with MySQL
  • MySQL – The relational database used for persistent storage
  • Testify – Provides easy-to-use assertion and mocking capabilities for unit tests

✅ Unit Testing

We use the github.com/stretchr/testify package to write clean and expressive unit tests.

Key features:

  • Table-driven tests
  • Mocking interfaces (e.g., repository methods)
  • Assertions like require.NoError(), require.Len(), etc.

Example Test

func TestGetUserPosts(t *testing.T) {
    mockRepo := new(mocks.PostRepoMock)
    mockRepo.On("GetUserPosts", "test@example.com").Return([]models.Post{{Title: "Post 1"}}, nil)

    posts, err := mockRepo.GetUserPosts("test@example.com")

    require.NoError(t, err)
    require.Len(t, posts, 1)

    mockRepo.AssertExpectations(t)
}

🧪 Running Tests

Make sure dependencies are installed:

go mod tidy
go mod vendor
go test ./...

🧰 Setting Up MySQL (For Real Integration)

  • Create a database for the project

    CREATE DATABASE GOLANG_UNIT_TESTS;
  • Update the DSN string for right user-name and password in configs/gorm.go

    func InitDatabase() {
        db, err := gorm.Open(mysql.New(mysql.Config{
            DSN: "root:shahil@tcp(127.0.0.1:3306)/GOLANG_UNIT_TESTS?charset=utf8&parseTime=True&loc=Local",
        }), &gorm.Config{})
        if err != nil {
            panic("Error connecting to the Database")
        }
    
        err = db.AutoMigrate(&models.User{}, &models.Post{})
        if err != nil {
            log.Fatal("Error auto migrating schemas", err)
        }
    
        DB = db
    }

🚀 Running the App

go run .

About

This project describes writing unit tests for golang web API's

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy