Golang Code Coverage

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

When you write automation, you have to count code coverage = =

Original link: https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests

In fact, I saw the original link I did not succeed in statistics, or to see the notes of colleagues to succeed.

Action steps--written in the steps I've actually done

1 Creating the test file for the main function

2 compiling the code, generating the executable file

3 Statistical coverage

A test file for the main function

Package Main//This file is mandatory as otherwise the Filebeat.test binary was not generated correctly.Import ("Testing"  "Flag")varSystemtest *BOOLfunc init () {systemtest= Flag. Bool ("systemtest",false,"Set to True when running system tests")}//Test started when the test binary is started. Only calls Main.Func Testsystem (T *testing. T) {if*systemtest {main ()}}

Attention:

1 Name: Take a look at the main () function of your code the go file name, directly named *_test.go file; For example, the code file name Main_server.go, can be directly named Main_server_test.go

2 contents: Placed in the same directory as the file under test @1

Two compiled code

1 View the main function, which will be the OS. Exit () Change to return (according to the actual situation Return/return 0/return 255 ...)

2 Original Compile command for GO build

Here you use the command go test-c-covermode=count-ldflags "-X main._version_= $VERSION. ${reversion}"-coverpkg./gopath/src/mvdsp/ module/,./gopath/src/mvdsp/extractor/,./gopath/src/mvdsp/mvutil/,./gopath/src/mvdsp/protocol/-O dsp_server.test

①-c indicates that a test binary is generated

②-covermode=count indicates that the generated binary contains coverage count information

③-ldflags is used to write version information to a binary file without the use of an additional versions file

④-coverpkg behind is to statistical coverage of the file source code

⑤-o is followed by the output binary file name

⑥ ... There may be more parameters available, and I don't know.

3 Execute command, generate an executable binary file, copy to the deployment directory

4 Start the service, add parameters after starting the command:-systemtest-test.coverprofile Coverage/coverage.cov

①-systemtest is used to start the main test that I said earlier

②-test.coverprofile used to specify which file the coverage information is written to

Three statistical coverage

1 Performing automation

2 Execute the following command to generate the coverage file Coverage.cov

Pid_server= ' Ps-ef | grep "My_server-systemtest" | Grep-v "grep" | awk ' {print $} ' kill $pid _server

The condition of coverage is that the program needs to exit/end normally, so when the automation is complete, we need to send a message to the program to indicate the end before we can get the coverage file.

For this: Go is required to return, that is, you can kill Server_pid (if the service starts with supervisor, it will pull itself up without affecting subsequent calls)

Do I need to exit C + +? , send P __gcov_flush (), see http://www.cnblogs.com/zhaoxd07/p/5608177.html

Python needs to trigger the Atexit module to register a callback function that can be used with Ctrl + C or Kill-2

3 Generating test reports

Go tool Cover-html=./coverage/coverage.cov-o/data/reports/coverage.html./coverage/gocover-cobertura < Coverage.cov >/data/reports/coverage.xml

You need to install a widget when generating an XML report: Go get Github.com/t-yuki/gocover-cobertura

Use the Gocover-cobertura under the bin in a directory that is easy to execute

4 integrating test reports into Jenkins, you are done.

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.