Programming Language: Golang

Namespace/Package Name: github.com/motain/gocheck

Class/Type: C

Examples at hotexamples.com: 30

The package library is github.com/motain/gocheck, and it provides a testing framework for Go programs that emphasizes readability and extensibility.

**Example 1:** Using gocheck to test a simple function.

This code tests a simple `Fibonacci` function using gocheck's `Checker`. The `Fibonacci` function is defined as an ordinary Go function, and the tests are defined within a `SuiteFibonacci` function. The `Checkers` variable is used to append the `DeepEqual` checker, and the `RunSpecs` function is used to run the tests.

**Example 2:** Using gocheck to test a struct

import ( "testing" . "gopkg.in/check.v1" ) func TestFibonacci(t *testing.T) { checker := &Checker{Checker: &checkers.DeepEqual{}} Checkers = append(Checkers, checker) r := RunSpecs(t, "Fibonacci Suite") if !r.Passed() { t.Errorf("Test failed") } } func SuiteFibonacci(c *C) { c.Assert(Fibonacci(1), checker.Equals, 0) c.Assert(Fibonacci(2), checker.Equals, 1) c.Assert(Fibonacci(3), checker.Equals, 1) c.Assert(Fibonacci(4), checker.Equals, 2) c.Assert(Fibonacci(5), checker.Equals, 3) } func Fibonacci(n int) int { if n <= 1 { return 0 } else if n == 2 { return 1 } else { return Fibonacci(n-1) + Fibonacci(n-2) } }

This code tests a simple `Fibonacci` function using gocheck's `Checker`. The `Fibonacci` function is defined as an ordinary Go function, and the tests are defined within a `SuiteFibonacci` function. The `Checkers` variable is used to append the `DeepEqual` checker, and the `RunSpecs` function is used to run the tests.

import ( "testing" . "gopkg.in/check.v1" ) func TestPerson(t *testing.T) { tester := &TestPerson{} Suite(&testers{tester}) } type TestPerson struct{} func (t *TestPerson) TestName(c *C) { p := Person{Name: "Alice"} c.Assert(p.Name, checker.Equals, "Alice") } func (t *TestPerson) TestAge(c *C) { p := Person{Age: 25} c.Assert(p.Age, checker.Equals, 25) } type Person struct { Name string Age int }This code tests a simple `Person` struct using gocheck's `Checker`. The `Person` struct is defined as an ordinary Go struct, and the tests are defined within the `TestPerson` struct. The `Suite` function is used to group the tests together, and each test checks a field of the `Person` struct using the `Equals` checker.

Frequently Used Methods

Related

Related in langs