Introduction to Unit Testing


Published on

A short intro to unit testing.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • - This presentation WILL NOT teach you how to unit test, it will introduce you to the concept, a brief overview of how to do it, how you can extend it and how it links to TDD
  • - Pura already introduced us to the concept of TDD.- Before we can start pure TDD, we need to understand Unit Testing- UT is the meat of TDD, without a thorough understanding, TDD is all but impossible.
  • - Unit testing is the act of testing the smallest testable part of a code, a "unit"- Unit testing should become second nature- Unit testing can also be automated, more about that at the end.- This pres will introduce you to the way I did unit testing for the STS front end.
  • - This pres. is about PHPUnit.- Pura has already successfully been using SimpleTest and will demonstrate his findings later.- We need to decide which is more suitable. PHPUnit, SimpleTest or both!
  • - PHPUnit is one of the xUnit (collective term for the UNIT tests ie: JUnit, PHPUnit, RUnit. Started with Sunit for smalltalk) family of frameworks that originated with Kent Beck's SUnit.- sUnit was written to test small talk and was later adapted to jUnit for Java and then further to PHPUnit for PHP.
  • Imowreck666Small bugs
  • Opo TerserBig Bugs
  •  conorwithonenYoung code* hiro008
  •  conorwithonenYoung codeOld code* hiro008
  • When developing and adding tests, running the tests and seeing them pass increases confidence in what you're doing, but, don't become complacent.Also...
  • Living Documentation of the system; unit tests can be run independantly to see how a method works
  • tm-tmBeware------------- Unit testing can never test everything- You can't possibly test all variations of input (unless it is absolutely expected)- UT should never be completley relied on- Still need integration testing and manual QA- You should also use version control, if a test that failed previously passed, then you'll need to go and look at why it passed to compare with why it fails (unless it is obvious).- Any failed tests should be identified and the problem fixed immediately otherwise the app will evolve out of sync with the tests and no-one will trust them making them useless.- Unit testing may add significant time (ex: if you have a function that returns boolean TRUE/FALSE, you need to write two tests that test both outcomes.)
  • This is the class we want to test.
  • malingering/
  • This is the class we want to test.public function someFunction($parameterOne, $parameterTwo) { return ((int)$paramterOne == $parameterTwo ? TRUE : FALSE);}
  • mailo [...someonestolemyname]
  • Ricky Romero
  • EricNixon
  • A stub replaces a real component on which the SUT (subject under test) depends on. This, for example, could be a “model” which will never actually looks in the database, it just returns a predefined set of data
  • A mock object is an object which replaces a real object in the execution of code. Similar to stubs, they don’t just return data, they check the data they are given is sane first then return pre-defined data. They can be used to throw exception in your test suite/harness to report errors in execution.It might be that you can’t directly test a method/class (it might be private, or otherwise not easily executable from external to the class). In that case, if the method/class accepted an object, you could pass it a mock object that had all the attributes of the real object, but also did some testing as it went about it’s business.
  • Hryck.
  • You can also test code that mutates data in a db.
  • Simply put, you provide PHPUnit with an XML document describing the database before and after a test, then run the test, then ask PHPUnit to check the two files against the database. You can also add filters to the database tests, for example if your database has a datetime column you’ll never be able to test it, so you can filter that kind of stuff out.
  • What else can you do?
  • BalakovXinc is our continual integration server (or CI Server for short)Xinc watches your every move, like some draconian overlord. If you commit something to the repo with broken tests or a code coverage percentage lower than the overlord likes
  • Adam Foster | Codefor You’ll have “broken the build” Everyone will be alerted and shame will be poured upon you… Naughty.
  • ZenTest harness or automated test framework is a collection of software and test data configured to test a program unit by running it under varying conditions and monitoring its behaviour and outputs. It can test at a higher level, ensuring the program “does what it is supposed to do”
  • A collection of test cases that test specific scenariosLess commonly known as a “validation suite”Test each “part” seperately A “part” can be a method, or a class, or a bunch of methods. Depending on what’s appropriate.
  • Sebastian Bergmann Selenium is a suite of tools to automate web app testing across many platforms. Specialises in front end testing (ie: testing web interfaces)
  • FlySiYou can think of it as your “automated testing robot” Like a QA guy you can get to run your tests many, many times
  • FlySi He won’t mind. He’s used to it. He can test all your form fields, your logins. With a known set of data, he can test data consistency, data accuracy and data validity. Selenium will actually open your target URL in a browser of your choosing, and check it renders correctly ( to a point, it’s not a messiah ). Failing tests if it finds things it doesn’t recognise or is unable to assert as true.
  • - Finally, on to TDD
  • It’s very much like I just described ... But backwards.First, you write your tests. Once you’ve done unit testing for a while, you’ll get a feel for what a test should do. You’ll be surprised, it’s not always about testing each method, but a group of methods (test harness)
  • ....TimRunning this ...
  • Creates this...
  • Behaviour Driven Development (not Business Driven Development, although it could be!)ITS NOT TESTING!
  • Behaviour Driven Development (not Business Driven Development, although it could be!)ITS NOT TESTING!
  • Introduction to Unit Testing

    1. 1. Unit Testing<br />How to make your code rock<br />Mike Pearce<br />
    2. 2. Introduction to<br />unit testing<br />
    3. 3. Toes<br />Toes<br />
    4. 4.
    5. 5. He madePHPUnit<br />Sebastian<br />Bergmann<br />
    6. 6. History<br />
    7. 7. ?<br />Why<br />use it<br />
    8. 8.
    9. 9.
    10. 10.
    11. 11.
    12. 12. $confidence++<br />
    13. 13. Living Tests<br />
    14. 14.
    15. 15. Onto the<br />CODE<br />
    16. 16.
    17. 17. %> phpunit --skeleton-test<br />
    18. 18.
    19. 19.
    20. 20. %> phpunitunittests<br />
    21. 21.
    22. 22.
    23. 23.
    24. 24. %> phpunitunittests<br />
    25. 25.
    26. 26.
    27. 27.
    28. 28. %> phpunitunittests<br />
    29. 29.
    30. 30. Other test<br />methods<br />
    31. 31. stubs<br />
    32. 32. mock objects<br />
    33. 33.
    34. 34. Not a database table<br />
    35. 35.
    36. 36. Extending<br />
    37. 37. xinc<br />
    38. 38. broken build<br />
    39. 39. Test<br />harness<br />
    40. 40. test suite<br />
    41. 41. selenium<br />
    42. 42. Automated<br />Testing<br />Robot<br />
    43. 43. Hi!<br />Automated<br />Testing<br />Robot<br />
    44. 44. and now...<br />TDD!<br />
    45. 45. sdrawkcaB<br />
    46. 46. Remember this?<br />%> phpunit --skeleton-test<br />
    47. 47. %> phpunit --skeleton-class<br />
    48. 48.
    49. 49. and now...<br />BDD!<br />
    50. 50. it’s<br />NOT<br />testing...<br />
    51. 51. Huh?<br />
    52. 52. So if it's not about testing, what's it about?<br />It's about figuring out what you are trying to do before you run off half-cocked to try to do it. You write a specification that nails down a small aspect of behaviour in a concise, unambiguous, and executable form.<br />Dave Astel<br />
    53. 53.<br />
    54. 54. questions?<br />
    55. 55. more info...<br /><br /><br /><br /><br /> (Part 1,2...4)<br />This presentation on<br />
    56. 56. flickr attributions<br />* hiro008<br />tm-tm<br />malingering<br />...someonestolemyname<br />EricNixon<br />Hryck.<br />Balakov<br />Adam Foster | Codefor<br />Zen<br />Sebastian Bergmann<br />FlySi<br />....Tim<br />