{"id":20545811,"url":"https://github.com/zcyc/go-test-demo","last_synced_at":"2026-02-27T14:44:50.800Z","repository":{"id":247551085,"uuid":"826178017","full_name":"zcyc/go-test-demo","owner":"zcyc","description":"这个项目展示了 Go 单元测试的多种方式，包括 Testing、Testify、Ginkgo、GoMock 和 Mockery 的使用案例。","archived":false,"fork":false,"pushed_at":"2025-04-13T15:46:29.000Z","size":36,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T10:13:08.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zcyc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-07-09T08:12:25.000Z","updated_at":"2025-04-13T15:48:13.000Z","dependencies_parsed_at":"2025-04-13T16:39:58.500Z","dependency_job_id":null,"html_url":"https://github.com/zcyc/go-test-demo","commit_stats":null,"previous_names":["zcyc/go-test-demo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zcyc/go-test-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcyc%2Fgo-test-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcyc%2Fgo-test-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcyc%2Fgo-test-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcyc%2Fgo-test-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcyc","download_url":"https://codeload.github.com/zcyc/go-test-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcyc%2Fgo-test-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29900271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T14:30:36.354Z","status":"ssl_error","status_checked_at":"2026-02-27T14:30:01.989Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T01:53:26.176Z","updated_at":"2026-02-27T14:44:50.749Z","avatar_url":"https://github.com/zcyc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go单元测试演示\n\n这个项目展示了 Go 单元测试的多种方式，包括 Testing、Testify、Ginkgo、GoMock 和 Mockery 的使用案例。\n\n## 项目结构\n\n```\n├── pkg                 # 项目源码包\n│   └── user            # 用户模块\n│       ├── dao         # 数据访问层（使用内存存储）\n│       ├── mocks       # 生成的 mock 文件\n│       ├── service     # 业务逻辑层\n│       └── tests       # 测试文件\n├── go.mod              # Go 模块文件\n├── go.sum              # Go 依赖版本记录\n└── README.md           # 项目说明文档\n```\n\n## 技术栈\n\n- **Testing**: Go 标准测试库\n- **Testify**: 更丰富的断言和 mock 功能\n- **Ginkgo**: BDD 风格测试框架\n- **GoMock**: 自动生成 mock 对象\n- **Mockery**: 自动生成 Testify mock 对象\n\n## 安装依赖工具\n\n```bash\ngo install github.com/onsi/ginkgo/v2/ginkgo\ngo install go.uber.org/mock/mockgen@latest\ngo install github.com/vektra/mockery/v2@latest\n```\n\n## Mock 文件生成\n\n### 使用命令生成\n\nGoMock:\n```bash\nmockgen -source=pkg/user/dao/struct.go -destination=pkg/user/mocks/gomock_struct.go -package=mocks\n```\n\nMockery:\n```bash\nmockery --name UserDao --output pkg/user/mocks --filename testify_struct.go --outpkg mocks\n```\n\n### 使用注释自动生成\n\n在接口定义文件中添加注释:\n\n```go\n//go:generate mockgen -source=struct.go -destination=../mocks/gomock_struct.go -package=mocks\n//go:generate mockery --name UserDao --filename testify_struct.go --outpkg mocks --output ../mocks\n```\n\n然后执行:\n\n```bash\ngo generate ./...\n```\n\n## 测试示例\n\n本项目包含三种测试方式的示例:\n\n1. **标准 testing 包**: 简单直接的测试方式\n2. **Testify**: 使用 assert 提供更丰富的断言功能\n3. **Ginkgo**: BDD 风格的测试，提供 Describe/It 语法和性能测量功能\n\n## 运行测试\n\n```bash\n# 运行所有测试\ngo test ./...\n\n# 显示详细输出\ngo test -v ./...\n\n# 显示测试覆盖率\ngo test -v -cover ./...\n\n# 生成覆盖率报告\ngo test -coverprofile=coverage.out ./...\ngo tool cover -html=coverage.out -o coverage.html\n```\n\n## Ginkgo 使用指南\n\n### 创建测试套件\n\n```bash\ncd pkg/user/tests\nginkgo bootstrap\n```\n\n### 生成测试文件\n\n```bash\nginkgo generate user\n```\n\n## 相关文档\n\n- [Testing Package](https://golang.org/pkg/testing/)\n- [Testify](https://github.com/stretchr/testify)\n- [Ginkgo](https://onsi.github.io/ginkgo/)\n- [Gomega](https://onsi.github.io/gomega/)\n- [GoMock](https://github.com/uber-go/mock)\n- [Mockery](https://vektra.github.io/mockery/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcyc%2Fgo-test-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcyc%2Fgo-test-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcyc%2Fgo-test-demo/lists"}