Testing in GO - code coverage within project packages -


i have question generating code coverage in go(lang) projects.i have simple structure:

root/     config/     handlers/     lib/     models/     router/     main.go 

config contains configuration in json , 1 simple config.go reads , parses json file , fills config struct used when initializing db connection. handlers contains controllers (i.e. handlers of respective method+url described in router/routes.go). lib contains db, request responder , logger logic. models contains structs , funcs mapped from-to json , db. router contains router , routes definition.

basically testing 1 single handler ensure config, logger, db, responder, router , corresponding model invoked (and tested somehow well).

now if php or java or don't know else language, having , running single handler test create code coverage other invoked parts of code if in different folders (i.e. packages here). unfortunately, not case in go.

and there little code in of lib files having 1 method (like initdb() or readconfig() or newrouter() or logger()) able have code coverage them have create stupid tests in these packages while invoked , tested testing main urls handling packages.

is there way how code coverage packages included other packages within 1 project?

you can import packages within single test , write tests them based on how they'll used within application. example, can have 1 test, main_test.go imports other packages, , write tests methods in imported packages.
(in main_test.go):

package main  import (     "testing"     "lib"     "models"     "handlers"     // etc )  // testing code here 

however, in personal opinion, might better make sure each package should , only. best way that, testing individual package itself, own test suite.


Comments