Go 101 : Testing simple Go server

Super Simple

Image for post
Image for post
Photo by Roman Synkevych on Unsplash

Code we will be testing :

Testing web server in Golang

  • create a file with name <anything>_test.go, these files are ignore by compiler
  • write a func matching func TestXxx(*testing.T) where Xxx does not start with a lowercase letter. The function name serves to identify the test routine.
  • To run the test : go test

Writing Test‌

  • inorder to test the handler, we call it by passing http.ResponseWriter and *http.Request
  • to create a new Request
  • to record the response from the writer
  • ‌to verify

Full code main_test.go

I hope you like this article :

Follow me on

GitHub: https://github.com/dev117uday

LinkedIn : https://www.linkedin.com/in/uday-yadav-cs/

Sophomore && CSE Undergrad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store