TDD & Benchmarking in GO

aditya goel
5 min readFeb 11, 2022

In this blog, we shall be looking at following concepts :-

  • Testing the functions written in GO.
  • Testing a function with multiple values.
  • Reading test-scenarios from Excel and then executing.
  • Benchmarking Applications written in GO.

Question:- How do we Test the functions written in Go ?

Step #1.) Let’s first create a file called as “sqrt.go” and define the name of package. We also import some errors package into the same :-

Step #2.) In this file, we shall declare two variables, to which we shall be using throughout our codebase :-

Step #3.) We also write a function to find the absolute value of the given number :-

Step #4.) We also write a write a function to compute the square-root of the given number :- We shall be testing this particular method going forward :-

Step #5.) Next, we create yet another file under same package and create following method into it :-

Step #6.) Now, we write a simple method named “TestSimple” and test it out as shown below :-

  • At line #11, we have pointer to “testing.T” being present into it.
  • At line #18, we shall assert the generated value with the static value.

Question:- How do we Test a given function with multiple values in one method itself ?

  • Let’s write a method “TestMany” to test the function with multiple values.
  • At line #30, we have created a slice of values, with which we shall be testing the function.

Question:- How do we execute a particular TestCase in GO ?


Question:- Demonstrate the process of reading the test-scenarios from the Excel file and then executing them for the afore-shown Square-Root Function.

Step #1.) Here is the CSV file, to which we are planning to read for the tes-cases :-

Step #2.) Since, we shall be reading the CSV file, we go ahead and import the package @ line #4, as shown in below snapshot :-

Step #3.) Let’s start writing our TestMethod to test-out various use-cases, as shown in the excel file :-

Step #4.) We write the Infinite Loop to read through all records in the Excel file. Below code-piece is self-explanatory :-

Step #5.) Now, let’s run the above test-cases, by running below command :-

go test sqrt_test.go sqrt.go -v

Question:- Whats the need of Benchmarking the code in GO ?

Answer:- When you write code, you sometimes want to benchmark it and make sure that you’re not making it slower when adding features or fixing bugs.

Question :- Is there a way to do benchmarking / profiling of functions written in GO ?

Answer:- We create a benchmark in a test file ending with _test.go. And we call it Benchmark and then the name of the benchmark. It gets one parameter, which is a pointer to testing.B.

  • So what we do in the benchmark is we iterate b.N times calling our function.
  • Here, we make sure there was no error and if there is, we’ll fatal the test, meaning stop it right here.

Question :- Whats the command to do Benchmarking in GO ?

Answer:- By default, go test, doesn’t executes benchmarks. So, we have to specify explicitly that, we wanted to run the tests :-

go test sqrt_test.go sqrt.go -v -bench . -run TTT
  • dot means all the benchmarks.
  • If you’re interested only in the benchmarks and not in the tests, you can specify -run with a name that does not match any other test. This way you’ll get only the benchmarks running and not the tests themself.

And we are running ~88 nanoseconds per operation.

Question :- Whats the main advantages of Profiling like this ?


  • Before optimizing a program, you need to profile it to see where it spends its time.
  • You can use your benchmark for profiling.

Question :- Can we know @ line level granularity that, how much time is being taken by each line to be executed ?

Answer:- Yes, that’s possible :-

  • We’ll do the benchmark and we say — Cpuprofile=prof.out.
  • And now that we have prof.out, we can use the pprof tool. So go tool pprof and then prof.out.
  • To see our function, we do list and Sqrt and we see that the run time of our function.
  • Here we can see every line and how much time it took flat and commulative.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-



aditya goel

Software Engineer for Big Data distributed systems