1
00:00:06,070 --> 00:00:11,580
Hey buddy what's going on this is Caleb with Debs Lopes dot com and I'm really really excited to teach

2
00:00:11,580 --> 00:00:13,320
you about unit testing.

3
00:00:13,350 --> 00:00:14,960
It's a super super important thing.

4
00:00:14,970 --> 00:00:20,280
Most developers don't even do but they should definitely know about it and they should definitely do

5
00:00:20,280 --> 00:00:20,760
it.

6
00:00:20,760 --> 00:00:25,140
We're going to start by talking about what even is unit testing what's the point.

7
00:00:25,140 --> 00:00:27,410
And I'm going to start with a little example.

8
00:00:27,450 --> 00:00:34,110
So I want you to imagine that maybe Apple or Samsung decided to build some brand new phone but as they

9
00:00:34,110 --> 00:00:39,930
built it they just kept adding in new features or components and apps without ever purposefully planning

10
00:00:39,930 --> 00:00:43,690
it out or even ensuring that everything worked the way that it was expected to.

11
00:00:43,920 --> 00:00:47,640
They would just you know throw it in the box ship it off to the consumer.

12
00:00:47,640 --> 00:00:52,030
And as you can probably imagine you'd probably end up with a really crappy product.

13
00:00:52,050 --> 00:00:56,760
It's likely though that like with their experience of releasing great products in the past that the

14
00:00:56,760 --> 00:01:01,950
phone would have some pretty okay parts like maybe the camera would work super well maybe the speakers

15
00:01:01,950 --> 00:01:08,460
sound nice but what if the experience of everything else was buggy or slow or broke or just plain didn't

16
00:01:08,460 --> 00:01:09,570
work.

17
00:01:09,570 --> 00:01:11,910
Apple and Samsung would never ever do this.

18
00:01:11,910 --> 00:01:15,410
They'd never release a product without extensive testing.

19
00:01:15,930 --> 00:01:20,030
This is why unit testing is so important for developers.

20
00:01:20,040 --> 00:01:21,450
But what is it.

21
00:01:21,480 --> 00:01:25,020
Why should I use it and why don't more developers use it.

22
00:01:25,020 --> 00:01:30,690
Well unit testing at its most simplest is testing individual components or functionality in a piece

23
00:01:30,690 --> 00:01:34,260
of software to ensure that it works the way that it's supposed to.

24
00:01:34,260 --> 00:01:39,590
Simple as that a unit is the smallest testable piece of software.

25
00:01:39,600 --> 00:01:45,350
It only has one or a few inputs and a single output or result usually.

26
00:01:45,390 --> 00:01:51,330
Now for example I might write a unit test to ensure the function I wrote works the way that it's supposed

27
00:01:51,330 --> 00:01:55,700
to my function could handle logging in a user for example.

28
00:01:55,740 --> 00:02:02,040
Now my test essentially is going to create instances and mock objects of any data models networking

29
00:02:02,040 --> 00:02:06,920
code etc. that are involved in the log in process at the end of my test.

30
00:02:06,960 --> 00:02:13,950
I check to see that the expected result actually happens when I run my test it passes meaning that my

31
00:02:13,950 --> 00:02:16,480
code is good and it's ready for review.

32
00:02:16,620 --> 00:02:18,080
I can create a pull request.

33
00:02:18,210 --> 00:02:21,860
It can get approved and then merged into our repository.

34
00:02:21,870 --> 00:02:25,920
Now we're now getting into the why part of unit testing why bother.

35
00:02:26,070 --> 00:02:33,060
Well imagine that my company brought on a new junior developer who's unfamiliar with our code base and

36
00:02:33,060 --> 00:02:38,760
they get tasked with adding some new functionality or even refactoring certain parts of our project.

37
00:02:39,090 --> 00:02:42,440
Maybe he or she goes into the code that I've written.

38
00:02:42,680 --> 00:02:48,570
OK and starts messing around unknowingly breaking the log in functionality since they're new to our

39
00:02:48,570 --> 00:02:49,040
team.

40
00:02:49,080 --> 00:02:52,900
They forget to run tests and they submit a PR request with their changes.

41
00:02:52,950 --> 00:02:54,030
It could happen.

42
00:02:54,240 --> 00:03:01,010
Now since having all tests pass before PR is merged is a requirement at my company the senior devs pulled

43
00:03:01,010 --> 00:03:05,710
down the project run the tests and see that the log in verification test fails.

44
00:03:05,790 --> 00:03:11,910
Therefore they can reject the PR and say Hey JR dev fix this and then submit a new poll request.

45
00:03:11,910 --> 00:03:14,840
That's why testing is super super powerful.

46
00:03:14,880 --> 00:03:18,900
It saves a lot of stress and headaches from happening before they happen.

47
00:03:18,900 --> 00:03:23,640
Now it's also helpful to write test for code that you have not even implemented yet.

48
00:03:23,670 --> 00:03:28,020
This is called test driven development which will talk about a bit in future video but basically you're

49
00:03:28,020 --> 00:03:32,450
going to write a test which simulates how your code should work when you run it.

50
00:03:32,490 --> 00:03:36,560
It's going to fail because obviously you haven't actually written any of that implementation code.

51
00:03:36,750 --> 00:03:40,500
Then you're going to write the code needed to make the test passed.

52
00:03:40,590 --> 00:03:43,460
You might be really bad code at first but you're going to refactor it.

53
00:03:43,500 --> 00:03:49,860
At least the test passes right and then at last you do end up refactoring it to make the test pass and

54
00:03:49,860 --> 00:03:55,050
also make the code clean efficient and only what is simple and necessary.

55
00:03:55,120 --> 00:03:56,700
Ok that's why it's awesome.

56
00:03:56,730 --> 00:04:00,070
Not many devs use unit testing as often as they probably should though.

57
00:04:00,240 --> 00:04:06,750
Ultimately unit testing is a difficult tedious time consuming process which is why a lot of devs choose

58
00:04:06,750 --> 00:04:08,030
not to use it.

59
00:04:08,070 --> 00:04:14,220
However it prevents a lot of careless mistakes bad code and also it makes collaborating with others

60
00:04:14,280 --> 00:04:17,190
on a big project much much easier in the long run.

61
00:04:17,200 --> 00:04:20,000
So this is a brief overview of unit testing.

62
00:04:20,010 --> 00:04:26,400
What it is why you should care that should be obvious now and why a lot of devs don't utilize it.

63
00:04:26,400 --> 00:04:31,680
By the way not a good example you definitely should be using unit tests because it makes things so much

64
00:04:31,710 --> 00:04:34,520
easier and your code much much cleaner.

65
00:04:34,560 --> 00:04:39,400
So now we're going to dive into this topic by talking about what makes a good unit tests.

66
00:04:39,510 --> 00:04:40,700
Let's head over there right now.
