I'm pretty new to go testing coming from a background of PHP with PHPUnit tests.
In PHP it's pretty much reliously preached that you need 100% coverage. In Go most stuff i've read about tests seem to minimal, without stuff like provoking errors.
For example my small program:
func main() {
config = readConfig("config.json")
}
func readConfig(path string) Config {
var cfg Config
file, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(file, &cfg)
if err != nil {
log.Fatal(err)
}
return cfg
}
func TestCanReadConfig(t *testing.T) {
cfg := readConfig("test_data/config.json")
if cfg.Broker_pass != "test" || cfg.Broker_port != "3333" {
t.Error("invalid config")
}
}
now in my example I would have issues with coverage because main() isn't covered at all in Unit tests (How should it?)
And the 2 log.Fatal()'s are not covered at all.
My question is how do I write my tests exactly in go? Do I do it in a less strict style not testing every possible scenario or can I do annotation like in php with
@expectedException \InvalidArgumentException
Can I or should I test the main function? If not can I ignore it from the coverage tool somehow? Should I consider a testing framework?
Most testing tutorial are nice but very short and only introducing simple tests.