4 Oct 2013
CiviCon London 2013
An introduction to unit testing
WHO AM I?
•Technical Lead, Compucorp (UK) Ltd.
•CiviBooking Lead Developer.
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
•Supplying a diverse range of organisations across 3 main market sectors:
•What is unit testing?
•What can we do with CiviCRM?
•How to set up?
•How does it work?
•Create the test class
•Create test function
•Run test and see the result
•Jenkins is awesome
WHY UNIT TESTING?
•Test become automated.
•Easier to change and refactor code.
•Tests a single behaviour.
•Save the development time (debug, isolate bugs).
•Test driven development (TDD)
•It is FUN! yeah.
WHAT CAN WE TEST WITH CIVICRM?
HOW TO SETUP?
1. Install CiviCRM from Github.
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
c. or pass the MySQL connection detail each time you run the
4. Set Up Civix.
5. Make sure Civix is linked to CiviCRM installation
a. civix civicrm:ping
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
•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
CREATE THE TEST CLASS
civix generate:test CRM_Booking_BAO_EntityTest
RUN TEST AND TEST RESULT?
civix test CRM_Booking_BAO_EntityTest
● CiviCRM uses Selenium for Web Test
● Class must extends CiviSeleniumTestCase