1 00:00:00,090 --> 00:00:00,960 In this lecture. 2 00:00:00,960 --> 00:00:03,690 Let's talk about testing and why it's important. 3 00:00:03,719 --> 00:00:06,570 Testing can make or break your development career. 4 00:00:06,600 --> 00:00:12,540 There are a lot of successful developers who can create applications without ever writing a single test. 5 00:00:12,630 --> 00:00:18,540 Unfortunately, not being able to write tests can stagnate your skills as a developer. 6 00:00:18,570 --> 00:00:22,710 Writing tests can improve your ability to write code. 7 00:00:22,800 --> 00:00:27,130 Recruiters are likely to hire developers who have experience with testing. 8 00:00:27,150 --> 00:00:30,340 Beginner developers may be tempted to skip testing. 9 00:00:30,360 --> 00:00:32,420 After all, it can seem trivial. 10 00:00:32,430 --> 00:00:34,890 In the past, this statement was true. 11 00:00:34,920 --> 00:00:36,000 Where do you start? 12 00:00:36,030 --> 00:00:38,010 What tests should you write? 13 00:00:38,040 --> 00:00:39,810 What do you use for testing? 14 00:00:39,840 --> 00:00:45,170 Over the years, the frontend testing community has blossomed with tools and guides. 15 00:00:45,180 --> 00:00:50,450 In this section of the course, I want to guide you through the process of testing your code. 16 00:00:50,460 --> 00:00:57,600 We'll create a suite of automated tests to verify that our angular application is functioning as expected. 17 00:00:57,600 --> 00:01:01,430 Your goal as a developer is to ship a bug free application. 18 00:01:01,440 --> 00:01:05,489 Applications with bugs lead to a bad user experience. 19 00:01:05,489 --> 00:01:09,160 A bad user experience leads to users leaving your app. 20 00:01:09,180 --> 00:01:12,500 Testing can prevent this scenario from happening. 21 00:01:12,510 --> 00:01:14,730 Let's take a step back for a moment. 22 00:01:14,730 --> 00:01:15,930 What is testing? 23 00:01:15,960 --> 00:01:21,010 Testing is the process of checking that an application is functioning as expected. 24 00:01:21,030 --> 00:01:27,670 For example, if a user clicks the login button, the authentication component should open on the page. 25 00:01:27,690 --> 00:01:32,700 The interactions users have with our application will change its behavior. 26 00:01:32,730 --> 00:01:37,200 Testing will help us verify that the correct behavior always occurs. 27 00:01:37,410 --> 00:01:40,830 There are different techniques for testing an application. 28 00:01:40,830 --> 00:01:47,340 Primarily, they will fall into two categories manual testing and automated testing. 29 00:01:47,520 --> 00:01:52,090 From the beginning of the course, we've been manually testing the application. 30 00:01:52,110 --> 00:01:56,480 After writing code, we proceeded to test if it worked in the browser. 31 00:01:56,490 --> 00:02:00,920 Sometimes we even checked other parts of the app to make sure they worked. 32 00:02:00,930 --> 00:02:03,610 We would never write code and then claim it worked. 33 00:02:03,630 --> 00:02:06,810 We always tested new features manually. 34 00:02:06,810 --> 00:02:09,870 Testing an app is perfect for small applications. 35 00:02:09,870 --> 00:02:14,370 If it takes less than 3 minutes to test, you don't need to write tests. 36 00:02:14,370 --> 00:02:18,960 However, as your app scales, you may find the process cumbersome. 37 00:02:18,990 --> 00:02:21,360 Manual testing isn't efficient. 38 00:02:21,360 --> 00:02:22,320 We're human. 39 00:02:22,320 --> 00:02:28,200 We can easily become bored if we have to perform the same tasks repetitively as humans. 40 00:02:28,200 --> 00:02:33,960 When we're bored, we tend to lose focus that can lead to bugs bleeding through into production. 41 00:02:33,960 --> 00:02:36,570 Manual testing doesn't scale. 42 00:02:36,780 --> 00:02:39,870 This is where automated testing saves the day. 43 00:02:39,900 --> 00:02:44,460 Automated testing is when a program performs tests on your application. 44 00:02:44,460 --> 00:02:46,170 It's completely automated. 45 00:02:46,170 --> 00:02:49,160 It'll go through a list of tasks to execute. 46 00:02:49,170 --> 00:02:53,820 You can test functions, components, events and screenshots. 47 00:02:53,820 --> 00:02:58,690 If all tests pass, you can go ahead with pushing the app into production. 48 00:02:58,710 --> 00:03:04,800 Otherwise, it'll tell you where things went wrong, thus saving you the time of having to find where 49 00:03:04,800 --> 00:03:05,990 the bug is present. 50 00:03:06,000 --> 00:03:10,530 Best of all, machines can perform tests much faster than humans can. 51 00:03:10,560 --> 00:03:12,240 The advantages are clear. 52 00:03:12,270 --> 00:03:14,580 Automated tests are the way to go. 53 00:03:14,850 --> 00:03:19,380 In this section, we're going to focus solely on automated testing. 54 00:03:19,410 --> 00:03:23,880 Testing is a very broad term for what the testing world has to offer. 55 00:03:23,910 --> 00:03:30,270 If you do your own research, you'll come across dozens of definitions and tutorials for different types 56 00:03:30,270 --> 00:03:31,140 of testing. 57 00:03:31,260 --> 00:03:35,250 You may also come across libraries that perform tests differently. 58 00:03:35,550 --> 00:03:43,680 To make things easier, we can generally categorize tests into three groups unit tests, snapshot tests, 59 00:03:43,680 --> 00:03:46,000 and end to end tests. 60 00:03:46,020 --> 00:03:47,910 Let's discuss each of them. 61 00:03:48,270 --> 00:03:53,890 The first type of test you'll become familiar with are end to end tests and end to end. 62 00:03:53,910 --> 00:03:57,180 Test is performed by a program within the browser. 63 00:03:57,210 --> 00:04:03,180 This program will load the app in the browser, navigate the app and fill in fields. 64 00:04:03,210 --> 00:04:05,880 Tests are completely visualised. 65 00:04:05,910 --> 00:04:10,990 The purpose is to make sure things look like they're working from the user's perspective. 66 00:04:11,010 --> 00:04:16,740 At first glance, it may seem like end to end testing is the only type of test you will ever need to 67 00:04:16,740 --> 00:04:17,279 write. 68 00:04:17,310 --> 00:04:19,649 Unfortunately, that's not the case. 69 00:04:19,680 --> 00:04:25,590 End to end testing can cause a lot of issues trying to automate user behavior in a browser. 70 00:04:25,590 --> 00:04:27,060 It can be time consuming. 71 00:04:27,090 --> 00:04:30,570 Tests can take up to an hour if you write a lot of them. 72 00:04:30,600 --> 00:04:35,340 On top of that, if a test does fail, you may not know the reason why. 73 00:04:35,370 --> 00:04:39,540 You may know which test failed, but you don't know why it failed. 74 00:04:39,570 --> 00:04:44,150 You will have to retrace the steps of the test to understand why it failed. 75 00:04:44,160 --> 00:04:47,640 Sometimes it may not even be because of the code you've written. 76 00:04:47,640 --> 00:04:53,320 Your app could be working fine, but one of the APIs you're using could be down during a test. 77 00:04:53,340 --> 00:04:56,240 This downtime can cause a test to fail. 78 00:04:56,250 --> 00:04:59,580 One day a test will work and the next day it may not. 79 00:05:00,070 --> 00:05:05,710 Generally end to end tests are not the most reliable way to test an application. 80 00:05:05,740 --> 00:05:10,420 Most developers don't rely on this type of testing for their applications. 81 00:05:10,570 --> 00:05:17,110 This leads us to the next type of testing unit testing, instead of testing an entire page in the browser. 82 00:05:17,140 --> 00:05:23,620 You can run tests against small sections in your application, hence the word unit. 83 00:05:23,650 --> 00:05:28,570 Unit tests are reliable because they're fast and can tell you where a problem is. 84 00:05:28,600 --> 00:05:34,270 Unit tests can be defined for classes, components, services and pipes. 85 00:05:34,360 --> 00:05:36,580 Unit tests are much smaller. 86 00:05:36,610 --> 00:05:42,130 Basically, a unit test is a way to check bits and pieces of your code in isolation. 87 00:05:42,160 --> 00:05:45,910 The main advantage of writing unit tests is that they're fast. 88 00:05:46,150 --> 00:05:48,650 After making changes to your source code. 89 00:05:48,670 --> 00:05:52,720 You can immediately run a test to get feedback on your changes. 90 00:05:52,750 --> 00:05:56,920 Another benefit is helping developers better understand the code base. 91 00:05:56,950 --> 00:06:00,160 Let's say you're hired to work on an existing project. 92 00:06:00,160 --> 00:06:04,750 The first thing you want to do is to become familiar with the existing code base. 93 00:06:04,780 --> 00:06:08,380 A great way to learn about a project is by viewing the tests. 94 00:06:08,410 --> 00:06:12,400 Tests are meant to be descriptive and provide documentation. 95 00:06:12,430 --> 00:06:18,700 This is because the test checks the behavior of a class in a way that can help you figure out what a 96 00:06:18,700 --> 00:06:20,500 class is supposed to do. 97 00:06:20,530 --> 00:06:25,260 You can study a test to quickly learn what components do in the application. 98 00:06:25,270 --> 00:06:27,910 It'll help you get up to speed with things. 99 00:06:27,940 --> 00:06:33,340 Overall, unit testing is usually the type of testing most developers like to use. 100 00:06:33,370 --> 00:06:36,610 On the other hand, unit testing isn't perfect. 101 00:06:36,640 --> 00:06:39,790 There are two major issues you'll face with them. 102 00:06:39,820 --> 00:06:45,890 Firstly, code refactoring can become a nightmare if you want to restructure your application. 103 00:06:45,910 --> 00:06:48,420 You will also need to update your tests. 104 00:06:48,430 --> 00:06:53,600 Even something as simple as outsourcing logic into a service can be time consuming. 105 00:06:53,620 --> 00:06:59,440 Unfortunately, there isn't a way around this besides better planning and structuring your code. 106 00:06:59,740 --> 00:07:03,570 The second issue is testing small pieces of an application. 107 00:07:03,580 --> 00:07:06,230 It's like testing out the parts of a machine. 108 00:07:06,250 --> 00:07:08,110 The parts of a machine can work. 109 00:07:08,110 --> 00:07:10,150 But what if they can't work together? 110 00:07:10,180 --> 00:07:13,190 This can render our tests useless. 111 00:07:13,210 --> 00:07:16,780 There is a way to get around this, but I'll explain in a moment. 112 00:07:16,810 --> 00:07:21,600 Let's talk about the last type of testing called snapshot testing. 113 00:07:21,610 --> 00:07:24,010 You're probably familiar with the game spot. 114 00:07:24,010 --> 00:07:29,060 The difference, it's a game where you're presented with two nearly identical images. 115 00:07:29,080 --> 00:07:31,790 The objective is to spot the differences. 116 00:07:31,810 --> 00:07:34,370 That's what snapshot testing is. 117 00:07:34,390 --> 00:07:40,880 It's a program that will take two snapshots of your application and compare them if it spots any differences. 118 00:07:40,900 --> 00:07:43,740 It'll consider the test a failure snapshot. 119 00:07:43,750 --> 00:07:48,570 Testing, similar to the other types of tests comes with a set of challenges. 120 00:07:48,580 --> 00:07:53,610 The biggest challenge is being able to test on different browsers and operating systems. 121 00:07:53,620 --> 00:07:56,800 The smallest differences can cause a test to fail. 122 00:07:56,830 --> 00:08:00,680 There are ways around this which I'll discuss later in the section. 123 00:08:00,700 --> 00:08:05,130 We've identified the most common types of tests you'll encounter. 124 00:08:05,140 --> 00:08:07,630 The question is which do you use? 125 00:08:07,660 --> 00:08:09,530 The answer is all of them. 126 00:08:09,550 --> 00:08:15,670 If you were to search for the phrase pyramid of tests, you'll come across an image similar to what 127 00:08:15,670 --> 00:08:16,690 I'm showing you. 128 00:08:16,720 --> 00:08:20,700 Tests are not a concept exclusive to Angular or the front end. 129 00:08:20,710 --> 00:08:25,430 It's a concept that's used across all software, languages and platforms. 130 00:08:25,450 --> 00:08:31,270 The main consensus is the following A majority of your tests should be unit tests. 131 00:08:31,270 --> 00:08:40,059 About 60% is the recommended proportion for unit tests, 30% for snapshot tests, and lastly, 10% for 132 00:08:40,059 --> 00:08:41,480 end to end tests. 133 00:08:41,500 --> 00:08:45,460 Unit tests make up the majority because they're quick and straightforward. 134 00:08:45,490 --> 00:08:50,650 In contrast, end to end tests are slow and sometimes unreliable. 135 00:08:50,680 --> 00:08:53,370 Keep in mind that this is merely your recommendation. 136 00:08:53,380 --> 00:08:57,370 You may not always have a suite of tests that fit this proportion. 137 00:08:57,490 --> 00:09:01,150 There's one important question I want to answer before we move on. 138 00:09:01,150 --> 00:09:03,280 When should we test an application? 139 00:09:03,310 --> 00:09:07,320 The goal of testing your application is to make development faster. 140 00:09:07,330 --> 00:09:10,800 Bugs can impact how long it may take to build an app. 141 00:09:10,810 --> 00:09:17,290 If you carelessly write tests, you may end up crippling yourself by spending too much time on tests 142 00:09:17,290 --> 00:09:19,390 that aren't completely necessary. 143 00:09:19,630 --> 00:09:23,550 You don't have to test every single line of code with experience. 144 00:09:23,560 --> 00:09:26,810 You'll understand what you should and shouldn't test. 145 00:09:26,830 --> 00:09:32,110 There is one general rule of thumb I recommend for determining when you should write a test. 146 00:09:32,290 --> 00:09:36,100 The rule is if it doesn't save you time, don't bother writing it. 147 00:09:36,100 --> 00:09:38,120 At the very beginning of this video. 148 00:09:38,140 --> 00:09:42,340 I talked about how manual testing can take a while to execute. 149 00:09:42,370 --> 00:09:46,170 The larger your app is, the longer it'll take to test. 150 00:09:46,180 --> 00:09:52,520 The purpose of writing tests is to automate the process and help you catch errors in your application. 151 00:09:52,540 --> 00:09:58,240 If a test doesn't help you reduce your testing process, it likely isn't worth writing. 152 00:09:58,540 --> 00:09:59,530 There's one last. 153 00:09:59,640 --> 00:10:03,280 Question I often hear, which is What do we test? 154 00:10:03,300 --> 00:10:08,670 I talked about how the purpose of unit testing is to test bits and pieces of code. 155 00:10:08,760 --> 00:10:13,350 And you also mentioned how tests shouldn't focus on single lines of code. 156 00:10:13,380 --> 00:10:16,770 The question then becomes, what should we be testing? 157 00:10:17,010 --> 00:10:19,920 The best place to start is with components. 158 00:10:19,920 --> 00:10:23,850 Specifically, you should test the output of a component. 159 00:10:23,880 --> 00:10:28,690 If you think about it, that's generally what most of the code we write does. 160 00:10:28,710 --> 00:10:32,650 Every class or component will have an input and output. 161 00:10:32,670 --> 00:10:37,340 We're going to be writing tests where we'll pass in data to a component. 162 00:10:37,350 --> 00:10:40,440 The test will check the output of the component. 163 00:10:40,440 --> 00:10:44,340 If it outputs the desired results, it'll pass the test. 164 00:10:44,520 --> 00:10:50,100 The idea of input and output is an important concept to understand for tests. 165 00:10:50,130 --> 00:10:53,010 Testing is about input and output. 166 00:10:53,010 --> 00:10:59,290 In a test, you supply an input, receive an output and assert that the output is correct. 167 00:10:59,310 --> 00:11:05,190 You should write tests from the perspective of a developer who's using the component but is unaware 168 00:11:05,190 --> 00:11:08,550 of how the component functionality is implemented. 169 00:11:08,790 --> 00:11:10,800 That was a bit of an introduction. 170 00:11:10,800 --> 00:11:14,640 This lecture was a general overview of things from here on out. 171 00:11:14,670 --> 00:11:17,590 We'll start writing tests when you're ready. 172 00:11:17,610 --> 00:11:19,860 I'll see you in the next lecture.