Unit and integration Testing

  • Published on
    14-Dec-2014

  • View
    1.583

  • Download
    0

DESCRIPTION

Lets break some code. In this workshop we will be going over some of the fundamental concepts of software testing and take a hands on approach to writing Unit and Integration tests. We will cover topics such as mocking, stubbing, database patching and how this can all fit into a continuous integration environment like Jenkins.

Transcript

1. UNIT & INTEGRATION TESTING David Berliner 2. Why Test Types of Tests PHPUnit Writing Tests Getting Stuck In 3. WHY TEST? 4. TESTS REDUCE BUGS 5. TESTS REDUCE BUGS TESTS ARE GOOD DOCUMENTATION 6. TESTS REDUCE BUGS TESTS ARE GOOD DOCUMENTATION TESTS ALLOW SAFE REFACTORING 7. TESTS REDUCE BUGS TESTS ARE GOOD DOCUMENTATION TESTS ALLOW SAFE REFACTORING TESTS REDUCE THE COST OF CHANGE 8. TESTS REDUCE BUGS TESTS ARE GOOD DOCUMENTATION TESTS ALLOW SAFE REFACTORING TESTS REDUCE THE COST OF CHANGE TESTING FORCES YOU TO THINK 9. TESTS REDUCE BUGS TESTS ARE GOOD DOCUMENTATION TESTS ALLOW SAFE REFACTORING TESTS REDUCE THE COST OF CHANGE TESTING FORCES YOU TO THINK TESTS REDUCE FEAR 10. A study conducted by Microsoft and IBM showed that writing tests can add 15% 35% to development time but reduce the number of bugs by 40% 90%. http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf 11. TYPES OF TESTS 12. Black Box White Box Unit Integration Functional System Regression Performance Smoke Canary Usability A/B 13. Black Box White Box Unit Integration Functional System Regression Performance Smoke Canary Usability A/B 14. UNIT TESTS The goal of unit testing is: 1. to isolate each part of the program, and 15. UNIT TESTS The goal of unit testing is: 1. to isolate each part of the program, and 2. show that the individual parts are correct. 16. UNIT TESTS The goal of unit testing is: 1. to isolate each part of the program, and 2. show that the individual parts are correct. Unit tests have a very narrow and well-defined scope. 17. UNIT TESTS Pros: 1. Fast 18. UNIT TESTS Pros: 1. Fast 2. Simple to understand 19. UNIT TESTS Pros: 1. Fast 2. Simple to understand 3. Reliable 20. UNIT TESTS Pros: 1. Fast 2. Simple to understand 3. Reliable Cons: 1. Large time investment 21. UNIT TESTS Pros: 1. Fast 2. Simple to understand 3. Reliable Cons: 1. Large time investment 2. Requires maintenance 22. UNIT TESTS A unit test should NOT: 1. Access the network 23. UNIT TESTS A unit test should NOT: 1. Access the network 2. Hit a database 24. UNIT TESTS A unit test should NOT: 1. Access the network 2. Hit a database 3. Use the file system 25. UNIT TESTS A unit test should NOT: 1. Access the network 2. Hit a database 3. Use the file system 4. Call other non-trivial components 26. INTEGRATION TESTS Test the inter-operation of multiple subsystems. Pros: 1. Make sure nuts and bolts fit together 27. INTEGRATION TESTS Test the inter-operation of multiple subsystems. Pros: 1. Make sure nuts and bolts fit together 2. Test behaviour and infrastructure 28. INTEGRATION TESTS Test the inter-operation of multiple subsystems. Pros: 1. Make sure nuts and bolts fit together 2. Test behaviour and infrastructure 3. (tested code) / test % is high 29. INTEGRATION TESTS Test the inter-operation of multiple subsystems. Pros: 1. Make sure nuts and bolts fit together 2. Test behaviour and infrastructure 3. (tested code) / test % is high Cons: 1. Hard to test all critical paths 30. INTEGRATION TESTS Test the inter-operation of multiple subsystems. Pros: 1. Make sure nuts and bolts fit together 2. Test behaviour and infrastructure 3. (tested code) / test % is high Cons: 1. Hard to test all critical paths 2. Harder to localise source of errors 31. TEST HIERARCHY 32. PHPUnit 33. 1. Unit Testing Framework written in PHP by Sebastian Bergmann 2. De facto standard 3. Major Frameworks use it (Zend, Cake, Laravel, Symphony etc.) 34. Installation: https://phpunit.de/manual/current/en/installation.html Be sure to install xDebug in order to generate code coverage. Note: PECL no longer supported 35. WRITING TESTS 36. ORGANISING TESTS - The tests for class Foo are placed in a class FooTest - Most of the time you will inherit from PHPUnit_Framework_TestCase PHPUnit_Extensions_Database_TestCase - Tests are public methods named test* - Inside the test methods, assertion methods such as assertEquals() are used. 37. ORGANISING TESTS Tests should mirror the code being tested. SRC SomeFolder Baz.php Foo.php Bar.php TEST SomeFolder FooTest.php BarTest.php BazTest.php 38. PHPUNIT.XML 39. BOOTSTRAP.PHP 40. ASSERTIONS assertArrayHasKey() assertClassHasAttribute() assertClassHasStaticAttribute() assertContains() assertContainsOnly() assertContainsOnlyInstancesOf() assertCount() assertEmpty() assertEqualXMLStructure() assertEquals() assertFalse() assertFileEquals() assertFileExists() assertGreaterThan() assertGreaterThanOrEqual() assertInstanceOf() assertInternalType() assertJsonFileEqualsJsonFile() assertJsonStringEqualsJsonFile() assertJsonStringEqualsJsonString() assertLessThan() assertLessThanOrEqual() assertNull ( ) assertObjectHasAttribute() assertRegExp() assertStringMatchesFormat() assertStringMatchesFormatFile() assertSame() assertStringEndsWi th() assertStringEqualsFile() assertStringStartsWi th() assertThat() assertTrue() assertXmlFileEqualsXmlFile() assertXmlStringEqualsXmlFile() assertXmlStringEqualsXmlString() 41. ASSERTIONS 42. ANNOTATIONS @DEPENDS 43. ANNOTATIONS @DATAPROVIDER 44. ANNOTATIONS @EXCEPTIONS 45. What do you do if the code you want to test is dependent on other components that cannot be used in the test environment? 46. MOCKS & STUBS 47. DATABASE TESTING Four stages of a DB test 1. Set up fixture 2. Exercise System Under Test 3. Verify outcome 4. Teardown 48. DATABASE TESTING (CONT) Give it a connection 49. DATABASE TESTING (CONT) Flat XML DataSet 50. DATABASE TESTING (CONT) 51. DATABASE TESTING (CONT) 52. GETTING STUCK IN 53. Demo: https://github.com/manatok/talk-demo-ci 54. db - Database Skel file and patches src - Project Code test/Output - Code coverage test/SportsBet - Projects Tests test/TestingCore - Uti l i ties tools - CI tools including ANT build file

Recommended

View more >