Github.com/stretchr/testify: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Blanked the page)
Tags: Blanking Reverted
Line 1: Line 1:
=External=
* https://github.com/stretchr/testify


=Internal=
* [[Go_Testing#Helper_Packages|Go Testing]]
=Overview=
=Installation=
<syntaxhighlight lang='bash'>
go get github.com/stretchr/testify
</syntaxhighlight>
=Programming Model=
==Assertions==
<syntaxhighlight lang='go'>
package yours
import (
  "testing"
  tassert "github.com/stretchr/testify/assert"
)
func TestSomething(t *testing.T) {
  assert := tassert.New(t)
  // assert equality
  assert.Equal(123, 123, "they should be equal")
  // assert inequality
  assert.NotEqual(123, 456, "they should not be equal")
  // assert for nil (good for errors)
  assert.Nil(object)
  // assert for not nil (good when you expect something)
  if 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)
  }
}
</syntaxhighlight>
To check that an error has the expected message:
<syntaxhighlight lang='go'>
err := ...
assert.NotNil(err)
assert.Equal(err.Error(), "expected message")
</syntaxhighlight>
==Mocks==
{{External|https://pkg.go.dev/github.com/stretchr/testify/mock}}
The Testify <code>mock</code> package provides a system by which it is possible to mock objects and verify calls into those objects are happening as expected. Mocking with Testify is based on the assumption that we want to construct [[Software_Testing_Concepts#Mock|mock instances]] to replace in testing [[Go_Interfaces#Overview|interface-defined]] real instances. Mocks can stand in for [[Software_Testing_Concepts#External_Dependency|external dependencies]] or other complex components defined by their interfaces.
The programming model requires to:
* [[#start_with_an_Interface|Start with an interface]], which will be implemented by the mock.
* [[#Defining_the_Mock|define]] the mock structure.
* [[#Instantiate_the_Mock|instantiate]] it with <code>new()</code> or <code>&SomeInterfaceMock{}</code>
* [[#Set_Up_Expectations|configure its behavior]], by configuring its methods' responses to invocations
* [[#Test_with_the_Mock_and_Verify_the_Results|pass]] it to the code that needs to be tested
* [[#Test_with_the_Mock_and_Verify_the_Results|run]] the code that needs to be tested
* [[#Test_with_the_Mock_and_Verify_the_Results|ensure]] the code behave correctly, knowing that the mock returned what we instructed it to return
* optionally, [[#Assert_Expectations|assert expectations]] on the mock.
===Start with an Interface===
Let's assume that the instances we want to mock are defined by the <code>Something</code> interface, declared as follows:
<syntaxhighlight lang='go'>
package somepkg
type Something interface {
SomeFunc(s string, i int) (string, error)
SomeOtherFunc(s string) (string, error)
}
</syntaxhighlight>
Our mocks will implement that interface and respond to invocations into it.
===<span id='Defining_the_Mock'></span>Define the Mock===
It's a good idea to encapsulate the mock definition(s) in a package-level <code>*_mocks_test.go</code> file. If we're testing a <code>somepkg</code> package, then the code lives in the <code>somepkg.go</code> file, the tests live in <code>somepkg_test.go</code> file and the mocks live in <code>somepkg_mocks_test.go</code>:
<font size=-2>
.
└── internal
     └── somepkg
        ├── somepkg.go
       ├── somepkg_test.go
       └── somepkg_mocks_test.go
</font>
The mock, implemented as the <code>SomethingMock</code> <code>struct</code>, should be defined in the <code>somepkg_mocks_test.go</code> file.
The mock struct should be declared as a wrapper around the Testify <code>mock.Mock</code> struct, which provides all functionality required to tracks activity on the actual mock object. Aside wrapping around <code>mock.Mock</code>, the struct may declare other fields, as needed. <code>SomethingMock</code> should implement all the methods that are going to be used in testing. Implementation examples are provided below: the method implementation should forward the invocation to the internal mock instance with <code>Called(args)</code> and return what the mock returns as result of <code>Called()</code>. If we know for sure that a method will not be exercised in testing, it is fine to let it <code>panic("not yet implemented")</code>.
<syntaxhighlight lang='go'>
package somepkg
import "github.com/stretchr/testify/mock"
type SomethingMock struct {
mock.Mock
    // other fields may be added here
}
// Something interface implementation
func (s *SomethingMock) SomeFunc(sa string, i int) (string, error) {
r := s.Called(sa, i)
return r.String(0), r.Error(1)
}
func (s *SomethingMock) SomeOtherFunc(sa string) (string, error) {
panic("not yet implemented")
}
</syntaxhighlight>
For arbitrary type objects, use <code>Arguments.Get(index)</code> and make a type assertion. This may cause a panic if the object you are getting is <code>nil</code>, the type assertion will fail, and in those cases you should check for <code>nil</code> first:
<syntaxhighlight lang='go'>
func (s *SomethingMock) SomeFunc2(...) (*SomeType, *SomeOtherType) {
r := s.Called(sa, i)
return r.Get(0).(*SomeType), r.Get(1).(* SomeOtherType)
}
</syntaxhighlight>
===Instantiate the Mock===
Instantiate the mock in the testing code with <code>new()</code> or <code>&SomethingMock{}</code>:
<syntaxhighlight lang='go'>
func TestSomething(t *testing.T) {
mock := new(SomethingMock)
// or mock := &SomethingMock{}
    [...]
}
</syntaxhighlight>
===Set Up Expectations===
After instantiation, we configure the mock's behavior by configuring its methods' responses to invocations. Testify calls this stage "setting up the expectations".
====Defining the Behavior of a Method====
The <code>On()</code> method may be used to configure the mock to respond in a certain way to a method invocation. If <code>On()</code> is used to set behavior, then the mock can then be queried to assert whether the call actually happened or not, with <code>[[#Assert_Expectations|AssertExpectations()]]</code>.
<syntaxhighlight lang='go'>
// On(function_name, arguments ...).Return(return_value_1, return_value_2, ...)
mock.On("SomeFunc", "fish", 3).Return("bouillabaisse ", nil)
</syntaxhighlight>
When the function is invoked with arguments different than the ones it was configured with, the mock with panic with:
<font size=-1>
mock: Unexpected Method Call
</font>
We can use specific argument values when configuring the method behavior, like in the example above, or we can use placeholders (<code>mock.Anything</code>) for each argument where the data being passed in is dynamically generated and cannot be predicted beforehand:
<syntaxhighlight lang='go'>
import (
"testing"
testifymock "github.com/stretchr/testify/mock"
)
[...]
mock.On("SomeFunc", testifymock.Anything, testifymock.Anything).Return("bouillabaisse ", nil)
</syntaxhighlight>
{{Warn|It is important to use the same number of arguments as in the method signature when configuring the mock with <code>On()</code> and <code>Anything</code>}}
===Test with the Mock and Verify the Results===
Pass the mock to the code that needs to be tested, run the code and ensure it behaves correctly, knowing that the mock returnes what we instructed it to return.
<syntaxhighlight lang='go'>
// this is the code to be tested
func Usage(si Something, s string, i int) (string, error) {
return si.SomeFunc(s, i)
}
</syntaxhighlight>
This is the test:
<syntaxhighlight lang='go'>
func TestUsage(t *testing.T) {
assert := tassert.New(t)
mock := new(SomethingMock)
mock.On("SomeFunc", "fish", 3).Return("bouillabaisse", nil)
sr, err := Usage(mock, "fish", 3)
assert.Equal("bouillabaisse", sr)
assert.Nil(err)
}
</syntaxhighlight>
===Assert Expectations===
<syntaxhighlight lang='go'>
func TestUsage(t *testing.T) {
[...]
    mock.AssertExpectations(t)
}
</syntaxhighlight>
<code>AssertExpectations</code> asserts that everything specified with <code>On()</code> and <code>Return</code> was in fact called as expected. Calls may have occurred in any order.
Other method that can be used to assert invocations with a finer granularity:
<syntaxhighlight lang='go'>
mock.AssertCalled(t, methodName string, arguments ...interface{})
mock.AssertNotCalled(t, methodName string, arguments ...interface{})
mock.AssertNumberOfCalls(t, methodName string, expectedCalls int)
mock.Called(...)
mock.IsMethodCallable(...)
mock.MethodCalled(...)
</syntaxhighlight>
===Reconfigure===
The mock instances can be "unconfigured" with <code>Unset()</code> invoked on the result of <code>On()</code> and then reconfigured again with <code>On</code>:
<syntaxhighlight lang='go'>
func TestUsage(t *testing.T) {
    mockCall := mock.On("SomeFunc", ....)
    // test
    [...]   
    mock.AssertExpectations(t)
    // unset
    mockCall.Unset()
    // reconfigure
    mockCall := mock.On("SomeFunc", ....)
    // test
    [...]
}
</syntaxhighlight>

Revision as of 00:32, 7 March 2024