{"id":17446939,"url":"https://github.com/arxeiss/go-false-positive-coverage","last_synced_at":"2025-03-28T03:41:11.653Z","repository":{"id":96809588,"uuid":"343120645","full_name":"arxeiss/go-false-positive-coverage","owner":"arxeiss","description":"How Code coverage in Go can give false positive results","archived":false,"fork":false,"pushed_at":"2021-03-09T10:12:05.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-01-30T02:04:35.408Z","etag":null,"topics":["coverage","golang","measure"],"latest_commit_sha":null,"homepage":"https://dev.to/arxeiss/false-positive-go-code-coverage-3k7j","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arxeiss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-02-28T14:08:47.000Z","updated_at":"2021-03-09T10:13:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"d8fc0ba9-ed47-4175-9362-6320b10c03ec","html_url":"https://github.com/arxeiss/go-false-positive-coverage","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"9a4d897a8787b40170783d6824f058991f8cf0e4"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxeiss%2Fgo-false-positive-coverage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxeiss%2Fgo-false-positive-coverage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxeiss%2Fgo-false-positive-coverage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arxeiss%2Fgo-false-positive-coverage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arxeiss","download_url":"https://codeload.github.com/arxeiss/go-false-positive-coverage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245966926,"owners_count":20701759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coverage","golang","measure"],"created_at":"2024-10-17T19:04:45.228Z","updated_at":"2025-03-28T03:41:11.633Z","avatar_url":"https://github.com/arxeiss.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go code coverage and False-Positive percentage result\n\nThe basic `go test` with cooperation with `go tool cover` can measure and show overall Code coverage of your code.\nHowever the result can be false positive and show **much higher percentage** of covered code that actually is.\n\n## ✍️ Readme more\n\nRead more why and how the code coverage is measured. I published the article in English on [Dev.to](https://dev.to/arxeiss/false-positive-go-code-coverage-3k7j) or in Czech on [kutac.cz](https://www.kutac.cz/pocitace-a-internety/falesne-pozitivni-code-coverage-v-go).\n\n## Try it yourself\n\nClone this repo and try all 3 cases:\n\n### Step 1 - tests only in `binary` package give 100%\n\n```bash\ngo test -covermode=count -coverprofile=coverage.out ./...\n# ?   \tgithub.com/arxeiss/go-false-positive-coverage\t[no test files]\n# ok  \tgithub.com/arxeiss/go-false-positive-coverage/binary\t0.002s\tcoverage: 100.0% of statements\n# ?   \tgithub.com/arxeiss/go-false-positive-coverage/unary\t[no test files]\n\ngo tool cover\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:3:\tAdd\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:7:\tSub\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:11:\tMul\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:15:\tDiv\t\t100.0%\n# total:\t\t\t\t\t\t\t\t\t(statements)\t100.0%\n```\n\n### Step 2 - With more tests the overall coverage goes down\n\n```bash\n# First \"activate\" tests in unary package by renaming .bak file\nmv unary/operations_test.go.bak unary/operations_test.go\n# Now run the tests\ngo test -covermode=count -coverprofile=coverage.out ./...\n# ?   \tgithub.com/arxeiss/go-false-positive-coverage\t[no test files]\n# ok  \tgithub.com/arxeiss/go-false-positive-coverage/binary\t0.002s\tcoverage: 100.0% of statements\n# ok  \tgithub.com/arxeiss/go-false-positive-coverage/unary\t0.002s\tcoverage: 33.3% of statements\n\n\ngo tool cover\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:3:\tAdd\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:7:\tSub\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:11:\tMul\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:15:\tDiv\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:5:\tAbs\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:9:\tSin\t\t0.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:13:\tCos\t\t0.0%\n# total:\t\t\t\t\t\t\t\t\t(statements)\t71.4%\n```\n\n### Step 3 - Measure \"real\" coverage with -coverpkg flag\n\n```bash\n# Now run the tests\ngo test -covermode=count -coverprofile=coverage.out -coverpkg=github.com/arxeiss/go-false-positive-coverage/... ./...\n# ?   \tgithub.com/arxeiss/go-false-positive-coverage\t[no test files]\n# ok  \tgithub.com/arxeiss/go-false-positive-coverage/binary\t0.002s\tcoverage: 44.4% of statements in github.com/arxeiss/go-false-positive-coverage/...\n# ok  \tgithub.com/arxeiss/go-false-positive-coverage/unary\t0.002s\tcoverage: 11.1% of statements in github.com/arxeiss/go-false-positive-coverage/...\n\n\ngo tool cover\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:3:\tAdd\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:7:\tSub\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:11:\tMul\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/binary/operations.go:15:\tDiv\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/main.go:10:\t\tmain\t\t0.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:5:\tAbs\t\t100.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:9:\tSin\t\t0.0%\n# github.com/arxeiss/go-false-positive-coverage/unary/operations.go:13:\tCos\t\t0.0%\n# total:\t\t\t\t\t\t\t\t\t(statements)\t55.6%\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farxeiss%2Fgo-false-positive-coverage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farxeiss%2Fgo-false-positive-coverage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farxeiss%2Fgo-false-positive-coverage/lists"}