Introduction to unit testing CiviCon London 2013

592 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
592
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to unit testing CiviCon London 2013

  1. 1. 4 Oct 2013 CiviCon London 2013 An introduction to unit testing
  2. 2. WHO AM I? •Erawat Chamanont •Technical Lead, Compucorp (UK) Ltd. •CiviBooking Lead Developer.
  3. 3. ABOUT US Open source software consultancy •End to end IT consultancy specialising in open source software and solutions •Our mission is to raise awareness of the advantages of selecting open source solutions •Supplying a diverse range of organisations across 3 main market sectors: •Non-profit/trade association •Government •Small business *
  4. 4. OVERVIEW •What is unit testing? •What can we do with CiviCRM? •How to set up? •How does it work? •Convention? •Create the test class •Create test function •Run test and see the result •Web test •Jenkins is awesome •Question *
  5. 5. WHY UNIT TESTING? •Test become automated. •Easier to change and refactor code. •Tests a single behaviour. •Save the development time (debug, isolate bugs). •Code documentation. •Test driven development (TDD) •It is FUN! yeah. *
  6. 6. WHAT CAN WE TEST WITH CIVICRM? •CiviCRM Hooks •Business logic/function •APIs •Web Test
  7. 7. HOW TO SETUP? 1. Install CiviCRM from Github. http://wiki.civicrm.org/confluence/display/CRMDOC/GitHub+for+CiviCRM http://wiki.civicrm.org/confluence/display/CRMDOC/Git+Version+Control+for+CiviCRM 2. Create a copy of civicrm database into new separate test database (it is conventionally called “civicrm_tests_dev”). 3. Create a database connection. (the civicrm setting file doesn't apply here). a. Using ~/.my.cnf b. or Config the PHP constant CIVICRM_DSN (located at civicrm/tests/phpunit/CiviTest/civicrm.settings.local.php) c. or pass the MySQL connection detail each time you run the unitest 4. Set Up Civix. a. https://github.com/totten/civix 5. Make sure Civix is linked to CiviCRM installation a. civix civicrm:ping http://wiki.civicrm. org/confluence/display/CRM/Setting+up+your+personal+testing+sandbox+HOWTO
  8. 8. HOW DOES IT WORK? Steps of writing unit test? 1. Understand test standards & the helper functions e.g assertion, CiviCRM APIs test suite. 2. Know what should be tested (test scenario) 3. Preparing test data set (expected input/output) 4. Writing the test * *
  9. 9. CONVENTION? •Test case needs to extend “CiviUnitTestCase”. •Class name must end with word “Test” e.g EntityTest •Method name starts with test and use camel case e.g testCreateWithoutTitle(). http://wiki.civicrm. org/confluence/display/CRM/CiviCRM+Unit+Testing+basic+informatio n * *
  10. 10. CREATE THE TEST CLASS civix generate:test CRM_Booking_BAO_EntityTest * *
  11. 11. RUN TEST AND TEST RESULT? Run test: civix test CRM_Booking_BAO_EntityTest Test Result:
  12. 12. WEB TEST? ● CiviCRM uses Selenium for Web Test ● Class must extends CiviSeleniumTestCase http://wiki.civicrm. org/confluence/display/CRM/Setting+yourself+up+to+wor k+with+Selenium+tests
  13. 13. JENKINS IS AWESOME https://test.civicrm.org/
  14. 14. Any questions?
  15. 15. Talk to me: •Twitter: @erawat •Linkedin: http://www.linkedin.com/in/erawat •Email: erawat@compucorp.co.uk

×