Coder Social home page Coder Social logo

assert's Introduction

This is a clone/modification of github.com/stretchr/testify, which you should use instead of this. I am going to be making changes to this at some point, so do not use/rely on it being in a good state!

assert

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

  • Prints friendly, easy to read failure descriptions
  • Allows for very readable code
  • Optionally annotate each assertion with a message

See it in action:

package yours

import (
  "testing"
  "hawx.me/code/assert"
)

func TestSomething(t *testing.T) {
  assert := assert.Wrap(t)

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

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

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

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

    // now we know that object isn't nil, we are safe to make
    // further assertions without causing any errors
    assert(object.Value).Equal("Something")
  }
}
  • 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.
  • 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.

if you assert many times, use the below:

package yours

import (
  "testing"
  "hawx.me/code/assert"
)

func TestSomething(t *testing.T) {
  assert := assert.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)
  }
}

assert's People

Contributors

2pi avatar andreas avatar anupcshan avatar comogo avatar daddye avatar doloopwhile avatar ernesto-jimenez avatar hawx avatar izaurio avatar jcelliott avatar jonhoo avatar joshrendek avatar leepa avatar matryer avatar neilconway avatar nelsam avatar obeattie avatar parkr avatar paulbellamy avatar pietern avatar pquerna avatar rgarcia avatar s-urbaniak avatar sam-nelson-mcn avatar taichi avatar technotronicoz avatar tylerb avatar viblo avatar vkryukov avatar xsleonard avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.