Introduction to unit testing CiviCon London 2013

15
CiviCon London 2013 4 Oct 2013 An introduction to unit testing

Transcript of Introduction to unit testing CiviCon London 2013

Page 1: Introduction to unit testing   CiviCon London 2013

CiviCon London 2013

4 Oct 2013

An introduction to unit testing

Page 2: Introduction to unit testing   CiviCon London 2013

WHO AM I?

•Erawat Chamanont•Technical Lead, Compucorp (UK) Ltd.•CiviBooking Lead Developer.

Page 3: Introduction to unit testing   CiviCon London 2013

•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

*

Open source software consultancy

ABOUT US

Page 4: Introduction to unit testing   CiviCon London 2013

*

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

Page 5: Introduction to unit testing   CiviCon London 2013

*

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.

Page 6: Introduction to unit testing   CiviCon London 2013

WHAT CAN WE TEST WITH CIVICRM?

•CiviCRM Hooks•Business logic/function•APIs•Web Test

Page 7: Introduction to unit testing   CiviCon London 2013

HOW TO SETUP?

1. Install CiviCRM from Github.http://wiki.civicrm.org/confluence/display/CRMDOC/GitHub+for+CiviCRMhttp://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 installationa. civix civicrm:ping

http://wiki.civicrm.org/confluence/display/CRM/Setting+up+your+personal+testing+sandbox+HOWTO

Page 8: Introduction to unit testing   CiviCon London 2013

* *

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

Page 9: Introduction to unit testing   CiviCon London 2013

* *

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+information

Page 10: Introduction to unit testing   CiviCon London 2013

* *

CREATE THE TEST CLASS

civix generate:test CRM_Booking_BAO_EntityTest

Page 11: Introduction to unit testing   CiviCon London 2013

RUN TEST AND TEST RESULT?

Run test:

Test Result:

civix test CRM_Booking_BAO_EntityTest

Page 12: Introduction to unit testing   CiviCon London 2013

WEB TEST?

● CiviCRM uses Selenium for Web Test● Class must extends CiviSeleniumTestCase

http://wiki.civicrm.org/confluence/display/CRM/Setting+yourself+up+to+work+with+Selenium+tests

Page 13: Introduction to unit testing   CiviCon London 2013

JENKINS IS AWESOME

https://test.civicrm.org/

Page 14: Introduction to unit testing   CiviCon London 2013

Any questions?