Documentation
¶
Overview ¶
Package should
This package strives to make it easy, even fun, for software developers to produce > a quick, sure, and repeatable proof that every element of the code works as it should. (See [The Programmer's Oath](http://blog.cleancoder.com/uncle-bob/2015/11/18/TheProgrammersOath.html))
The simplest way is by combining the So function with the many provided assertions, such as should.Equal:
package whatever
import (
"log"
"testing"
"github.com/mdw-go/testing/should"
)
func Test(t *testing.T) {
should.So(t, 1, should.Equal, 1)
}
This package also implement an xUnit-style test runner, which is based on the following packages:
- github.com/stretchr/testify/suite(https://pkg.go.dev/github.com/stretchr/testify/suite)
- github.com/smartystreets/gunit(https://pkg.go.dev/github.com/smartystreets/gunit)
For those using an IDE by JetBrains, you may find the following "live template" helpful:
func Test$NAME$Suite(t *testing.T) {
should.Run(&$NAME$Suite{T: should.New(t)}, should.Options.UnitTests())
}
type $NAME$Suite struct {
*should.T
}
func (this *$NAME$Suite) Setup() {
}
func (this *$NAME$Suite) Test$END$() {
}
From a test method like the one in the template above, simply use the embedded So method:
func (this TheSuite) TestSomething() {
this.So(1, should.Equal, 1)
}
Happy testing!
Index ¶
- Variables
- func BeChronological(actual any, expected ...any) error
- func BeEmpty(actual any, expected ...any) error
- func BeFalse(actual any, expected ...any) error
- func BeGreaterThan(actual any, EXPECTED ...any) error
- func BeGreaterThanOrEqualTo(actual any, expected ...any) error
- func BeIn(actual any, expected ...any) error
- func BeLessThan(actual any, EXPECTED ...any) error
- func BeLessThanOrEqualTo(actual any, expected ...any) error
- func BeNil(actual any, expected ...any) error
- func BeTrue(actual any, expected ...any) error
- func Contain(actual any, expected ...any) error
- func EndWith(actual any, expected ...any) error
- func Equal(actual any, EXPECTED ...any) error
- func HappenAfter(actual any, expected ...any) error
- func HappenBefore(actual any, expected ...any) error
- func HappenOn(actual any, expected ...any) error
- func HappenWithin(actual any, expected ...any) error
- func HaveLength(actual any, expected ...any) error
- func Panic(actual any, expected ...any) (err error)
- func Run(fixture any, options ...Option)
- func So(t *testing.T, actual any, assertion Func, expected ...any)
- func StartWith(actual any, expected ...any) error
- func WrapError(actual any, expected ...any) error
- type CompositeReporter
- type Func
- type LogReporter
- type Opt
- type Option
- type Reporter
- type T
- type TestingReporter
- type WriterReporter
Constants ¶
This section is empty.
Variables ¶
var ( ErrExpectedCountInvalid = errors.New("expected count invalid") ErrTypeMismatch = errors.New("type mismatch") ErrKindMismatch = errors.New("kind mismatch") ErrAssertionFailure = errors.New("assertion failure") )
var NOT negated
NOT (a singleton) constrains all negated assertions to their own namespace.
Functions ¶
func BeChronological ¶
BeChronological asserts whether actual is a []time.Time and whether the values are in chronological order.
func BeGreaterThan ¶
BeGreaterThan verifies that actual is greater than expected. Both actual and expected must be strings or numeric in type.
func BeGreaterThanOrEqualTo ¶
BeGreaterThanOrEqualTo verifies that actual is less than or equal to expected. Both actual and expected must be strings or numeric in type.
func BeLessThan ¶
BeLessThan verifies that actual is less than expected. Both actual and expected must be strings or numeric in type.
func BeLessThanOrEqualTo ¶
BeLessThanOrEqualTo verifies that actual is less than or equal to expected. Both actual and expected must be strings or numeric in type.
func Contain ¶
Contain determines whether actual contains expected[0]. The actual value may be a map, array, slice, or string:
- In the case of maps the expected value is assumed to be a map key.
- In the case of slices and arrays the expected value is assumed to be a member.
- In the case of strings the expected value may be a rune or substring.
func EndWith ¶
EndWith verifies that actual ends with expected[0]. The actual value may be an array, slice, or string.
func Equal ¶
Equal verifies that the actual value is equal to the expected value. It uses reflect.DeepEqual in most cases, but also compares numerics regardless of specific type and compares time.Time values using the time.Equal method.
func HappenAfter ¶
HappenAfter ensures that the first time value happens after the second.
func HappenBefore ¶
HappenBefore ensures that the first time value happens before the second.
func HappenOn ¶
HappenOn ensures that two time values happen at the same instant. See the time.Time.Equal method for the details. This function defers to Equal to do the work.
func HappenWithin ¶
HappenWithin ensures that the first time value happens within a specified duration of the other time value. The actual value should be a time.Time. The first expected value should be a time.Duration. The second expected value should be a time.Time.
func HaveLength ¶
HaveLength uses reflection to verify that len(actual) == 0.
func Panic ¶
Panic invokes the func() provided as actual and recovers from any panic. It returns an error if actual() does not result in a panic.
func Run ¶
Run accepts a fixture with Test* methods and optional setup/teardown methods and executes the suite. Fixtures must be struct types which embed a *suite.T. Assuming a fixture struct with test methods 'Test1' and 'Test2' execution would proceed as follows:
- fixture.SetupSuite()
- fixture.Setup()
- fixture.Test1()
- fixture.Teardown()
- fixture.Setup()
- fixture.Test2()
- fixture.Teardown()
- fixture.TeardownSuite()
The methods provided by Options may be supplied to this function to tweak the execution.
Types ¶
type CompositeReporter ¶
type CompositeReporter struct {
// contains filtered or unexported fields
}
Deprecated
func NewCompositeReporter ¶
func NewCompositeReporter(reporters ...Reporter) *CompositeReporter
Deprecated
type Opt ¶
type Opt struct{}
var Options Opt
Options provides the sole entrypoint to the option functions provided by this package.
func (Opt) FreshFixture ¶
FreshFixture signals to Run that the new instances of the provided fixture are to be instantiated for each and every test case. The Setup and Teardown methods are also executed on the specifically instantiated fixtures. NOTE: the SetupSuite and TeardownSuite methods are always run on the provided fixture instance, regardless of this options having been provided.
func (Opt) IntegrationTests ¶
IntegrationTests is a composite option that signals to Run that the test suite should be treated as an integration test suite, avoiding parallelism and utilizing shared fixtures to allow reuse of potentially expensive resources.
func (Opt) LongRunning ¶
LongRunning signals to Run that the provided fixture is long-running and should be skipped entirely in the case that testing.Short() returns true.
func (Opt) ParallelFixture ¶
ParallelFixture signals to Run that the provided fixture instance can be executed in parallel with other go test functions. This option assumes that `go test` was invoked with the -parallel flag.
func (Opt) ParallelTests ¶
ParallelTests signals to Run that the test methods on the provided fixture instance can be executed in parallel with each other. This option assumes that `go test` was invoked with the -parallel flag.
func (Opt) SharedFixture ¶
SharedFixture signals to Run that the provided fixture instance is to be used to run all test methods. This mode is not compatible with ParallelFixture or ParallelTests and disables them.
type Option ¶
type Option func(*config)
Option is a function that modifies a config. See Options for provided behaviors.
type TestingReporter ¶
Deprecated
type WriterReporter ¶
Deprecated
Source Files
¶
- be_chronological.go
- be_empty.go
- be_false.go
- be_greater_than.go
- be_greater_than_or_equal_to.go
- be_in.go
- be_less_than.go
- be_less_than_or_equal_to.go
- be_nil.go
- be_true.go
- contain.go
- doc.go
- end_with.go
- equal.go
- errors.go
- expected.go
- happen_after.go
- happen_before.go
- happen_on.go
- happen_within.go
- have_length.go
- kinds.go
- not.go
- panic.go
- reporter.go
- run.go
- run_options.go
- so.go
- spec.go
- start_with.go
- t.go
- wrap_error.go