项目作者: viniciusmarson

项目描述 :
go-expect lets you write better assertions
高级语言: Go
项目地址: git://github.com/viniciusmarson/go-expect.git
创建时间: 2017-01-27T21:00:33Z
项目社区:https://github.com/viniciusmarson/go-expect

开源协议:MIT License

下载


license

Golang testing

When you use go-expect, you write beautiful assertions as if you were writing a text. When you write assertions in this way, you don’t need to remember the order of actual and expected arguments to functions like assert.equal, which helps you write better tests.

Installing:

  1. $ go get github.com/viniciusmarson/go-expect/expect

Example:

  1. package main
  2. import (
  3. "testing"
  4. "github.com/viniciusmarson/go-expect/expect"
  5. )
  6. func TestToInclude(t *testing.T) {
  7. expect := expect.New(t)
  8. expect([]int{10,9,8}).ToInclude(10)
  9. }
  10. func TestBubbleSort(t *testing.T) {
  11. expect := expect.New(t)
  12. theExpectedResponse := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  13. response := []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
  14. BubbleSort(response)
  15. expect(response).ToBe(theExpectedResponse)
  16. }

Features:

ToExist

expect(value).ToExist()

Asserts the given value is not nil.

  1. expect("something truthy").ToExist()

ToNotExist

expect(value).ToNotExist()

Asserts the given value is nil.

  1. expect(nil).ToNotExist()

ToBe

expect(value).ToBe(expectedValue)

Asserts that value is strictly equal to expectedValue.

  1. expect([]int{ 1, 2, 3, 4 }).ToBe([]int{ 1, 2, 3, 4 })
  2. expect("test").ToBe("test")
  3. expect(11).ToBe(11)

ToNotBe

expect(value).ToNotBe(expectedValue)

Asserts that value is not strictly equal to expectedValue.

  1. expect([]int{ 1, 2, 3, 4 }).ToNotBe([]int{ 4, 3, 2, 1 })
  2. expect("test").ToNotBe("tset")
  3. expect(10).ToNotBe(11)

ToBeAn and ToBeA

expect(value).ToBeAn(expectedType)

Asserts that value is of the type expectedType.

  1. expect("test").ToBeA("string")
  2. expect(true).ToBeA("bool")
  3. expect(10).ToBeAn("int")
  4. expect([]int{}).ToBeA("slice")
  5. expect(`some interface{}`).ToBeAn("interface")
  6. expect(`some struct`).ToBeA("struct")

ToBeTrue

expect(bool).ToBeTrue()

Asserts that bool is true.

  1. expect(true).ToBeTrue()

ToBeFalse

expect(bool).ToBeFalse()

Asserts that bool is false.

  1. expect(false).ToBeFalse()

Contains

expect(string).Contains(expectedString)

Asserts that string contains expectedString.

  1. expect("banana").Contains("nana")

NotContains

expect(string).NotContains(notExpectedString)

Asserts that string not contains notExpectedString.

  1. expect("banana").NotContains("haha")

ToBeLessThan

expect(number).ToBeLessThan(value)

Asserts the given number is less than value.

  1. expect(2).ToBeLessThan(3)

ToBeLessThanOrEqualTo

expect(number).ToBeLessThanOrEqualTo(value)

Asserts the given number is less than or equal to value.

  1. expect(2).ToBeLessThanOrEqualTo(3)

ToBeGreaterThan

expect(number).ToBeGreaterThan(valu)

Asserts the given number is greater than value.

  1. expect(3).ToBeGreaterThan(2)

ToBeGreaterThanOrEqualTo

expect(number).ToBeGreaterThanOrEqualTo(value)

Asserts the given number is greater than or equal to value.

  1. expect(3).ToBeGreaterThanOrEqualTo(2)

ToInclude

expect(slice).ToInclude(value)

Asserts the given slice contains the value.

  1. expect([]int{ 10, 9 , 8 }).ToInclude(9)

ToExclude

expect(slice).ToExclude(value)

Asserts the given slice not contains the value.

  1. expect([]int{ 10, 9 , 8 }).ToExclude(2)

Fail

expect().Fail()

Explicitly forces failure.

  1. expect().Fail()
  2. expect().Fail("Custom message")

TODO

Go