Go Program Unit test case preparation and Gogland running unit test method

Source: Internet
Author: User
Tags alphanumeric characters

One, the go language is ready for us to test the framework.

The execution of unit test files requires a unit test framework, and the go language is ready for us, with a lightweight test framework testing in the went, you can use the "Go Test" command to implement unit and performance testing. Without any other installation or configuration, we can write unit test procedures for the GO program ...

Second, the Go Language Unit test program should put where.

I've seen a lot of articles saying that the Go language test program should be in a unified directory, but it doesn't actually have to be. You can put the Go Language Unit test file in any place you want to put ...

The General Unit test file program is written synchronously with the development of the program, once a feature is finished, you should write the unit test file, and there is a point where a unit test file may contain multiple unit test methods, and we usually don't execute all the unit test methods in this unit test file. In my previous experience writing unit test programs, it is usually a unit test method that executes a unit test file individually. In such cases, the unit test files are stored where it does not matter, as long as the normal implementation of the need to test the unit Test files and unit test methods can be ...

Third, write the unit test program in Gogland.

Before we formally write unit test files, we should remember to write rules for these go Language Unit test files. The filename must be _test.go, so that you execute the GO test before executing to the appropriate code. You must import testing this package all the test case functions must be test start tests are performed using the test function in the order in which they are written in the source code testxxx ( The parameter is testing. T, we can use that type to record the error or the Test status Test format: Func testxxx (t *testing). T), the xxx section can be any combination of alphanumeric characters, but the first letter cannot be a lowercase letter [a-z], for example, Testintdiv is the wrong function name. function by calling the testing. The error, Errorf, Failnow, Fatal, Fatalif method of T, which indicates that the test is not passed, calls the log method to record the test information.

Here is the test file and unit test file code:

Structfunc.go: This is the program file to be tested, and I want to unit test the "Structfunc" function, which is one of the program files.

Package MyData

//Custom structure, the first letter of uppercase can be exported, including the first letter of the inside field is output
type teststruct struct {
	Id string
	Name string
}

This is a struct function that returns two values and is entered as a method recipient
func (ts *teststruct) Structfunc (Idin,namein string) (Idout,nameout string) {

	ts. Id=idin;
	Ts. Name=namein;

	Return TS. Id,ts. Name
}

//This is another struct function, return structure, input as method receiver
func (ts *teststruct) STRUCTFUNC2 (Idin,namein string) teststruct{

	ts. Id=idin;
	Ts. Name=namein;

	Return *ts
}

//This is a normal function with the function name not preceded by a method recipient
func  ordinaryfunc (Input1,input2 string) (OutPut string) {

	temp:=input1+input2 return

	temp
}

Structfunc_test.go: This is the unit test program above, just test the "structfunc" function.

Package MyData

Import (
	"testing"
)

//structfunc test Method
func teststructfunc (t *testing. T) {

	///DECLARE variable
	var (
		IdIn String Namein String) entered by the struct function

	to assign values to the variable entered by the struct function
	IdIn = " inputID "
	Namein =" InputName "

	//Get structure
	ts: = &teststruct{}

	//call struct function 1
	idout, nameout: = ts. Structfunc (IdIn, Namein)

	if idout==idin&&nameout==namein{
		t.Log ("test passed.") ")
	}else{
		t.error (" Function execution error ")
	}

}

Arbitrarily execute any unit test method in Gogland.

Any unit test method in any unit test file should be able to be executed separately, and in Gogland you can do so ...

1, open the Unit test program for which you want to perform the test, followed by the left mouse button to select the test method to perform the test.

2, in the selected unit test method above the right mouse button, and then execute the corresponding command in the pop-up menu.

3, this is the screenshot after the unit test method, which indicates that the current test method is completely correct and passed the unit test.

4, the other unit test methods, the same with the right mouse button, in the pop-up menu to execute the corresponding command on it. Here's a screenshot of my unit test for a new unit test method.

5, we can store the unit file to any location, I copied the unit test files above to the "MyData" package for unit testing, as well as the unit test, the following is a screenshot:

A suggestion: Although we can put the unit test program anywhere, but for the convenience of maintenance, I recommend that the store to a fixed location, I put all my unit test procedures are stored in the "test" package.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.