What is BDD? | Cucumber for Java Example | Tech Primers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do you know what ASCO khumba have you used it before okay wait this is not the cucumber which we used to eat this is the automation testing framework which I am talking about if you have not known about koo kumba or if you have not used to khumba let's see how we can use kookaburra and why people use coke umber to write test cases additional to the J units and the integration tests we already have place the bell icon on the YouTube app and never miss any update from tech friends cucumber is a framework for writing behavior driven development tests oh wait what is this behavior driven development write behavior driven development is similar to the s driven development where we start off with tests however in BDDs we start off with writing behaviors imagine when your product owner is asking you for what was the test which you have written for a particular feature you cannot show him the J unit or the integration test right what if you see you of the company or your business analyst or your customer comes and says show me the feature what is the feature deliver you cannot show your test saying okay this is what the test is doing right people might not understand because you might be writing each feature in a different language right kokumo helps us in solving that problem by creating behavior around tests so how does the behavior help them because the way the behaviors are written that is what is going to help them in understanding that particular feature kokumo uses a language called kirkin and the Gherkin language looks something like this so this is an example of a feature file the feature says it's a feature so some description is present here and the scenario says some determined business situation so for example let's consider the example of a fizzbuzz test so I have an example which I have a class which expects you to play with a number if you have a multiple of three it is going to safe is if you have if you provide a number which is a multiple of five it is going to say bus alright so that is what my class is doing so you can write a scenario saying this scenario is specific to get a number which is eligible for Phase and given so you need to provide a precondition so you can provide let's say I need to have my game play set right so I need to create a fist bus game that is what the given is and an could be some additional condition to support that precondition if you don't need it you can ignore it the next step is the bend so when is something like an action when you want to play the game so the ven is where you play the game with a particular number so you just say I want to play the game with three so that is where the ven will go and then again you have additional and hands which are supporting the actions then finally then then is where the assertions happen in like a normal test so in then you say the game returns is something like that so we will see how to write that in the next few minutes however why do we have BD DS so BD DS are useful when you want to communicate the behavior of a feature to somebody who is not technical so the behaviors are the selling point between a developer and a non-technical person and that is why people start writing feature files or behavior driven files so that it defines the complete functionality of a feature and these are automated as a test so that your behaviors can be changed anytime without breaking your code so koo kumba is one such framework which uses gherkin language and which uses the BDD approach and it is completely open-source you can use it I use koo kumba a lot in the last two to three years I have been using Co cumber a lot for writing behavior driven development test now that we know what is Co khumba let's jump into some hands-on programming with koo kumba right so I have my ID open so this is the IntelliJ IDEA I also have the kokum before Java plug-in because I'm going to write the code in Java so I already have the kokum before Java plug-in installed you can also install that this will help you in suggesting or correcting the files which you are writing the feature files basically which is the co Cooper framework understands so it used to help you in writing the Gherkin language so I will show you that in a while so before that we need to add the dependencies the Kokomo dependencies because we need to run some Kokomo tests right so we need to run the feature so feature file is the starting point of a khumba test so it is something similar to your j init feature files are the starting point however in order to run those feature files and convert into the respective java code we need to use the coq umber dependencies so I'm going to use the Kokomo dependency I am going to use the latest version of koo kumba so the artifact and ID is coumba Java and the group ID is info dot cukes and the version I'm going to use is 1.2.5 so this is the current latest version of koo kumba i'm going to use the same also we need to add the j-unit dependency for it khumba j-unit so that me we can run j units with kokum ba so that's it so these are the two different Java dependencies which we require for koo kumba the starting point of Cocoon bar is the feature file let's take a look at the feature which we have written so the feature is nothing but game so this is the game it is called fizzbuzz right if you provide a number to this particular game so if you if let's say three people are paying this game the player will be given a number and based on the multiple of three or five he has to tell a different term so if the number is a multiple of three he has to say fizz and if the number is a multiple of five here say buzz right and also there is one more functionality which is pending which is not written here which is fizzbuzz so if the number is both the multiple of three and fine it should be fizzbuzz so I have not written that here but I will show you how I can add it and then add a new feature right so this is the game so the game is pretty simple if you just provide a number if it is a multiple of 3 it is fist if it is a multiple of 5 it is burst that's it so we need to write a feature file for it so I'm going to write the feature file I will just call it as fizzbuzz dot feature so all coke ember files should be renamed or should be named with an extension dot feature and if you see automatically detects that this is a kick number file since this is a feature file we can provide a feature so if you see IntelliJ is like telling me what I should write right and this is nothing but a feature so I'll just say this is a fizzbuzz feature play right I'll just say Phyllis Bears game gameplay because we were just playing a face bus game isn't it so this is just the name so this you can write anything if somebody sees as this particular feature file they should be able to understand that's that's the ultimate idea that's the ultimate idea of the behavior winters right so once we have the feature ready we need to now write scenarios so senior rate scenarios are nothing but your description for a test so the first scenario which I'm gonna run this play fizzbuzz to get Phil's right I want to give a number which should written fizz always right that's what my scenario is now my scenario is set I need to get the output has face that's it the next part is providing the inputs on creating this particular gameplay right so the first one would be given so given is when you provide some preconditions saying okay this is what is required before running my game right so in the given part what I will do is I will just say create a fizz bus game right I need to create a game because I need to create a game first right or I will just save create a Facebook game play right that is what my precondition is I need to have a fizz bus game now I need to provide an action so how will the game proceed when I provide something right when I say so when is the key word which we'll be using so all these orange are the key words so these are the Gherkin key words which is required for running the Kakuma right son and each as I said given is a precondition and when is where you provide an action now I need to provide an action so what action do we need to provide so we need to get fizz in order to get fizz we need to play the game with the number three so I'll say when when I play with number three right when I play with the number three that is what my action and so now the game is going to play with the number three will be played with the number three now the output needs to be verified right so what was the result of the game right that is when we will be using them so given when then are the same approach which we use across all these scenarios where given is a precondition when is an action and then is the assertion so what happened after the game played right the result was fills so you can had add something meaningful since I chose the fizzbuzz example I am not getting much of a meaningful sentence but you get the point right the den then is like an assertion so I have written the feature file so this is a single single scenario so in a feature file I have one scenario so imagine this as one particular test now I need to convert this into a Java code which can test the main code right so I have only the feature file now we need to create something called step definition so these are different steps if you see these are different steps whatever we have written here given when and then these are different steps and we need to define these steps right underneath these steps there should be something happening that is when the test is going to execute right so I'll use the intelligence feature of creating a step def writer directly from here so I'll just say create step def it just says my filename is my step def but I am going to provide the name as fizzbuzz step def also the file location is something I'll just say comm take prime slash testing so this will create a step definition with a single step so I see that it has created a step definition with some step right now I will have to create all these steps so IntelliJ identifies that there is no steps defined and it shows that by highlighting it right it says undefined step what I will do is I'll just create all these steps with the help of intelligent automatically I'll just say create all step and I will say create it in this particular file that's it all my steps are done and if you see here there is nothing if I run this particular test right it will throw an exception so if I run this particular scenario so in in Kokomo we call it as a scenario I cannot call it as a test because each of these are the are a behavior so each of these are nothing but a scenario see that when I ran it it failed right because we did not write anything there is an exception right with saying do something so I need to do something there so let's go one by one so in my given what should I do in my given I need to say create a gameplay so I need to create a gameplay I need to create a this bus object right because that's what I need in order to create my gameplay I need to create a new physical object so I'll just say new fizzbuzz I'll just created a new fitness class that will create a new game flipped from me right the next one is I play with the number three so I'm going to play the game with the number three so we need to provide the phys bus object we just say play and we just need to provide the number so now argument is going to be the number right I'll just provide the number here and this will be stored in a variable right and this variable is the result isn't it now we need to access this result in the next in the next step the result was fizz right so this this result is now fist how will I assert it's in a different function right so what I'll do is I'll just make this as a global variable and I'll just use it here now I need to assert it assertions dot so it equals I'll just say a result needs to be asserted with fizz right because the function says the result was fist the result is face I can name it as a result is fist I'll just name it as some grammatical so that's it now I have written my scenario I have implemented my step definitions let's run this test again this should now go to success isn't it yep see that the tests and I'm succeeding because I provided the number three and my gameplay is returning phase now the business says I need to provide another gameplay as well another scenario to get was I need to get buzzed right now here I say that I play with a number five and if I see I need to change the result as well right so when I change the result this step definition is not present however if I see these can be changed anytime so what I will do is I'll just enclose these with double clothes and then make it as a string and I'll write my step definition as an argument so this is going to be the result string I'll just do the same here only thing is I'm just going to assert with whatever we are getting from the behavior-driven test that's it so I don't need this particular then I don't have to have duplicate step death right so I have written a step Def where it returns it gets the argument and then it asserts based on that so that way both of my tests are see this right IntelliJ is now saying that there is no issue now both of my tests will work with just a single function so this way you can write a BDD step definition in such a way that whenever you write more and more scenarios so that your tests can accept new scenarios or new step definitions without even having to change the implementation or the definition so let's run this feature I will run both the features so that way I will know that both of them are passing right see that both of them succeeded right now the business comes and says I'll show you a typical example when the business will be able to understand this right so they know that okay this is your feature file now they come on say I need to add a new scenario saying when I return a number when I provide a number 15 which is a multiple of both three and five it should return fizzbuzz so the BA or maybe your customer right they will provide you this particular example they will just say okay I want the feature or the scenario to be like this right I'll just say 15 when I do this I need to get this that's it so they will provide this as a requirement saying okay this is my scenario that's it so go and write your implementation for that now when I run this particular feature file it is obviously going to fail because there is no implementation right so there is no feature present in this so that way now I have already the scenario or the test I just need to go and change the code so you don't have to write the scenario as a developer however this would be provided by your business analyst or your product owner or your customer whoever it is right that's it so now I'll go to the main class and I'll just do some refactoring so when I do refactoring my scenario doesn't change the user doesn't even have to know the BS doesn't even have to know what I changed right so what I will do is I will just do some refactoring here I will enclose this into a method right is a multiple of three I I need to provide okay okay this is a good example I will just say is a multiple off and then I'll use it for five as well right so now if I I have just refactored something now I want to make sure my tests are working right I will just run the feature file again and then make sure that s run after factor the other two tests are not affected yeah both got succeeded now I will go and add my new one is a multiple of three and he is a multiple of five so this is what I wanted right if I if it is a multiple of three and five both I'll just say fizzbuzz so this is my feature I've added a new feature or a scenario basically for the behavior Ventus I'll run the scenario again I'll run the feature fell again now all the three should pass because I have added the fizzbuzz as well however notice that it is not succeeding why because if you see the number was a multiple of three already and it returned fish before it even reached this particular line I just move this he flew Pablo and see that because the first time when it came it was successful with this particular if loop and that is why it went to fist now I am moving this operation above so that way I know that if it is a multiple of both then first written fizzbuzz then only come to the other checks right see that all my scenarios are successful and IntelliJ shows up in a well structured manner right so if you have a very big feature file and then you have lots of scenarios in it you can easily check that with lots of steps right so this is how you can write a cucumber test basically a behavior driven test I'll just summarize again so kokum bar is the behavior driven development framework which is open source you can use it for writing different scenarios different features this can be shared with the business be is product owners and people who are non-technical able to easily understand this particular file and they will be able to come up with more and more scenarios so that you can add that to your product so in this particular example we saw how to play the fizzbuzz game say we add up his buzz claim and we added some scenarios these scenarios will be converted into something called step definitions in this table definition you will have to write the code this code is something similar to your J init code right so I have not written it as a J unit specifically you will have to run it with a kookaburra and all those stuff I just used intelligence Runner so that is why IntelliJ was able to run without any hassle but in the next video what I will do is I will show you how we can run this comber as a part of your build so as a part of your Jenkins build you will see how we can run the Kokomo but the step definition is the file which is exactly similar to your J init file where we write write the definitions of the or the implementation of what should be done for this particular feature so this is how you can automate your testing for your product right I hope you guys understood what is cuckoo bird if you like the video go ahead and like it if you haven't subscribed to the channel go ahead and subscribe to it meet you again in the next video thank you very much [Music]
Info
Channel: Tech Primers
Views: 88,203
Rating: undefined out of 5
Keywords: techprimers, tech primers, cucumber, what is cucumber framework, cucumber for java, cucumber java example, cucumber implementation, what is bdd, behaviour driven development, BDD testing, behaviour driven development testing, bdd automation, automation testing framework using cucumber, automation testing framework, cucumber for java example, cucumber example, cucumber java dependency, cucumber feature example, cucumber scenarios example
Id: Wc3UDbYO_b4
Channel Id: undefined
Length: 20min 56sec (1256 seconds)
Published: Sat Jun 16 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.