Skip to content

Instantly share code, notes, and snippets.

@hectorgool
Created September 4, 2017 22:10
Show Gist options
  • Select an option

  • Save hectorgool/63289c3fa5cd6fc13c4e356f5178f28a to your computer and use it in GitHub Desktop.

Select an option

Save hectorgool/63289c3fa5cd6fc13c4e356f5178f28a to your computer and use it in GitHub Desktop.
ejemplo de testing
package operations
func Suma( a, b int) int {
return a + b
}
func Resta( a, b int) int {
return a - b
}
func Multiplicacion( a, b int) int {
return a * b
}
/*
go test -v -bench .
go test -v -benchmen .
*/
package operations
import (
"testing"
)
type DatosDePrueba struct {
entradas []int
resultado int
}
var parametrosDePrueba = []DatosDePrueba{
{ entradas: []int{1,2}, resultado: 2 },
{ entradas: []int{3,3}, resultado: 9 },
{ entradas: []int{5,4}, resultado: 20 },
{ entradas: []int{0,0}, resultado: 0 },
{ entradas: []int{-1,-1}, resultado: 1 },
{ entradas: []int{1,1}, resultado: 2 },
}
func TestSuma(t *testing.T) {
input1 := 2
input2 := 3
expected := 5
result := Suma(input1, input2)
if result != expected {
t.Errorf("TestSuma Result: %d, Expected: %d", result, expected)
}
}
func TestResta(t *testing.T) {
input1 := 2
input2 := 3
expected := -1
result := Resta(input1, input2)
if result != expected {
t.Errorf("TestResta Result: %d, Expected: %d", result, expected)
}
}
func TestMultiplicacion(t *testing.T) {
for _, parametros := range parametrosDePrueba {
resultado := Multiplicacion(parametros.entradas[0],parametros.entradas[1])
if resultado != parametros.resultado {
t.Error(
"Multiplicacion :", parametros.entradas[0], " X ", parametros.entradas[1], " = ", parametros.resultado,
"Resultado esperado: ", resultado,
)
}
}
}
// Benchmarks
func BenchmarkSuma(b *testing.B) {
for i := 0; i < b.N; i++ {
Suma(2, 3)
}
}
func BenchmarkResta(b *testing.B) {
for i := 0; i < b.N; i++ {
Resta(2, 3)
}
}
func BenchmarkMultiplicacion(b *testing.B) {
for i := 0; i < b.N; i++ {
Multiplicacion(2, 3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment