{"id":5056,"date":"2017-06-03T20:08:12","date_gmt":"2017-06-03T20:08:12","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5056"},"modified":"2017-06-03T20:08:12","modified_gmt":"2017-06-03T20:08:12","slug":"unit-tests-in-go","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/unit-tests-in-go\/","title":{"rendered":"Unit tests in Go"},"content":{"rendered":"<p>In the previous post I mentioned that the Go SDK often has unit tests alongside the package code. So what do we need to do to write unit tests in Go.<\/p>\n<p>Let&#8217;s assume we have the package (from the previous post)<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage test\r\n\r\nfunc Echo(s string) string {\r\n\treturn s\r\n}\r\n<\/pre>\n<p>assuming the previous code is in file test.go and we then create a new file in the same package\/folder named test_test.go (I know the name&#8217;s not great).<\/p>\n<p>Let&#8217;s look at the code within this file<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage test_test\r\n\r\nimport &quot;testing&quot;\r\nimport &quot;Test1\/test&quot;\r\n\r\nfunc TestEcho(t *testing.T) {\r\n\texpected := &quot;Hello&quot;\r\n\tactual := test.Echo(&quot;Hello&quot;)\r\n\r\n\tif actual != expected {\r\n\t\tt.Error(&quot;Test failed&quot;)\r\n\t}\r\n}\r\n<\/pre>\n<p>So Go&#8217;s unit testing functionality comes in the package &#8220;testing&#8221; and our tests must start with the word Test and takes a pointer to type T. T gives us the methods to create failures etc.<\/p>\n<p>In Gogland you can select the test_test.go file, right mouse click and you&#8217;ll see the Run, Debug and Run with coverage options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I mentioned that the Go SDK often has unit tests alongside the package code. So what do we need to do to write unit tests in Go. Let&#8217;s assume we have the package (from the previous post) package test func Echo(s string) string { return s } assuming the previous code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[178,28],"tags":[],"class_list":["post-5056","post","type-post","status-publish","format-standard","hentry","category-go","category-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5056"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5056\/revisions"}],"predecessor-version":[{"id":5075,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5056\/revisions\/5075"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}