{"id":13413907,"url":"https://github.com/stretchr/testify","last_synced_at":"2025-05-12T14:51:44.511Z","repository":{"id":5085415,"uuid":"6247705","full_name":"stretchr/testify","owner":"stretchr","description":"A toolkit with common assertions and mocks that plays nicely with the standard library","archived":false,"fork":false,"pushed_at":"2025-03-24T17:30:37.000Z","size":1843,"stargazers_count":24523,"open_issues_count":394,"forks_count":1642,"subscribers_count":176,"default_branch":"master","last_synced_at":"2025-05-05T13:56:01.138Z","etag":null,"topics":["assertions","go","golang","mocking","testify","testing","toolkit"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"rbock/sqlpp11-connector-sqlite3","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stretchr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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":"2012-10-16T16:43:17.000Z","updated_at":"2025-05-05T12:32:51.000Z","dependencies_parsed_at":"2023-10-16T13:03:34.489Z","dependency_job_id":"993c0fd7-1fe9-43f1-a82f-da51f63545c6","html_url":"https://github.com/stretchr/testify","commit_stats":{"total_commits":807,"total_committers":292,"mean_commits":"2.7636986301369864","dds":0.9256505576208178,"last_synced_commit":"a1b9c9efe3c25c50678b1e492045164b914e255f"},"previous_names":["stretchrcom/testify"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stretchr%2Ftestify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stretchr%2Ftestify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stretchr%2Ftestify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stretchr%2Ftestify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stretchr","download_url":"https://codeload.github.com/stretchr/testify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253317168,"owners_count":21889530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["assertions","go","golang","mocking","testify","testing","toolkit"],"created_at":"2024-07-30T20:01:52.489Z","updated_at":"2025-05-12T14:51:44.486Z","avatar_url":"https://github.com/stretchr.png","language":"Go","readme":"Testify - Thou Shalt Write Tests\n================================\n\n\u003e [!NOTE]\n\u003e Testify is being maintained at v1, no breaking changes will be accepted in this repo.  \n\u003e [See discussion about v2](https://github.com/stretchr/testify/discussions/1560).\n\n[![Build Status](https://github.com/stretchr/testify/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/stretchr/testify/actions/workflows/main.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/testify)](https://goreportcard.com/report/github.com/stretchr/testify) [![PkgGoDev](https://pkg.go.dev/badge/github.com/stretchr/testify)](https://pkg.go.dev/github.com/stretchr/testify)\n\nGo code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.\n\nFeatures include:\n\n  * [Easy assertions](#assert-package)\n  * [Mocking](#mock-package)\n  * [Testing suite interfaces and functions](#suite-package)\n\nGet started:\n\n  * Install testify with [one line of code](#installation), or [update it with another](#staying-up-to-date)\n  * For an introduction to writing test code in Go, see https://go.dev/doc/code#Testing\n  * Check out the API Documentation https://pkg.go.dev/github.com/stretchr/testify\n  * Use [testifylint](https://github.com/Antonboom/testifylint) (via [golangci-lint](https://golangci-lint.run/)) to avoid common mistakes\n  * A little about [Test-Driven Development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development)\n\n[`assert`](https://pkg.go.dev/github.com/stretchr/testify/assert \"API documentation\") package\n-------------------------------------------------------------------------------------------\n\nThe `assert` package provides some helpful methods that allow you to write better test code in Go.\n\n  * Prints friendly, easy to read failure descriptions\n  * Allows for very readable code\n  * Optionally annotate each assertion with a message\n\nSee it in action:\n\n```go\npackage yours\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc TestSomething(t *testing.T) {\n\t// assert equality\n\tassert.Equal(t, 123, 123, \"they should be equal\")\n\n\t// assert inequality\n\tassert.NotEqual(t, 123, 456, \"they should not be equal\")\n\n\t// assert for nil (good for errors)\n\tassert.Nil(t, object)\n\n\t// assert for not nil (good when you expect something)\n\tif assert.NotNil(t, object) {\n\t\t// now we know that object isn't nil, we are safe to make\n\t\t// further assertions without causing any errors\n\t\tassert.Equal(t, \"Something\", object.Value)\n\t}\n}\n```\n\n  * Every assert func takes the `testing.T` object as the first argument.  This is how it writes the errors out through the normal `go test` capabilities.\n  * Every assert func returns a bool indicating whether the assertion was successful or not, this is useful for if you want to go on making further assertions under certain conditions.\n\nif you assert many times, use the below:\n\n```go\npackage yours\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc TestSomething(t *testing.T) {\n\tassert := assert.New(t)\n\n\t// assert equality\n\tassert.Equal(123, 123, \"they should be equal\")\n\n\t// assert inequality\n\tassert.NotEqual(123, 456, \"they should not be equal\")\n\n\t// assert for nil (good for errors)\n\tassert.Nil(object)\n\n\t// assert for not nil (good when you expect something)\n\tif assert.NotNil(object) {\n\t\t// now we know that object isn't nil, we are safe to make\n\t\t// further assertions without causing any errors\n\t\tassert.Equal(\"Something\", object.Value)\n\t}\n}\n```\n\n[`require`](https://pkg.go.dev/github.com/stretchr/testify/require \"API documentation\") package\n---------------------------------------------------------------------------------------------\n\nThe `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test.\nThese functions must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test.\nOtherwise race conditions may occur.\n\nSee [t.FailNow](https://pkg.go.dev/testing#T.FailNow) for details.\n\n[`mock`](https://pkg.go.dev/github.com/stretchr/testify/mock \"API documentation\") package\n----------------------------------------------------------------------------------------\n\nThe `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.\n\nAn example test function that tests a piece of code that relies on an external object `testObj`, can set up expectations (testify) and assert that they indeed happened:\n\n```go\npackage yours\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/mock\"\n)\n\n/*\n  Test objects\n*/\n\n// MyMockedObject is a mocked object that implements an interface\n// that describes an object that the code I am testing relies on.\ntype MyMockedObject struct {\n\tmock.Mock\n}\n\n// DoSomething is a method on MyMockedObject that implements some interface\n// and just records the activity, and returns what the Mock object tells it to.\n//\n// In the real object, this method would do something useful, but since this\n// is a mocked object - we're just going to stub it out.\n//\n// NOTE: This method is not being tested here, code that uses this object is.\nfunc (m *MyMockedObject) DoSomething(number int) (bool, error) {\n\targs := m.Called(number)\n\treturn args.Bool(0), args.Error(1)\n}\n\n/*\n  Actual test functions\n*/\n\n// TestSomething is an example of how to use our test object to\n// make assertions about some target code we are testing.\nfunc TestSomething(t *testing.T) {\n\t// create an instance of our test object\n\ttestObj := new(MyMockedObject)\n\n\t// set up expectations\n\ttestObj.On(\"DoSomething\", 123).Return(true, nil)\n\n\t// call the code we are testing\n\ttargetFuncThatDoesSomethingWithObj(testObj)\n\n\t// assert that the expectations were met\n\ttestObj.AssertExpectations(t)\n}\n\n// TestSomethingWithPlaceholder is a second example of how to use our test object to\n// make assertions about some target code we are testing.\n// This time using a placeholder. Placeholders might be used when the\n// data being passed in is normally dynamically generated and cannot be\n// predicted beforehand (eg. containing hashes that are time sensitive)\nfunc TestSomethingWithPlaceholder(t *testing.T) {\n\t// create an instance of our test object\n\ttestObj := new(MyMockedObject)\n\n\t// set up expectations with a placeholder in the argument list\n\ttestObj.On(\"DoSomething\", mock.Anything).Return(true, nil)\n\n\t// call the code we are testing\n\ttargetFuncThatDoesSomethingWithObj(testObj)\n\n\t// assert that the expectations were met\n\ttestObj.AssertExpectations(t)\n\n}\n\n// TestSomethingElse2 is a third example that shows how you can use\n// the Unset method to cleanup handlers and then add new ones.\nfunc TestSomethingElse2(t *testing.T) {\n\t// create an instance of our test object\n\ttestObj := new(MyMockedObject)\n\n\t// set up expectations with a placeholder in the argument list\n\tmockCall := testObj.On(\"DoSomething\", mock.Anything).Return(true, nil)\n\n\t// call the code we are testing\n\ttargetFuncThatDoesSomethingWithObj(testObj)\n\n\t// assert that the expectations were met\n\ttestObj.AssertExpectations(t)\n\n\t// remove the handler now so we can add another one that takes precedence\n\tmockCall.Unset()\n\n\t// return false now instead of true\n\ttestObj.On(\"DoSomething\", mock.Anything).Return(false, nil)\n\n\ttestObj.AssertExpectations(t)\n}\n```\n\nFor more information on how to write mock code, check out the [API documentation for the `mock` package](https://pkg.go.dev/github.com/stretchr/testify/mock).\n\nYou can use the [mockery tool](https://vektra.github.io/mockery/latest/) to autogenerate the mock code against an interface as well, making using mocks much quicker.\n\n[`suite`](https://pkg.go.dev/github.com/stretchr/testify/suite \"API documentation\") package\n-----------------------------------------------------------------------------------------\n\u003e [!WARNING]\n\u003e The suite package does not support parallel tests. See [#934](https://github.com/stretchr/testify/issues/934).\n\nThe `suite` package provides functionality that you might be used to from more common object-oriented languages.  With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with 'go test' as per normal.\n\nAn example suite is shown below:\n\n```go\n// Basic imports\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n\t\"github.com/stretchr/testify/suite\"\n)\n\n// Define the suite, and absorb the built-in basic suite\n// functionality from testify - including a T() method which\n// returns the current testing context\ntype ExampleTestSuite struct {\n\tsuite.Suite\n\tVariableThatShouldStartAtFive int\n}\n\n// Make sure that VariableThatShouldStartAtFive is set to five\n// before each test\nfunc (suite *ExampleTestSuite) SetupTest() {\n\tsuite.VariableThatShouldStartAtFive = 5\n}\n\n// All methods that begin with \"Test\" are run as tests within a\n// suite.\nfunc (suite *ExampleTestSuite) TestExample() {\n\tassert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)\n}\n\n// In order for 'go test' to run this suite, we need to create\n// a normal test function and pass our suite to suite.Run\nfunc TestExampleTestSuite(t *testing.T) {\n\tsuite.Run(t, new(ExampleTestSuite))\n}\n```\n\nFor a more complete example, using all of the functionality provided by the suite package, look at our [example testing suite](https://github.com/stretchr/testify/blob/master/suite/suite_test.go)\n\nFor more information on writing suites, check out the [API documentation for the `suite` package](https://pkg.go.dev/github.com/stretchr/testify/suite).\n\n`Suite` object has assertion methods:\n\n```go\n// Basic imports\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/suite\"\n)\n\n// Define the suite, and absorb the built-in basic suite\n// functionality from testify - including assertion methods.\ntype ExampleTestSuite struct {\n\tsuite.Suite\n\tVariableThatShouldStartAtFive int\n}\n\n// Make sure that VariableThatShouldStartAtFive is set to five\n// before each test\nfunc (suite *ExampleTestSuite) SetupTest() {\n\tsuite.VariableThatShouldStartAtFive = 5\n}\n\n// All methods that begin with \"Test\" are run as tests within a\n// suite.\nfunc (suite *ExampleTestSuite) TestExample() {\n\tsuite.Equal(suite.VariableThatShouldStartAtFive, 5)\n}\n\n// In order for 'go test' to run this suite, we need to create\n// a normal test function and pass our suite to suite.Run\nfunc TestExampleTestSuite(t *testing.T) {\n\tsuite.Run(t, new(ExampleTestSuite))\n}\n```\n\n------\n\nInstallation\n============\n\nTo install Testify, use `go get`:\n\n    go get github.com/stretchr/testify\n\nThis will then make the following packages available to you:\n\n    github.com/stretchr/testify/assert\n    github.com/stretchr/testify/require\n    github.com/stretchr/testify/mock\n    github.com/stretchr/testify/suite\n    github.com/stretchr/testify/http (deprecated)\n\nImport the `testify/assert` package into your code using this template:\n\n```go\npackage yours\n\nimport (\n\t\"testing\"\n\n\t\"github.com/stretchr/testify/assert\"\n)\n\nfunc TestSomething(t *testing.T) {\n\tassert.True(t, true, \"True is true!\")\n}\n```\n\n------\n\nStaying up to date\n==================\n\nTo update Testify to the latest version, use `go get -u github.com/stretchr/testify`.\n\n------\n\nSupported go versions\n==================\n\nWe currently support the most recent major Go versions from 1.19 onward.\n\n------\n\nContributing\n============\n\nPlease feel free to submit issues, fork the repository and send pull requests!\n\nWhen submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it.\n\nCode generation is used. [Look for `Code generated with`](https://github.com/search?q=repo%3Astretchr%2Ftestify%20%22Code%20generated%20with%22\u0026type=code) at the top of some files. Run `go generate ./...` to update generated files.\n\nWe also chat on the [Gophers Slack](https://gophers.slack.com) group in the `#testify` and `#testify-dev` channels.\n\n------\n\nLicense\n=======\n\nThis project is licensed under the terms of the MIT license.\n","funding_links":[],"categories":["Popular","测试相关","HarmonyOS","Go","开源类库","Test Frameworks","Test","Misc","Links Úteis","Testing","测试","Programming","Testing \u0026 Benchmarking","Open source library","语言资源库","自动化测试","testing","測試","Repos","Template Engines","Resources","Testing Tools by Language","Repositories","测试相关`测试库和测试数据集生成库`","Testing Frameworks","\u003cspan id=\"测试-testing\"\u003e测试 Testing\u003c/span\u003e"],"sub_categories":["查询语","Windows Manager","测试","Go","Em Português","Testing Frameworks","HTTP客户端","Golang","Advanced Console UIs","Test","go","HTTP Clients","测试框架","高級控制台界面","高级控制台界面","交流","Middlewares","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstretchr%2Ftestify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstretchr%2Ftestify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstretchr%2Ftestify/lists"}