Skip to content

Instantly share code, notes, and snippets.

@zaky
Created March 15, 2016 11:30
Show Gist options
  • Save zaky/8304b5abbdc03600c29e to your computer and use it in GitHub Desktop.
Save zaky/8304b5abbdc03600c29e to your computer and use it in GitHub Desktop.
package Fibonacci
import "testing"
func TestFibonacci(t *testing.T) {
parameters := []struct {
input, expected int
}{
{0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8},
}
for i := range parameters {
actual := compute(parameters[i].input)
if actual != parameters[i].expected {
t.Logf("expected%d: , actual:%d", parameters[i].expected, actual)
t.Fail()
}
}
}
func compute(n int) int {
result := 0
if n <= 1 {
result = n
} else {
result = compute(n-1) + compute(n-2)
}
return result
}
//In oppose to https://github.com/junit-team/junit/wiki/Parameterized-tests
@EngHabu
Copy link

EngHabu commented Sep 30, 2022

You can also add t.Run so that when one (or some) of the parameters fail, you get better error messages in go test

package Fibonacci

import "testing"

func TestFibonacci(t *testing.T) {
	parameters := []struct {
		input, expected int
	}{
		{0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8},
	}

	for i := range parameters {
                t.Run(fmt.Sprintf("Testing [%v], i), func(t *testing.T) {
        		actual := compute(parameters[i].input)
        		if actual != parameters[i].expected {
        			t.Logf("expected%d: , actual:%d", parameters[i].expected, actual)
	        		t.Fail()
		        }
                })
	}
}

func compute(n int) int {
	result := 0
	if n <= 1 {
		result = n
	} else {
		result = compute(n-1) + compute(n-2)
	}
	return result
}

//In oppose to https://github.com/junit-team/junit/wiki/Parameterized-tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment