mercredi 18 juillet 2018

How to implement unit test program using golang?

I saw many blogs where they write how to do unit testing but I will understand theory portion and not understand how to implement the test case can anyone will tell me that how will I implement the first test case for more understanding with the unit testing. Here I'm implementing the small program for average:-

Folder structure is:-

main.go
average(Folder)----> math_test.go

code in the both file is:-

main.go

 package main

 import "fmt"
 import "testcases/average"

 func main() {
    xs := []float64{1,2,3,4}
    avg := m.Average(xs)
    fmt.Println(avg) 
 }

math_test.go

package math

import "testing"

func TestAverage(t *testing.T) {
  var v float64
  v = Average([]float64{1,2})
  if v != 1.5 {
    t.Error("Expected 1.5, got ", v)
  }
 }

ERROR:- go build testcases/average: no non-test Go files in /home/iron/go/src/testcases/average

Helping me I'm very thankful to you.

Aucun commentaire:

Enregistrer un commentaire