This is a creation in Article, where the information may have evolved or changed.
Unit testing plays a very important role in program development.
1, can ensure the robustness of the program code, the smallest range of test program code, so as to ensure the correctness of the program!
2, you can quickly understand the current program through the unit test code.
I worked in the previous software companies, all the program code unit testing, generally after the unit test program quality is very good!! Now that I'm using Gogland to learn the go language and also want to use it to develop the Go Language program, how do I write unit tests for go programs in Gogland?
One, the go language is ready for us to test the framework!
Unit test file Execution requires unit test framework, go language is ready for us, go language comes with a lightweight test framework testing, you can use the "Go Test" command to implement unit testing and performance testing! Without any other installation or configuration, we can write the unit test program of GO Program!!
Second, where should the unit test program of the go language be placed?
I see a lot of articles saying that the Go language test program should be put into a unified directory, but it doesn't really need to be done. You can put the unit test file of the go language where you want to put it!!!
The General Unit test file program is written in parallel with the development of the program, after a function is finished, you should write the unit test file, and there is a point, a unit test file may contain multiple unit test methods, we usually do not fully execute the unit test file all the unit test methods. In my previous experience of writing unit test programs, it is usually a unit test method that executes a unit test file alone!! In such cases, it doesn't matter where the unit test files are stored, as long as the unit test files and unit test methods that need to be tested can be performed properly!!!
Third, write the unit test program in Gogland.
Before we formally write the unit test files, we should remember the rules for writing the Go Language Unit Test files!!
- The file name must be the end of _test.go so that it executes to the appropriate code when the go test is executed.
- You have to import testing this bag.
- All test case functions must start with test
- The tests are executed in the order in which they were written in the source code.
- The parameter of the test function testxxx () is testing. T, we can use this type to log errors or test status
- Test format: Func testxxx (t *testing. T), the xxx section can be any combination of alphanumeric, but the first letter cannot be a lowercase letter [a-z], for example testintdiv is the wrong function name.
- function, by calling testing. T's error, Errorf, Failnow, Fatal, Fatalif method, indicates that the test does not pass, and calls the log method to log the information of the test.
The following is the code of the test file and the unit test file:
Structfunc.go: This is the program file to be tested, I want to unit test the "Structfunc" function, this function is one of this program file!
Package mydata//Custom structure, the initial capital can be exported, including the first letter of the field inside the output type teststruct struct {Id Stringname string}//This is a structure function, return two values, Enter the method receiver, 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 that returns a struct, entered as the method receiver, func (TS *teststruct) STRUCTFUNC2 (Idin,namein string) teststruct{ts. Id=idin;ts. Name=namein;return *ts}//This is a normal function, the function name does not precede the method receiver Func ordinaryfunc (Input1,input2 string) (OutPut string) {temp:= Input1+input2return Temp}
Structfunc_test.go: This is the unit test program above, just testing the "structfunc" function.
The test method for package Mydataimport ("testing")//structfunc is func teststructfunc (t *testing. T) {//Declare the variable var (idin Stringnamein String) of the struct function input //Assign a value to the variable assigned to the struct function IdIn = "inputID" Namein = "inputname"//Get structure TS: = & teststruct{}//calls the struct function 1idOut, nameout: = ts. Structfunc (IdIn, Namein) if Idout==idin&&nameout==namein{t.log ("Test pass! ")}else{t.error (" Function execution Error ")}}
Four, Arbitrary unit test method is executed at random in Gogland.
Any unit test method in any unit test file should be able to be executed separately, in Gogland really can do so!!!
1. Open the Unit test program that you want to perform the test on, followed by the left mouse button to select the test method to perform the test.
2, right-click on the selected unit test method and then execute the corresponding command in the pop-up menu.
3, this is the execution of the unit test method, indicating that the current test method is completely correct, through the unit test it!
4, the other unit test method, the same with the right mouse button, in the pop-up menu to execute the corresponding command on it. Here is the unit test I made for a new unit test method.
5, we can store the unit file in any location, I copied the above unit test file to the "MyData" package for unit testing, as well as the unit test, the following is:
A bit of advice: Although we can put the unit test program anywhere, but for ease of maintenance, I would recommend to store in a fixed location, I put all my unit test program into the "Test" package!
Reference:
Https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/11.3.md
http://blog.csdn.net/samxx8/article/details/46894587