Running PHPUnit tests after each commit (Get started with Jenkins part 5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in the previous video I showed you how you can integrate Jenkins with bitbucket so that the new build is automatically started after each commit but we didn't actually do anything we didn't build real code we didn't test it we didn't deploy it so in this video I'll be taking it a bit further and configure Jenkins to let it run speech for unit tests after each commit to follow this video you'll need to know the basics of unit testing you also need to know the basics of PHP and PHP unit if you don't know any of these subjects you might find it a bit harder to follow this video but give it a try so what's next for this video well first of all I'm going to run through the code that I want to test afterwards I'm going to install PHP unit onto my Jenkins server and I'm going to install the J unit plugin for Jenkins then I'm going to configure our existing job so that it runs unit tests and then I'm going to test and verify that everything works correctly but hold on a minute you might ask why do you install the J unit plug-in to run PHP unit while good question to answer it let's take a look at the complete flow it begins when Jenkins starts the build process we will configure it so that it runs PHP unit tests on the server PHP unit will run all these tests and we will ask it to generate a report of all these tests in the J unit XML format and it will actually store that on to the file system now finally Jenkins will parse the contents of the XML file it can determine if all tests were successful and act accordingly so now that you have this overview let's begin so for the purpose of this video I also created an example project and my example project project is stored in the folder Jenkins - PHP unit - tests and it basically contains just one PHP class it's called combo machine PHP which mimics the behavior of a gumbo machine it has one attribute it has the number of gumballs in the machine as attribute and then it has three methods the first method is a getter which returns the amount of gumballs that are still in the machine the second one is a setter which allows us to set the amount of gumballs in the machine and then the third method method is turn wheel which simulates a user turning the wheel and the Machine dispensing a gumball so this reduces the number of gumballs in the machine by one now we also wrote a unit test for this gumball machine class and I put that in the test directory so here is gumball machine test dot PHP and this is a PHP unit framework test case which creates a new instance of a new gumball machine every for every test and then I just have one test to see if the wheel turn actually works so what we do is we create a new instance of the machine the gumball machine we set the amount of gumballs to 100 we then turn the wheel and then we verify that the amount of gumballs in the machine is 99 so just to show you that everything is working correctly let me open up my terminal and run HP unit so I'm going to run PHP unit test slash gumball machine test I'm going to hit enter and here it says okay one test one assertion everything went well and just to show you I'm going to screw up the gumball machine and show you what happens when this test actually fails so instead of reducing the amount of gumballs in the machine by one every time we turn the wheel I'm just going to do nothing I'm not going to reduce the amount of gumballs in the machine which is which is done correct so I go back to my terminal I run the test again and now boom there was one failure fail failed to assert that 100 matches the expected 99 so I'm going to fix the gumball machine and then I'm going to show you the bitbucket repository for this project so here a bit bucket I have the exact same project I have my gumball machine with the same logic and I also have the same unit tests now I've also created a job in Jenkins which is also called Jenkins - PHP unit - test to actually build this project and unit test it as we make new commits but the first thing we have to do is we have to install PHP unit test on to our Jenkins server so for that I'm going to open up an SSH connection to my server okay so here I am logged in into my Jenkins server and installing PHP unit test is actually incredibly easy you just have to run sudo apt-get install PHP unit and this will install not only PHP unit will also install all the dependencies such as PHP itself and many other frameworks so I'm going to say yes and I'm going to speed up the video so that this doesn't bore you okay so that's all we have to do on our server PHP unit is now correctly installed and we can continue with the Jenkins part so I'm going to go to Jenkins and the first thing you need to do is you need to verify that the J unit plug-in is installed and enabled in your Jenkins installation now normally this is the case but check it anyway so and available in the manage plugins go to the installed section and here you should see that the J unit plug-in is indeed active and installed so now I'm going to go back to the home page of Jenkins and I'm going to change the settings of this job so I'm going to configure it and in the in the build section I'm going to add a new build step so here I'm going to go for execute shell and what I want to do here is I want to instruct Jenkins to run the PHP unit tests and then do something with the results so if the tests failed and Jenkins should obviously fail the build and if the tests succeed then it should continue with the other tasks so here I have the possibility to execute any shell command that's available on my linux server so I'm going to run PHP unit and I'm going to instruct it to log the results as in or in the J unit file format and I'm going to say that you can log in in results - peach-pit unit - HP unit XML you can choose this path and you can choose the name of the test results as well now you might not have noticed but in my project I also have a beach per unit an XML configuration file and I'm going to reference that here I'm going to say the configuration file you have to use for this unit tests is located in a desk directory and it's called PHP unit down XML now just to show you I can show you this is this configuration right here it's in the test directory it's called PHP unit of XML and here basically I say that the entire test suite is located in this very directory and it will run all the tests it finds in this directory pretty simple back to Jenkins that's pretty much all we have to do for the execute shell step here so this will this will be running on every build and PHP unit will publish results as a J unit XML file okay so now that that's done Jenkins will run the beach for unit test but it won't do anything with it so to do something with these test results I'm going to add a post built action and I'm going to choose publish J unit test results report and this basically asks me a which results or which reports do you want to publish and now I gave it this path so I set to beach be unit publish your results in this file so all I have to do is I have to copy and paste it in here and then I can configure the health report amplification factor now I want all my tests for this project to succeed now you could say the build is still successful if just 90 percent or 95 percent of test succeeded but in this case I really want all my tests to succeed well hey I've only got one test so would be pretty bad if even that test would fail okay so that's it I'm going to save my project and I'm going to test our configuration so the for the purpose of this video I'm not going to create a new commit and test if everything works correctly I'm just going to manually trigger the build so I'm going to click on build now the schedules of building and should run our beach video test and as you can see our build succeeded I can click on it and I can see test results right here and it says no failures and if I click on it I can see well in the root package there is a gumball machine test it took three milliseconds to complete there was one past test I can even click on it I can see the name of the test I can see how long it took okay so let's now see what happens if these tests fail so let's go back to our gumball machine and that's rigged it let's make it faulty I'm going to edit this file and instead of giving the user one gumball for every turn I'm going to say we're going to give him two I'm going to make a commit I'm going to leave the commit message like this I'm going to say come in and I'm going to go back to Jenkins and I'm going to trigger another build here so I'm going to get back to project built now and wait for the build to finish and boom there you have it built number two has failed if I click on it I can see one failure that's one more than the previous built and if I click on test results again I can see which test has failed and what the differences are between this build and the previous build so that's it for this video curious to learn more about Jenkins well in the next video I'll show you how you can integrate Jenkins with the bitbucket build status API that way you can view the status of your builds on the bitbucket website thank you very much for watching this video and if you liked it you can support me by following me on Twitter or subscribing to my channel you
Info
Channel: Simply Explained
Views: 43,202
Rating: 4.9410028 out of 5
Keywords: lesson, devops, bitbucket, free, jenkins, best practices, continuous integration, introduction, unit testing, howto, free learning, basic tutorial, video tutorial, ci, junit, basics, java, getting started, explained, dummies, beginner, fundamentals, training, unit tests, course, learning, phpunit, in action, tutorial
Id: 68cDNUz7uro
Channel Id: undefined
Length: 10min 39sec (639 seconds)
Published: Tue Mar 15 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.