Introducing Code Coverage 0:00Let's talk about code coverage using a simple example. So I have this empty CommandNameTranslator class that I'm pulling in. We're not using namespacing or PSR here, so I just require it into the test class. So a CommandNameTranslator is a class that we built in one of the series. It's very simple. Its only job is to take a class name and translate that into an appropriate validator class name that we can then instantiate. So it's simple logic, but it gives us something to test. So if I switch back here, let's write that first test. So what does it do? Writing the First Test 0:31So if I switch back here, let's write that first test. So what does it do? Well, it translates a CommandName to its validator counterpart. That's essentially what it does. So now I need some kind of instance to work with, right? Let's pull that in. Protected CommandNameTranslator, and we can add that to the setup method. This CommandNameTranslator equals a new instance of CommandNameTranslator. So we can say this CommandNameTranslator to ValidatorName, and let's pass in some kind of command name.So we can say this CommandNameTranslator to ValidatorName, and let's pass in some kind of command name. How about TaskArchivedCommand? OK, so we want to translate that into the appropriate validator class. So if we save that to ValidatorName, we can do a simple assert equals to compare the results. I want that to be TaskArchivedValidator, and let's compare that against what was returned. OK, so I will right-click and choose Run Command Translator with Coverage this time. However, it couldn't get very far at all because we don't have this method name just yet. So let's do that now. Let's go to the main class and create that method. Implementing Translator Logic 1:40So let's do that now. Let's go to the main class and create that method. OK, let's rerun the tests. And just for some more real estate, let's close that. And we can see that for this class specifically, well, we do have code coverage in that we do have the method created. However, there's no logic just yet. Let's see why the test failed. Failed asserting that null matches and then our results. OK, let's take our first step at it.Failed asserting that null matches and then our results. OK, let's take our first step at it. So that will accept the command name, and we will simply return string replace, look for command, and replace that with validator using CommandName as your subject. OK, let's rerun it. And now, once again, it says we have 100% code coverage. But what if this is a little more complex? For example, for the demo, what if we had some kind of check here? Like, if CommandName equals foobar, then in that case, for our application, we need to do something a little bit differently. Interpreting Coverage Results 2:31Like, if CommandName equals foobar, then in that case, for our application, we need to do something a little bit differently. We will return baz just to simulate something. OK, well now, we don't have any coverage of this conditional here. So if I run it, you're going to see some red in the sidebar. Give that just a second, and there you go. So how does this work? Well, it's essentially reporting to you every line of code that it was able to hit, essentially. So when we ran our test, yes, it hit this line. But that returned false, so we never did anything within it.So when we ran our test, yes, it hit this line. But that returned false, so we never did anything within it. That's why that's marked as red. So it's sort of a way of telling you, hey, if you do want to test for this, and by the way, I'm not suggesting that you need 100% test coverage. However, if you do want that for a particular class, this is a nice way to figure out which tests you need to write. So notice that now, we only have 80% code coverage. So let's fix that. Let's go back, and now, we will write another test. Adding Tests for Branches 3:27So let's fix that. Let's go back, and now, we will write another test. And again, we have dummy code here, so I'm not sure how to phrase this test. How about just something like function, it returns baz if foobar is provided. A little lame, I know, but go with me. So let's just redo some of this code. We will provide foobar, and now, we expect that to equal baz. Okay, so if we rerun it, now, we can see that, yes, both tests have passed. We get green, and once again, we are back to 100% code coverage. Let's check it out. Customizing Coverage Colors 4:00We get green, and once again, we are back to 100% code coverage. Let's check it out. So now, notice that we have no red whatsoever. So that means every line of code in this class is covered under our tests. And mostly, that does it for this lesson, but one last thing. If you ever want to adjust the coloring for the code coverage, you want to go to colors and specifically general. So now, if I scroll down, the ones you want are full coverage, partial line coverage, and then finally, uncovered line. Those three will give you full control over whether you have a little stripe in the gutterpartial line coverage, and then finally, uncovered line. Those three will give you full control over whether you have a little stripe in the gutter or you highlight the entire background. It's up to you.