Hey buddy what's going on this is Caleb with Debs Lopes dot com and I'm really really excited to teach
you about unit testing.
It's a super super important thing.
Most developers don't even do but they should definitely know about it and they should definitely do
it.
We're going to start by talking about what even is unit testing what's the point.
And I'm going to start with a little example.
So I want you to imagine that maybe Apple or Samsung decided to build some brand new phone but as they
built it they just kept adding in new features or components and apps without ever purposefully planning
it out or even ensuring that everything worked the way that it was expected to.
They would just you know throw it in the box ship it off to the consumer.
And as you can probably imagine you'd probably end up with a really crappy product.
It's likely though that like with their experience of releasing great products in the past that the
phone would have some pretty okay parts like maybe the camera would work super well maybe the speakers
sound nice but what if the experience of everything else was buggy or slow or broke or just plain didn't
work.
Apple and Samsung would never ever do this.
They'd never release a product without extensive testing.
This is why unit testing is so important for developers.
But what is it.
Why should I use it and why don't more developers use it.
Well unit testing at its most simplest is testing individual components or functionality in a piece
of software to ensure that it works the way that it's supposed to.
Simple as that a unit is the smallest testable piece of software.
It only has one or a few inputs and a single output or result usually.
Now for example I might write a unit test to ensure the function I wrote works the way that it's supposed
to my function could handle logging in a user for example.
Now my test essentially is going to create instances and mock objects of any data models networking
code etc. that are involved in the log in process at the end of my test.
I check to see that the expected result actually happens when I run my test it passes meaning that my
code is good and it's ready for review.
I can create a pull request.
It can get approved and then merged into our repository.
Now we're now getting into the why part of unit testing why bother.
Well imagine that my company brought on a new junior developer who's unfamiliar with our code base and
they get tasked with adding some new functionality or even refactoring certain parts of our project.
Maybe he or she goes into the code that I've written.
OK and starts messing around unknowingly breaking the log in functionality since they're new to our
team.
They forget to run tests and they submit a PR request with their changes.
It could happen.
Now since having all tests pass before PR is merged is a requirement at my company the senior devs pulled
down the project run the tests and see that the log in verification test fails.
Therefore they can reject the PR and say Hey JR dev fix this and then submit a new poll request.
That's why testing is super super powerful.
It saves a lot of stress and headaches from happening before they happen.
Now it's also helpful to write test for code that you have not even implemented yet.
This is called test driven development which will talk about a bit in future video but basically you're
going to write a test which simulates how your code should work when you run it.
It's going to fail because obviously you haven't actually written any of that implementation code.
Then you're going to write the code needed to make the test passed.
You might be really bad code at first but you're going to refactor it.
At least the test passes right and then at last you do end up refactoring it to make the test pass and
also make the code clean efficient and only what is simple and necessary.
Ok that's why it's awesome.
Not many devs use unit testing as often as they probably should though.
Ultimately unit testing is a difficult tedious time consuming process which is why a lot of devs choose
not to use it.
However it prevents a lot of careless mistakes bad code and also it makes collaborating with others
on a big project much much easier in the long run.
So this is a brief overview of unit testing.
What it is why you should care that should be obvious now and why a lot of devs don't utilize it.
By the way not a good example you definitely should be using unit tests because it makes things so much
easier and your code much much cleaner.
So now we're going to dive into this topic by talking about what makes a good unit tests.
Let's head over there right now.