Test driven development (TDD) - Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I once told our intern "No, TDD is tuna-driven-development" which means you go buy me a sandwich every day at lunchtime"

(nah, he didn't fall for it)

👍︎︎ 4 👤︎︎ u/rfinger1337 📅︎︎ Sep 05 2019 🗫︎ replies

Good job, quality youtube tutorials. Thumbs up! :)

👍︎︎ 2 👤︎︎ u/Maximcr 📅︎︎ Sep 05 2019 🗫︎ replies
Captions
hello everybody I'm Nick in this video I'm going to talk to you about TDD also known as test-driven development we're going to talk about what it is why we want to use TDD and how we can use TDD I'm going to show you an example explaining exactly what it is how it works and what steps you need to follow first let's just answer a few questions what is TDD well test-driven development is a development life cycle in which you write your tests before you actually write your implementation first you have the read mode and this read mode says write a failing test this test it's written in a way that says what your code should be doing so what a piece of behavior or a piece of software or a unit in your code should be doing the next you make this test pass and you write the minimum amount of code to make this red test up green test the next thing you need to do is the blue state and the blue state is a refactor State during factoring you would be modifying the code you wrote at the green step to make your test pass and you're gonna make it a bit nicer this is of course only when applicable and then we go back to the failed test and we write a new test and then we make this new test pass again and the cycle goes on and on and on until our software is complete doing this we achieve several things the major thing you achieve is your a decayed fear of change TDD eradicate sphere of change because these tests are always true they are your source of truth and no matter what happens to your code if the business logic is still sound these should all pass if one of them fails then something happened there you accidentally had a knock-on effect to another piece of the software and you go back and change it this makes refactoring way easier because you can just go to your code change it and if it test fails you know what firmly you know what you broke this might look like something that makes you write more code to achieve the same thing but in the long run is way more beneficial and we will see how you can get into it and how simple it is using a cutter as an example a code cutter is an exercise in programming which helped programmers exercise the skills through practice and repetition there are many cutters but I'm gonna go with a more straightforward one and this is a string calculator cutter in here as you can see on your screen right now I have a solution in c-sharp by the way this example is applicable to any programming language the only thing that changes is the code that you're gonna write and how you're actually going to write your tests but if you're writing JavaScript or Java Kotlin or C++ or anything the approach and the thought process is the same so if you don't write the same language as me don't worry this and guarantee you there's a framework to write unit tests in this very same way and you can use it to achieve the same thing as what you're gonna see now the first thing I want to do is I want to create a text file and I'm gonna name that string calculator dot txt and in here I'm gonna copy my kata step-by-step the string calculator kata is a very simple kata and the first step says the following we're going to create a simple calculator that accepts a string which the string is represented by numbers and a comma separator and we separate on the comma and we just add the two numbers so empty string return 0 1 returns 1 1 coma 2 returns 3 and so on and so forth this design will drive our TDD approach so what I'm going to do now is I'm going to create a new project in my solution and this will be a unit test project using X unit in your scenario it might be any unit in might be different language but please stick with us to understand the approach I'm going to name this string calculator dot test so creating that project now and what I need to do is delete this unit test default class and add reference to my string calculate project now the requirements are simple I need to have something that accepts a comma-separated on a non comma-separated number containing string and does a month the first thing I'm going to do is I'm going to create a class called calculator tests as you can see I did not create any calculator class to start adding logic I start from my tests in here I have configured my IDE to run unit tests every time I save my document so as I'll be developing I will be getting instant feedback of my unit tests I want to add another package here because I'm gonna need it for our sessions and this package is called fluent assertions I will explain what these does as I'm writing the test now with the package added I'm ready to write my first test case so in c-sharp I will need to create a theory and the theory has some inline data which I'm gonna populate in a bit but first I'm gonna write the body so it's a public void and I'll be calling that method add when I create it and all I'm trying to say is let's just read what the document says except string empty one value or a comma separated set of numbers and add them so add adds up to two numbers because it does specify that this is for up to two numbers separated by commas and return their sums so add returns after two numbers when string is valid and we're only testing for the degeneracy of other valid scenarios first I want to break down my test in three sections and these sections will be arranged act and assert this is not necessarily part of TDD but it gives us a clear definition of what each step is doing and the first thing I want to do is I'm gonna have a system on the test I'm gonna arrange my calculator class so I'm gonna say new calculator class that does not exist yet then what I'm gonna do is I'm gonna say result equals to system on the test dot ad because there will be an ADD method here and last but not least I'm gonna say resolve dot should this is coming from fluent assertions let me just make this poll be and there should be some expected value that I'll be comparing it against and I'm using inline data because I want to test different test cases with the same test body so what I'm gonna say is here's a spring called calculation and let me make this smaller and this is the string we will be testing against and int expected is the text I'm expecting so I misspelled that here sorry I meant to say this is the number I'm expecting so the sum of these numbers should be adding up to this expected an example would be string empty should return 0 1 should return 1 and 1 coma 2 should return 3 this is according to the requirements in this step now that we have this we clearly don't have the calculator class so we cannot actually run our test so we know we need a calculator class because I will design the system based on the test so I'm gonna go to the string calculator project and I'm going to say create a class called calculator this is now created and you can see that now the error is in the add method I'm going to be copying the calculation and adding it here and now I can just say to resharper which is the IBM using create method calculator add and this method returns an integer and this is the calculation we're gonna use the word numbers because I think that's what the tests that we do yeah it's correct numbers and now I'm gonna implement the method I'm going to split this string so split numbers are the numbers split on a comma and now what I should say is that if split numbers don't have anything split here then I'm gonna return 0 if split numbers dot length equals 1 which means we have one number or nothing in here then I'm gonna check if split numbers 0 dot length equals 0 which means that the value is this one string empty then I'm going to return 0 we're currently in the writing the code to pass the test values if that's not the case then return int dot pause and we are going to pass this single number and return it and last but not least we're gonna say return and we are going to return an addition of the two split strings so it looks like this code should satisfy the needs for these tests now we need to import something here but once we've done this we are able to run out as for the first time and after that they should be automatically running every time I save if I just turn out to saving on so my testify meaning I don't satisfy the criteria and the problem is that the index is outside of the boundaries we can see why as you can see here is because of a typo which I made accidentally oh you might have made accidentally while developing it and now that I can fix it I can say that my code we in fact be passing see we just got an issue just for pure carelessness without even knowing that we had an issue just because we had a test which is awesome so now we have this code that satisfies this test case let's make our code a little bit better you see I don't think that this is a 9 a way of writing code because we would actually use the split options to remove any empty entries automatically on the split level so what I could say here is I would say if I just said new char array and I put my delimiter here I could dance play say split option remove empty entries and if I do that I don't need to check for that all that and I can just say if the length is 1 then return 1 if the length is more than compare the first and the second I did make a mistake here and the test coded of course if I just remove the previous version I automatically lose the 0 check but if I just say length equals to 0 now or just change this to any or it doesn't have any values and if I run the tests they are all passing so we earlier with our code and that means we can move to the next criteria so the next thing we need to Adam let me just copy it over is we need to be able to allow the method to handle an unknown amount of numbers so I'm going to copy this and I'm gonna create a new method here I could in fact add more inline data here the tests will look identical at least the body of them but I want to give different names to specify what I'm testing every time so now when I be able to allow any amount of numbers to be added so if I copy the theory in the inline data what my tests will be looking like is add adds up to any number when string is valid so I'm going to test one two three and this should be adding to 6 and that I'll probably add another scenario is like 10 and then 90 and then 10 again and then 20 and this adds up to 130 so if I save this now let's see what happens I'll look the new added tests fail and that's because we don't have anything to cover them now is when we go to the calculator class and we change this so let's look what we have here another requirement is any number can be added so what if I just remove all that and I say you go to a new line and now that everything is played I can actually do a select and pass everything as an integer and if everything is passed as an integer link which is a c-shape feature allows us to do the following we can do split numbers dot sum and this will summarize any amount of numbers passed as integers separated by the color removing all the empty ones which anyway meant zero to begin with so let's see what we did in fact all our tests satisfied and this new scenario is also satisfied so there's nothing really we can refactor at this point so the refactoring step is not applicable we just made the test pass with a minimum amount of code and we'll refer to our code as we went the next thing we need to add is the following we just copied over we need to allow the add method to handle the new lines between numbers instead of commas the new line is another character and we need to allow for it to be here we do not need to prove that coalmine new line need to be next to each other and that's not a valid case so the test specifies us to say that new line is treated just like a coma let's go ahead and add the test that actually tests for that so if I just copy the same thing again now I say add adds numbers using new line delimiter and let's reuse this we want to say that this should still give the same result and then a combination of the two again should also give the new the same result so this is how our test looks like if I just save and run the tests they should fail and in fact they do fail so now let's go to the calculator and make pass as you can see here in the split method we're accepting an array of different characters I can actually make a test pass so I can actually simply go here and say accept new line as well and if I save here they should actually make my test pass so I satisfied this again and there's not much much refactoring I can do here but there is a little bit that I can do from what I can see and this is extracting this array of delimited into their own variable so I can say well the limiters equals this array and then I can copy this here it gives more clarity to where I'm splitting on so we ran out of the way let's move on to the next requirement and the next requirement is the following support different delimiters to change in the limiter the beginning of the string will contain a separate line that looks like this so we have two backslashes and then the delimiter and then a new line and then the numbers so for example this should return three so let's go ahead and write the tests to cover that and I'm just gonna copy this suggested test case and I'm gonna leave it as a comment here until I taste the test so let me just do that so here we go and adds number using custom delimiter and I'm gonna use this very test case I talked about and I'm gonna paste it here and this should add to three and I can actually add another one as well that tests for more so for in this scenario and adds up to seven so I have my new test let me just save my file run the test see what happens it is failing so I am going to write some code to add the custom delimiter now to add things to an array you can but in this and now it will be weird do it so I can just simply change this to a list and that's a list of characters and then I can do the following I can say if my numbers start with double for slashes then I'm gonna split on first new line symbol which is what we are testing for to get our custom delimiter now I can do this by doing numbers dot split and I'm going to use a new new line delimiter here and I'm gonna say split this up to two and the reason why I'm doing that is because I want to split on the first newline appearance if new line appears on anywhere else in my string I don't want to split again on that so this will give me two pieces the first and the second and then I can just isolate the custom there limiter and I can say split a new line get the first part replace the double false lashes with a string dot empty and then just get a single character out of this because it should be a single character and then I can do the limiters dot add and I'm going to add the custom delimiter in my arsenal of are the limiters here and then simply mutate the incoming string from numbers to split on first line second half which is where I when I run my calculations on the reason why this is failing is because this needs to be an erase I'm gonna cast to an array and I'm gonna say up and let's see what happens now all my tests are passing so this again is now satisfied I'm gonna add and now and that's gonna be the last case that I will be showing you how to do and you will take four more to do on your own you can find the code for this in the description you can click on that folk the project on github and finish it so let's read what this is telling us to do calling add with a negative number will throw an exception negative is not allowed and the negative that was passed if there are multiple negatives show all of them in the exception message like to go ahead and write a failing test for that because I think tests that check that something is throwing are very important as well so what I want to do is let's see how much we can copy from this I'm gonna rename this so let's see what we're testing for this is ad should throw an exception when negative numbers are used in the string but the arguments are not calculation and expected but more like calculation and negative numbers so the calculation and let's just take it back a little bit it should look like this one coma two coma minus three and the expected thing that I want to see failing is this the minus one is a negative I want it to fail and I'm going to reuse the previous scenario so this custom delimiter one and I'm gonna say minus 2 and minus four and the string I'm gonna test to see what's negative is minus two coma - for now how do we test that something throws in c-sharp using fluent assertions you can do the fault line you can say action and then you can turn this call into an action which is a delicate essentially and what this allows you to do is to say let me just rename this to action and this allows me to say action should throw exception and I want this exception to have a message specific because the test tells us two negatives negatives not allowed and then I want to say what negatives are not allowed so negative numbers go here I'll just break it in a new line so you can say so this is what I'm asserting for now now clearly this test will now fail but we're gonna make it pass let's go back to our code and let's see what we have here so we're past the initial custom delimiter splitting and we have the split numbers are integers remember negative integers are still integers so I can just simply go here and say var negative numbers equals new list of integers and then for each split numbers which is potentiate only negative number I can simply say if potential negative number is less than zero so it's negative add it to the negative numbers list and then after this I can add the check that says if negative numbers have any values in them so if we have more than one essentially then throw new exception and this exception would read negatives not allowed if what is not allowed in this will be spring dot join and we're gonna say join the following things with a comma and the things we want to join all the negative numbers so if I now save that all my tests are in fact passing because the negative numbers are being thrown so again we went from red to green and now let's go back to blue and refactor and how can we refactor this one of the many ways I don't like the fact that we're using a for each loop we can just change this so we can say negative numbers equals split numbers thought we're using link X is less than zero are we gonna enumerate dot two lists and these are all the negative numbers so if we have any negative numbers then throw the exception and we can take this even further because as you can see the possible multiple enumeration is a warning so if I to list this here this goes away because I enumerated once and again my tests are passing please take a look down below you're gonna find four more cases to complete this cutter what's this video as many times as you want to make sure you understand the process it's a continuous circle that goes from write a failing test make the test pass with a minimum amount of code necessary we factor that code and go on and on and on into a circle I can guarantee you that you should be feeling liberated by the fact that you'll be able to change code without fearing that it might break this is a very strong tool and I highly suggest you understand and look into it and start using it today that's all I had for you for today leave a like if you liked this video subscribe for more content like this and ring the bell as well and we'll see you in the next video keep coding you
Info
Channel: Nick Chapsas
Views: 53,038
Rating: 4.8995633 out of 5
Keywords: Elfocrash, elfo, coding, core, C#, how to code, tutorial, javascript, js, csharp, rest, rest api, development, lesson, software engineering, dev, microsoft, microsoft mvp, nick chapsas, chapsas, tdd, test driven development, test driven development for beginners, for beginners, atdd, bdd, testing, xunit, unit testing, dotnet, .net
Id: y8TcPr73Bwo
Channel Id: undefined
Length: 25min 9sec (1509 seconds)
Published: Thu Sep 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.