Introduction to unit testing CiviCon London 2013

Post on 07-Jul-2015

438 views 0 download

Transcript of Introduction to unit testing CiviCon London 2013

CiviCon London 2013

4 Oct 2013

An introduction to unit testing

WHO AM I?

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

•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

*

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

*

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.

WHAT CAN WE TEST WITH CIVICRM?

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

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

* *

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

* *

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

* *

CREATE THE TEST CLASS

civix generate:test CRM_Booking_BAO_EntityTest

RUN TEST AND TEST RESULT?

Run test:

Test Result:

civix test CRM_Booking_BAO_EntityTest

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

JENKINS IS AWESOME

https://test.civicrm.org/

Any questions?

Talk to me:

•Twitter: @erawat•Linkedin: http://www.linkedin.com/in/erawat•Email: erawat@compucorp.co.uk