We are now at the seventh video of this section testing and.
D d That is test driven development in the previous video we looked at pointers and structures in this
video.
We will learn about the testing package and see what is TDD go has a powerful testing package that allows
you also to work in a TDD environment quite easily.
It is also very convenient to check the portions of your code without the need to write an entire main
application that uses it.
Now let's have a look at the testing package.
So testing is very important in every programming language.
Go creators knew it and decided to provide all libraries and packages needed for the test in the core
package.
You don't need any third party library for testing or code coverage the package that allows for testing
go apps is called conveniently testing.
We will create a small app that sums two numbers that we provide through the command line with our main
dot go.
We have added these lines of code that computes the sum of two numbers.
Let us execute our program in the terminal to get the some go run main dot go and we give the two numbers
as 3 and four.
So as you can see we got the output as the sum of three and or is seven.
By the way we are using the SDR conf package to convert strings to other types.
In this case to end the method 80 0 I received a string and returns an int and an error that for simplicity
we are ignoring here by using the underscore.
You can ignore variable returns by using the underscores if necessary but usually you don't want to
ignore errors.
Okay so let's write a test that checks the correct result of the sum we have created a new file called
Main underscore test dot go by convention test files are named like the files they're testing plus the
underscore test suffix within this file.
Add these lines of code.
Make sure that the package used in both is the same.
I have used the main package as you can see testing in Go is used by writing methods started with the
prefix test a test name and the injection of the testing dot t pointer called T contrary to other languages.
There are no asserts no special syntax for testing in go you can use go syntax to check for errors and
you call T with information about the error in case it fails.
If the code reaches the end of the test function without arising errors the function has passed the
tests to run a test and go.
You must use the go test hyphen v command hyphen V is to receive verbose output from the test keyword.
Let me show you.
Open a terminal and write.
Go test hyphen V.
So you can see the output.
Our tests were correct.
Let's see what happens if we break things on purpose and we change the expected value in main underscore.
Test dot go from eleven to ten.
Let's save this file and open the terminal.
Execute the command.
Go test as you can see over here.
The test has failed as we expected the testing package provides the information you set on the test.
Let's make it work again and check.
Test coverage change the value of the variable expected from 10 to 11 again and run the command.
Go test iPhone cover to see the code.
Coverage the hyphen cover option gives us information about the code coverage for a given package.
Unfortunately it doesn't provide information about overall application coverage before we move further
let us see what TDD is TDD is the acronym for test driven development.
It consists of writing the tests first before writing the function.
The TDD changes the way to write code and structure code so that it can be tested.
So how does it work.
Let's explain this with a real life example.
Imagine that you are in summer and you want to be refreshed somehow.
You can build a pool it with cold water and jump into it.
But in TDD terms the steps will be as listed here.
First you jump into a place where the pool will be built.
That is you write a test that you know it will fail next.
It hurts and you aren't cool either.
Which means yes the test failed as we predicted.
After this you build a pool and fill it with cold water.
So you code the functionality and then you jump into the pool.
Or rather you repeat the point one test again.
Following this your code now also objects completed which means test passed.
Finally go to the fridge and take a beer to the pool drink.
Double also nurse or we could say refactor the code so let's repeat the previous example.
But with a multiplication First we will write the Declaration of the function that we're going to test.
Let's save this file.
Now let's write the test that will check the correctness of the previous function.
And now we test it through the command line.
So first save this file and open the command line terminal here.
Execute the command.
Go test nice like in our pool example where the water wasn't there yet.
Our function returns an incorrect value too.
So now we have a function declaration but isn't defined yet and the test that fails.
Let's go back to main dot go file.
Let's add these lines of code.
Now we have to make the test pass by writing the function and executing the test to check.
Let's go back to the terminal and we execute again our testing suite.
After writing our code correctly the test should pass so we can continue to the refactoring process.
Great.
We have developed the multiply function following TDD.
Now we must refactor our code but we cannot make it more simple or readable.
So the loop can be considered closed during this course we will write many tests that define the functionality
that we want to achieve in our patterns TDD promotes encapsulation and abstraction just like design
patterns to in this video.
We have learned about testing and test driven development or TDD in the next video.
We will dive into the libraries available in go.