DataDog/testify

Name: testify

Owner: Datadog, Inc.

Description: A toolkit with common assertions and mocks that plays nicely with the standard library

Forked from: stretchr/testify

Created: 2017-06-08 22:00:18.0

Updated: 2017-06-08 22:00:19.0

Pushed: 2017-06-08 22:32:03.0

Homepage:

Size: 700

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Testify - Thou Shalt Write Tests

Build Status Go Report Card GoDoc

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

Features include:

Get started:

assert package

The assert package provides some helpful methods that allow you to write better test code in Go.

See it in action:

age yours

rt (
esting"
ithub.com/stretchr/testify/assert"


 TestSomething(t *testing.T) {

 assert equality
sert.Equal(t, 123, 123, "they should be equal")

 assert inequality
sert.NotEqual(t, 123, 456, "they should not be equal")

 assert for nil (good for errors)
sert.Nil(t, object)

 assert for not nil (good when you expect something)
 assert.NotNil(t, object) {

// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal(t, "Something", object.Value)




if you assert many times, use the below:

age yours

rt (
esting"
ithub.com/stretchr/testify/assert"


 TestSomething(t *testing.T) {
sert := assert.New(t)

 assert equality
sert.Equal(123, 123, "they should be equal")

 assert inequality
sert.NotEqual(123, 456, "they should not be equal")

 assert for nil (good for errors)
sert.Nil(object)

 assert for not nil (good when you expect something)
 assert.NotNil(object) {

// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal("Something", object.Value)


require package

The require package provides same global functions as the assert package, but instead of returning a boolean result they terminate current test.

See t.FailNow for details.

http package

The http package contains test objects useful for testing code that relies on the net/http package. Check out the (deprecated) API documentation for the http package.

We recommend you use httptest instead.

mock package

The mock package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.

An example test function that tests a piece of code that relies on an external object testObj, can setup expectations (testify) and assert that they indeed happened:

age yours

rt (
esting"
ithub.com/stretchr/testify/mock"



st objects


yMockedObject is a mocked object that implements an interface
hat describes an object that the code I am testing relies on.
 MyMockedObject struct{
ck.Mock


oSomething is a method on MyMockedObject that implements some interface
nd just records the activity, and returns what the Mock object tells it to.

n the real object, this method would do something useful, but since this
s a mocked object - we're just going to stub it out.

OTE: This method is not being tested here, code that uses this object is.
 (m *MyMockedObject) DoSomething(number int) (bool, error) {

gs := m.Called(number)
turn args.Bool(0), args.Error(1)




tual test functions


estSomething is an example of how to use our test object to
ake assertions about some target code we are testing.
 TestSomething(t *testing.T) {

 create an instance of our test object
stObj := new(MyMockedObject)

 setup expectations
stObj.On("DoSomething", 123).Return(true, nil)

 call the code we are testing
rgetFuncThatDoesSomethingWithObj(testObj)

 assert that the expectations were met
stObj.AssertExpectations(t)


For more information on how to write mock code, check out the API documentation for the mock package.

You can use the mockery tool to autogenerate the mock code against an interface as well, making using mocks much quicker.

suite package

The 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.

An example suite is shown below:

asic imports
rt (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"


efine the suite, and absorb the built-in basic suite
unctionality from testify - including a T() method which
eturns the current testing context
 ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int


ake sure that VariableThatShouldStartAtFive is set to five
efore each test
 (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5


ll methods that begin with "Test" are run as tests within a
uite.
 (suite *ExampleTestSuite) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)


n order for 'go test' to run this suite, we need to create
 normal test function and pass our suite to suite.Run
 TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))

For a more complete example, using all of the functionality provided by the suite package, look at our example testing suite

For more information on writing suites, check out the API documentation for the suite package.

Suite object has assertion methods:

asic imports
rt (
"testing"
"github.com/stretchr/testify/suite"


efine the suite, and absorb the built-in basic suite
unctionality from testify - including assertion methods.
 ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int


ake sure that VariableThatShouldStartAtFive is set to five
efore each test
 (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5


ll methods that begin with "Test" are run as tests within a
uite.
 (suite *ExampleTestSuite) TestExample() {
suite.Equal(suite.VariableThatShouldStartAtFive, 5)


n order for 'go test' to run this suite, we need to create
 normal test function and pass our suite to suite.Run
 TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))


Installation

To install Testify, use go get:

* Latest version: go get github.com/stretchr/testify
* Specific version: go get gopkg.in/stretchr/testify.v1

This will then make the following packages available to you:

github.com/stretchr/testify/assert
github.com/stretchr/testify/mock
github.com/stretchr/testify/http

Import the testify/assert package into your code using this template:

age yours

rt (
esting"
ithub.com/stretchr/testify/assert"


 TestSomething(t *testing.T) {

sert.True(t, true, "True is true!")



Staying up to date

To update Testify to the latest version, use go get -u github.com/stretchr/testify.


Version History


Contributing

Please feel free to submit issues, fork the repository and send pull requests!

When 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.


Licence

Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell

Please consider promoting this project if you find it useful.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.