4

I have a go webservices (a REST Api) for which we have unit test, and for which go cover works fine.

Now we have a test suite written in python that launch a instance of the server, run the test , stop the server.

I would like to know if there's some tools that would permit me to run my server binary with a specific flag , so that at the end it prints coverage of the tests executed by my "blackbox" testing ?

Thanks.

3 Answers 3

5

Based on this post here's what I did:

  1. created a main_test.go with this content:

    package main
    
    // code based on technique explained here:
    // https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests
    // you can look there if you want to see how not to execute this test
    // when running unit test etc.
    
    // This file is mandatory as otherwise the packetbeat.test binary is not generated correctly.
    
    import (
        "testing"
    )
    
    // Test started when the test binary is started. Only calls main.
    func TestSystem(t *testing.T) {
        main()
    }
    
  2. as it was a web services (and hence in an infinite loop), I needed a way to gracefully exit on SIGTERM (without it being considered a failure), so I used the package go get gopkg.in/tylerb/graceful.v1 and replaced (I use go-restful) in main.go the line

        -       log.Fatal(http.ListenAndServe(":"+port, nil))
        +       graceful.Run(":"+port, 10*time.Second, nil)
    
  3. then I would run the test like this

    • go test -c -covermode=count -coverpkg ./... -o foo.test
    • ./foo.test -test.coverprofile coverage.cov & echo $! > /tmp/test.pid
    • run my test suite
    • kill "$(cat /tmp/test.pid)"
Sign up to request clarification or add additional context in comments.

Comments

0

You probably don't want to do that. Running code with coverage, race detection and/or other tools increases the binary size and makes it much slower. Running both the race detector and code coverage is 25 times slower on my computer.

Simply use go test -cover -race for testing, and go build when deploying. This will give you the output you want, albeit not in exactly the way you wanted it.

1 Comment

the fact it is slow / bigger is not at all a problem as said, it's just the time the automated functionnal test are run, the integration tests / end to end are end are run with a normal build, and go test only run the test in go, not the binary itself right?
0

go test -c -cover solution has some disadvantages like the service under tests must be stopped when to generate the code coverage profile. and it would also inject some unnecessary flags into the covered binary like "-test.v", which may break the service's original starting way.

We use goc instead, it help us collect code coverage of system tests (API tests or e2e tests) at runtime easily, i think it's more elegant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.