15, Unit test

Posted by cigardude on Thu, 03 Mar 2022 20:56:40 +0100

During development, it is necessary to confirm whether the results of a function and module are correct

func addUpper(n int)int{
   res:=0
   for i := 1; i <n ; i++ {
      res+=i
   }
   return res
}

Traditional method testing is to call this function to see whether the returned result is correct

Disadvantages:

  1. You need to call in the main function, modify the main function when testing, and run the project to stop testing.
  2. It is not conducive to management, because when we test multiple functions or modules, we have to write them in the main function, and the logic is chaotic
  3. The testing framework can solve the problem well

Basic introduction

The Go language comes with a lightweight testing framework testing and its own go test command to realize unit testing and performance testing. The testing framework can write test cases and stress test cases,

Unit test function:

  1. Make sure that each function is runnable and the result is correct
  2. Make sure that the performance of the written code is good,
  3. Unit testing can find the logical errors of program design or implementation in time, so as to expose the problems as soon as possible and facilitate the positioning and solution of the problems. The focus of performance testing is to find some problems in program design, so that the program can remain stable in the case of high concurrency

unit testing

cal.go

func AddUpper(n int)int{
   res:=0
   for i := 0; i < n; i++ {
      res += i
   }
   return res
}

cal_test.go

//Write test cases to test whether addUpper is normal
func TestAddUpper(t *testing.T) {
   res := AddUpper(10)
   if res != 55 {
      fmt.Println("AddUpper(10)Execution error, expected value=%v actual value=%v", 55, res)
   }

   //Output log
   t.Logf("AddUpper(10)Correct execution")
}
C:\Users\55480\go\go\learn\testcase01>go test -v 
# learngo/learn/testcase01 
.\cat_test.go:15:3: Println call has possible formatting directive %v 
FAIL    learngo/learn/testcase01 [build failed] 

# learngo/learn/testcase01 
.\cat_test.go:15:3: Println call has possible formatting directive %v 
FAIL    learngo/learn/testcase01 [build failed] 

C:\Users\55480\go\go\learn\testcase01>go test -v 
=== RUN   TestAddUpper 
AddUpper(10)Execution error, expected value=55 actual value=45    cat_test.go:18: AddUpper(10)Correct execution 
--- PASS: TestAddUpper (0.00s) 
PASS 
ok      learngo/learn/testcase01        0.080s 

The testing framework will_ test. Add the file of go and call the testXxx method

summary

  1. Test case file name must be_ test.go end
  2. Test case function must start with test (test + function name)
  3. Formal parameter must be (t * testing. T)
  4. In a test case file, there can be multiple test case functions, or in different files, as long as the name is unified
  5. Run test case instruction
    1. go test runs correctly without log, and the error log will be output
    2. go test -v the log will be output when the error is correct
  6. When an error occurs, you can use t.Fatalf to format the output error message and exit the program
  7. t. The logf method can output the corresponding log
  8. When testing a single file, be sure to bring the original file tested: C: \ users \ 55480 \ go \ learn \ testcase01 > go test - V cat_ test. go cal. go
  9. Test a single method: go test - V - test run TestAddUpper
  10. Scan all test cases in the current folder by default

Case:

package main

import (
   "encoding/json"
   "fmt"
   "io/ioutil"
)

type monster struct {
   Name string
   Age int
   Skill string
}
//Bind the monster method store. You can serialize a monster variable (object) and save it to a file
func (this *monster) Store()bool{
   //Serialize first
   data,err:=json.Marshal(this)
   if err!=nil{
      fmt.Println("marshal err=",err)
      return  false
   }

   //Save to file
   filePath:="c/monster.ser"
   err=ioutil.WriteFile(filePath,data,0777)
   if err!=nil{
      fmt.Println("write fiule err=",err)
      return false
   }
   return true
}

func (this *monster) ReStore()bool{
   //Deserialization
   filePath:="d/monster.ser"
   data,err:=ioutil.ReadFile(filePath)
   if err!=nil{
      fmt.Println("readFile ",err)
      return false
   }

   //Deserialization
   err= json.Unmarshal(data,this)
   if err!=nil{
      fmt.Println("Unmarshal err ",err)
      return false
   }
   return true
}

Test case

package main

import "testing"

func TestStore(t *testing.T){
   //First create a monster instance
   monster := &monster{
      Name:"clean",
      Age:12,
      Skill:"Wind fire wheel",
   }
   res := monster.Store()
   if res{
      t.Fatalf("Method error, expecting%v,Actually%v",true,res)
   }
   t.Logf("Test successful")
}

func TestReStore(t *testing.T){
   //First create a monster instance
   var monster= &monster{}
   res:=monster.ReStore()
   if res{
      t.Fatalf("Wrong method, hope for%v,Actually%v",true,res)
   }

   if monster.Name !="clean" {
      t.Fatalf("Method error, expecting%v,Actually%v","clean",res)

   }
   t.Logf("Test successful")
}

Topics: Go