Go unit tests

1. The disadvantage of the conventional method 

2. Unit Testing

CAL Package 

// is a test function 
FUNC addUpper (n-int) {int 
	RES: = 0 
	for I: =. 1; I <= n--. 1; I ++ { 
		RES = I + 
	} 
	return RES 
} 

// find the two numbers check 
FUNC GETSUB (int N1, N2 int) {int 
	return N1 - N2 
} 
CAL Package Penalty for 
Import ( 
	"fmt" 
	"testing" // introduced testing framework package go in 
) 

// give written test cases to test whether the correct addUpper 
FUNC TestAddUpper (t * testing.T) { 

	// call 
	res: = addUpper ( 10) 
	IF RES! = 55 { 
		//fmt.Printf("AddUpper(10) performing error, Expect =% v actual value =% v \ n-", 55, RES) 
		t.Fatalf (" AddUpper (10) performs error , Expect =% v actual value =% v \ n-", 55, RES) 
	} 

	// if correct, the output log 
	t.Logf (" AddUpper (10) executed correctly ... ") 

} 

FUNC TestHello (T * Testing. T) { 

	fmt.Println ( "TestHello is called ..") 

}
CAL Package Penalty for 
Import ( 
	_ "fmt" 
	"testing" // introduced testing framework package go in 
) 

// give written test cases to test whether the correct addUpper 
FUNC TestGetSub (t * testing.T) { 

	// call 
	res: = getSub (10,. 3) 
	IF RES! = {. 7 
		//fmt.Printf("AddUpper(10) performing error, Expect =% v actual value =% v \ n-", 55, RES) 
		t.Fatalf (" GETSUB (10 3) performing an error, the actual value of the expected value =% v =% v \ n-",. 7, RES) 
	} 

	// if correct, the output log 
	t.Logf (" getSub (10, 3 ) .. performed correctly !!!! . ") 

}

3. Summary

 4. schematic

 

Guess you like

Origin www.cnblogs.com/yzg-14/p/12235267.html
Recommended