I have a go function called setupConfig() and I have a Test_setupconfig that tests it and tests it just fine. But when I do a cover test on it and look at the HTML report, it shows my handling of some error returns by the Viper package is not covered. Why is this not covered and how do I handle this?
Go test coverage, if statement not covered
412 Views Asked by Dan Sherwin At
1
There are 1 best solutions below
Related Questions in GO
- Go Fiber and HTMX - HX-Trigger header is changed to Hx-Trigger, which is not what HTMX is listening for
- Golang == Error: OCI runtime create failed: unable to start container process: exec: "./bin": stat ./bin: no such file or directory: unknown
- Handling both JSON and form values in POST request body with unknown values in Golang
- invalid transaction: Transaction failed to sanitize accounts offsets correctly
- Golang lambda upload image into s3 static website
- Is there a way to get a list of selected module versions, but only for modules within the pruned graph?
- Save Interface in DB golang
- ERROR: column "country" is of type text[] but expression is of type record (SQLSTATE 42804)
- Trying to update the version.go file with the release tag from GitHub actions but its failing
- How can I optimize this transposition table for connect 4 AI?
- const declaration - How to evaluate expressions at compile time?
- How add array of authors for unique user in database in Goland IDE?
- Why is the main goroutine not blocked after write in unbuffered channel?
- Insert & Retrieve from a channel in same main function throws "goroutine 1 [chan receive]: main.main() /path exit status 2" error
- Gob error when decoding array of structs: decoding into local type but received remote type
Related Questions in GO-TESTING
- In `go test`, is it possible to pass custom flags?
- golang.org/x/oauth2 oauth2.Config.Endpoint.TokenURL mock: missing access_token
- Go Test not working with application that requires build tags
- How do I prevent testify command line args from leaking into my Cobra application?
- Why gomonkey.ApplyFunc mocked func in for loop share the same address?
- Go test coverage, if statement not covered
- How to avoid timeout in go test after 10m
- Issue with executing tests for main function in Go
- Form variables not available in testing
- How to create Unit test cases for “GetListOfUsers()” function of “UserService.go“ file. Facing issue in creating dummy aerospike.Recordset object
- Is the go test coverage.out file able to show how many tests were run?
- How to correctly cover all golang packages with all tests?
- hex-arc golang - overcome import cycling error when testing
- How to run `go test` in the parent directory of multiple go modules and return non-zero in case of error
- Go test "-run -" flag executed tests much faster
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?

The coverage report tells you how much of your code is executed during tests. What you're seeing is the
returnstatements within thoseifblocks are not tested, which means you don't have any unit tests that are designed to fail and return errors. While testing that you code works when given correct input, it's also important to ensure that tests fail correctly and safely when given bad input.However, this is a weird situation because those errors aren't within your own package, those errors are coming from the
viperpackage. at this point it's important to ask yourself the question "what am I really testing?". If you're using theviperpackage then the assumption is that package is thoroughly tested, and by creating your own tests for the viper errors you're just doubling up on those tests with no real improvements. For this reason, sometimes we choose to omit testing these branches because realistically, if the viper package errors - and assuming all your inputs are static and hard coded like shown - then that's not a problem with your code that's a problem with theviperlibrary.If you really want to get 100% coverage and test all decision trees, the only way to do so is to put the
viperpackage behind some kind of abstraction. Most likely an interface that's passed into the function allowing for multiple implementations whether you're running in production or running in tests.With that said, hard coding all your values within the function like that isn't recommended practice. Ideally you'd want the values of your config struct coming from a local config file, environment variables, commandline flags, or a combination of them. By doing this, you could have this setup function accept an interface that it uses to retrieve the configuration, making this function easily testable as all you'd need to do is mock out the implementation of that interface in your tests. So it would be something like the following:
config.go:config_test.go:By doing this and giving your
setupConfig()function accepting an interface, it means you can give it a function implementation when it's running in production, but also mock it out with hard-coded test data when running tests. This is quite often used when interacting with other services too, like databases. Instead of having to spin up a database and connect to it when running tests, you can have your code accept an interface that tells it how to interact with the database and mock it out in tests. This allows you to isolate parts of your code and only test exactly what you're wanting to.