Introduction to Unit Testing

  • 1,718 views
Uploaded on

A short intro to unit testing.

A short intro to unit testing.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,718
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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!

Transcript

  • 1. Unit Testing
    How to make your code rock
    Mike Pearce
  • 2. Introduction to
    unit testing
  • 3. Toes
    Toes
  • 4.
  • 5. He madePHPUnit
    Sebastian
    Bergmann
  • 6. History
  • 7. ?
    Why
    use it
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. $confidence++
  • 13. Living Tests
  • 14.
  • 15. Onto the
    CODE
  • 16.
  • 17. %> phpunit --skeleton-test
  • 18.
  • 19.
  • 20. %> phpunitunittests
  • 21.
  • 22.
  • 23.
  • 24. %> phpunitunittests
  • 25.
  • 26.
  • 27.
  • 28. %> phpunitunittests
  • 29.
  • 30. Other test
    methods
  • 31. stubs
  • 32. mock objects
  • 33.
  • 34. Not a database table
  • 35.
  • 36. Extending
  • 37. xinc
  • 38. broken build
  • 39. Test
    harness
  • 40. test suite
  • 41. selenium
  • 42. Automated
    Testing
    Robot
  • 43. Hi!
    Automated
    Testing
    Robot
  • 44. and now...
    TDD!
  • 45. sdrawkcaB
  • 46. Remember this?
    %> phpunit --skeleton-test
  • 47. %> phpunit --skeleton-class
  • 48.
  • 49. and now...
    BDD!
  • 50. it’s
    NOT
    testing...
  • 51. Huh?
  • 52. So if it's not about testing, what's it about?
    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.
    Dave Astel
  • 53. www.phpunit.de/manual/current/en/behaviour-driven-development.html#behaviour-driven-development.bowlinggame-example
  • 54. questions?
  • 55. more info...
    http://en.wikipedia.org/wiki/Portal:Software_Testing
    http://phpunit.de
    http://en.wikipedia.org/wiki/PHPUnit
    http://www.ohloh.net/p/phpunit_pocket_guide
    http://www.youtube.com/watch?v=28KGs9c0bSw (Part 1,2...4)
    This presentation on Slideshare.net:
  • 56. flickr attributions
    * hiro008
    tm-tm
    malingering
    ...someonestolemyname
    EricNixon
    Hryck.
    Balakov
    Adam Foster | Codefor
    Zen
    Sebastian Bergmann
    FlySi
    ....Tim