Socket
Socket
Sign inDemoInstall

github.com/alecthomas/assert

Package Overview
Dependencies
5
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/alecthomas/assert

Package assert provides a set of comprehensive testing tools for use with the normal Go testing system. The following is a complete example using assert in a standard test function: if you assert many times, use the format below: Assertions allow you to easily write test code, and are global funcs in the `assert` package. All assertion functions take, as the first argument, the `*testing.T` object provided by the testing framework. This allows the assertion funcs to write the failings and other details to the correct place. Every assertion function also takes an optional string message as the final argument, allowing custom error messages to be appended to the message the assertion method outputs.


Version published

Readme

Source

Go assertion library (fork of stretchr/testify/require)

This is a fork of stretchr's assertion library that does two things:

  1. It makes spotting differences in equality much easier. It uses repr and diffmatchpatch to display structural differences in colour.
  2. Aborts tests on first assertion failure (the same behaviour as stretchr/testify/require).

Example

Given the following test:

type Person struct {
  Name string
  Age  int
}

// Use github.com/alecthomas/assert
func TestDiff(t *testing.T) {
  expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
  actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
  assert.Equal(t, expected, actual)
}

// Use github.com/stretchr/testify/require
func TestTestifyDiff(t *testing.T) {
  expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
  actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
  require.Equal(t, expected, actual)
}

The following output illustrates the problems this solves. Firstly, it shows nested structures correctly, and secondly it highlights the differences between actual and expected text.

FAQs

Last updated on 01 Dec 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc