PHP UK Conference 2013

Una introducción práctica a la creación de tests unitarios con PHPUnit

Harrie Verveer  · 

Presentación

PDF (pincha para descargar)

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

okay hi I'm Harry um I've been a PHP developer for almost a decade now and last year I switched jobs I now work for a back shop in Netherlands it's a web shop I also have a Twitter account which you can see here I'm from hoosh in the Netherlands

which is there so it's not really that far you know it's just like there's a bit of water in between but besides that I like to think we're practically neighbors right and as I said I work at back shop so we are an e-commerce website we're

selling equipment for stages Studios musicians DJ's that kind of stuff until now the website is in Dutch so we are actually shipping to the UK but if you wanted to order something you would have to learn Dutch first so I'm sure eventually there will

be an English version as well but for now this is it and I'm going to talk to you about unit testing today I'm first going to explain to you why unit testing is important and why you should do it let me ask you who of you has ever written unit tests

yes okay so this talk is really meant as like an introduction to people who haven't written you need that's much so I don't know if this will be too simple for you guys or boring but whatever if you wanted just to see if you're doing the right

thing then of course you're welcome to stay who of you has never written unit tests before also quite a lot who of you has been planning to start writing unit tests but never got around to do doing it yeah a lot of times now I was hoping for that that

happens a lot right and that was kind of why I decided I wanted to do a hands-on presentation so me actually writing some unit tests for you guys so you can actually see how it works in practice and hopefully that will help you getting started when you're

back working on your own project and so I'm first going to explain why unit testing is important then we're going to write a really simple school book example unit test I'm going to explain something about writing testable code and why it's

important how you do that I have some things I place under the chapter voodoo which are things you probably shouldn't do but they are damn handy so you probably would like to do them anyway test-driven development I want to explain what it is and why it's

important and then I want to tell you what to test so first why is unit testing important well first of all I like for something I like to call the balloon effect in software it's really common that you fix a problem at one place and you test that and

that's fixed you know and you you put it in the repository and you think you're done but then that change suddenly happens to break something else somewhere else in your code you know so you go there and you go fix that and then that's fixed but

then the other thing is broken again you know and you keep like running around all the time so just testing what you just build or change doesn't always cut it in an ideal situation you would like to test your entire application every time after every

change you want to test everything is everything still working the way it is and doing that manually is like a lot of work if not impossible you know I mean imagine after every bug fix you would have to test your entire application that you're working

on is everything still work is everything still there so you're not going to do that and it's important to have a tool for you that can do that and be it per unit is a tool that will do it for you if you write your unit tests another reason this is

the cost of change curve well or my interpretation of it but what this says is that the earlier that problems are found in your software the cheaper they are to fix now this is really an old concept going back to I think the 70s or something like that and

what it basically says is that when you're in your functional design stage and you find a flaw in the design it's still relatively easy to fix right you can just like change a couple of lines in the functional design maybe upload another graph and

you're good to go if you discovered in the technical design it takes a little bit more time to fix the problem because you have to go back to the functional design change that as well think about technical solution as well but it's still relatively

cheap if you're in development it's already a bit more expensive to change and so on if we're in an acceptance test and we discover a problem that might be quite a lot of work right if somebody says well this this isn't what I want I want to

work as differently or find a book or something else you will have to go back into your code investigate what was I doing here you're it's not in your head anymore so you have to we have to read up on it and so on and even worse in production of course

right if something's in production if something in production is wrong it takes a lot of time and a lot of costs to fix it now this graph as I said H goes back quite a long way and you can imagine back in the days when we were distributing software on

CDs or stuff like that you would actually have to send out a new CD or whatever you know where the software said that was expensive and nowadays with the web and with the modern technology development methodologies like a child and stuff like that it may be

not this literal anymore but if still a lot of it's still true you know the sooner you find a book the easier it is to fix and the less costs you have so that's why it's important so check we've seen that now let's go write the first unit

test this is the first time I'm going to do live coding on stage and I was always told that this will blow up in my face sometimes so but it will fend for you guys to see as well you know I'm using PHP unit to run my unit tests I think it's the

most popular tool to run unit tests at the moment you can find it at PHP unit de and this is linked to the manual of course that's also on the website the document is the documentation is very thorough and well-structured so it's really easy to find

things and and so if you start unit testing if I write you in tests I always have this like in the background window somewhere to pop up whenever I need to know something or want to look something up installing PHP units I leave up to you the free method you

can upload you can use pair you can use composer or you can download the for archive it's all very well documented and I trust you all with that you can manage that widest like typing free lines of code or something in your command line and then you have

it so that that's rather easy so let's write the first unit test oh there we go again if you can't read this move closer okay so this is the method we want to test right we want to write the unit test for this of course it's a very easy method

but I always like to write a unit test for some method like this in my project just to be sure that I've got everything set up properly they know so that PHP unit is running in etc if this is my project I can create a folder unit tests in it and what you

see is that I placed two files in here you can probably you can't read this right I'm sorry about that but I didn't know how to get left column larger but this is PHP unit that XML is in the unit tests that directory and what I'm telling here

is that there's a test suite test suite is a way of grouping tests together so that you can later on you run certain tests or all together you know but you can decide to only run tests on a certain subsystem or only the smoke test really important tests

and leave the big test for everything in a separate test suite something like that in this case I'm declaring just one suite which is called as RC that's the name of our test suite and the directory where the tests reside in is called SRC so that's

the directory over here that's the directory in our unit tests directory you can also see a reference here to the bootstrap dot PHP and this is the file that PHP unit will include before starting to run any tests so you can use use bootstrap dot PHP for

sample two if you're using cent framework to include cent framework to get that auto loader running to get the database connections working stuff like that you know same for other frameworks of course in this case the only thing I'm interested in is

[ ... ]

Nota: se han omitido las otras 4.221 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.