31
Automated testing testing at every stage

Automated testing with Drupal

Embed Size (px)

DESCRIPTION

Presented at Drupal Camp Chicago 2012 Michelle Krejci details how Promet has used Jenkins, PHPUnit, and Selenium to automate our current continuous integration process so you can begin to start automating your QA testing today. She then outlines how Promet has begun to also include Phing and Chef to run PHPUnit tests on custom modules as part of test driven development. Finally, she looks at the challenges to running user acceptance tests on a Drupal installation and moving the Drupal community away from SimpleTest towards PHPUnit testing. In short, this is an overview of what works, what doesn't, and why this is important to the Drupal community.

Citation preview

Page 1: Automated testing with Drupal

Automated testingtesting at every stage

Page 2: Automated testing with Drupal

Exposition: characters

• Promet Source– www.prometsource.com

• Michelle Krejci– a.k.a. craychee– a.k.a. dev_meshev

Page 3: Automated testing with Drupal

Exposition: characters

• Selenium*– a.k.a. Selenium IDE– a.k.a. Selenium RC– a.k.a. Selenium

Webdriver

*Actually, these are not at all the same. To wit:Selenium IDE = development enviroment to create scriptsSelenium RC = Refers to Selenium 1 executableSelenium Webdriver = Selenium 2 executable

Page 4: Automated testing with Drupal

Exposition: characters

• PHPUnit– a.k.a. the testing standard

of the PHP community– a.k.a. the successor of

SimpleTest

Page 5: Automated testing with Drupal

Exposition: characters

• Jenkins– a.k.a. Hudson– a.k.a. A developer’s

most dependable butler

Page 6: Automated testing with Drupal

Exposition: characters

• Chef– a.k.a. An opscode

product– a.k.a. the answer to all

your server-configuration-related prayers

Page 7: Automated testing with Drupal

Exposition: characters

• xvfb– a.k.a. X virtual framer– a.k.a. what you need to

run Selenium on a headless

Page 8: Automated testing with Drupal

Exposition: characters

• Phing– a.k.a. Phing Is Not GNU make– a.k.a. Apache Ant for PHP– a.k.a. a build system for PHP– a.k.a. what you will need to

run deeper regression, metric, and user acceptance testing

Page 9: Automated testing with Drupal

Exposition: the setting

– During development– After every update– If someone somehow

has the time: on a regular basis as part of on-going support.

Lots of

clicking

Page 10: Automated testing with Drupal

Development: rising action

The Solution: Roll Call Testing

– Use Selenium scripts to verify that elements such as ids, divs, and text are present on a regular basis (e.g., after updates, during development, on a regular basis)

Page 11: Automated testing with Drupal

Development: rising action

Roll Call Tests– Each page gets its own test.– Each test must start and

end at the home page. • Verify that all blocks and links

are present using the VerifyElement command.

• Do not use “assert” or this will halt all tests if element or text is not present.

Page 12: Automated testing with Drupal

Development: rising action

Tools needed to write tests– Selenium IDE¹– PHPUnit bindings²– Xpath plug-in³

¹ Selenium IDE:– https://addons.mozilla.org/en-US/firefox/addon/

selenium-ide/

² PHP bindings:– https://addons.mozilla.org/en-US/firefox/addon/

selenium-ide-php-formatters/?src=ss

³ Xpath-checker add on:– https://addons.mozilla.org/en-US/firefox/addon/xpath-

checker/?src=search

Page 13: Automated testing with Drupal

Development: rising action

Selenium IDE1. Starting point.2. Controls3. Record actions (not needed for

creating roll call tests, recommend using right click and select command)

4. Name of tests (should be named after page)

5. The display6. View and edit selected

command7. Reference of commands

Page 14: Automated testing with Drupal

Development: rising action

Tools needed to run tests locally

– Selenium webdriver– PHP Unit– PHP Unit Selenium Exstentions

• Installing Selenium Webdriver:– http://www.danstraw.com/installing-selenium-

server-2-as-a-service-on-ubuntu/2010/09/23/

• Installing PHP Unit & Extensions:– http://www.phpunit.de/manual/current/en/

installation.html

Page 15: Automated testing with Drupal

Development: rising action

Start Selenium Webdriver:

In a new tab, run command phpunit*:

*In this example, I am using a bootstrap file. I have created a parent class that each page extends. In the parent class, I test the branding elements of each page.

Page 16: Automated testing with Drupal

Development: rising action

Now, let’s automate it:

Page 17: Automated testing with Drupal

Development: rising action

#!/user/bin/env ruby

current_dir = File.dirname(__FILE__)bootstrap_file = "MCMC.php” //if parent classbuild_dir = "build"

Dir[File.join(current_dir), '*'].each do |file| puts file if file != bootstrap_file and File.file?(file) cmd = "phpunit --log-junit #{build_dir}/#{file}.selenium.xml /*--bootstrap #{bootstrap_file} #{file}*/" print cmd system(cmd) endend

*No. It does not need to be ruby.

Add a ruby build script:*

Page 18: Automated testing with Drupal

Development: rising action

$git init newrepo

*Yes. Use git.

Create a git repo:*

Page 19: Automated testing with Drupal

Development: rising action

Configure Jenkins Server:– Add xvfb

There is a script in /root that has the commands to start the selenium service w/ Xvfb in a script named ‘start_selenium.sh’

– Install the following plugins• analysis-core• Analysis-collector• Checkstyle• Dry• Phing• Plot• pmd

Page 20: Automated testing with Drupal

Development: rising action

Configure Jenkins Job:– Specify source

Page 21: Automated testing with Drupal

Development: rising action

Configure Jenkins Job:– Specify triggers:

Page 22: Automated testing with Drupal

Development: rising action

Configure Jenkins Job:– Specify build command:

Page 23: Automated testing with Drupal

Development: rising action

Sit back and admire how clever you are:

Page 24: Automated testing with Drupal

Development: At this point you might start thinking:

Let’s automate everything!

Page 25: Automated testing with Drupal

Development: falling action

Continuous delivery*

*image from: http://en.wikipedia.org/wiki/Continuous_delivery

Page 26: Automated testing with Drupal

Development: falling actionTest-driven dev:– Build tests– Then modules

Page 27: Automated testing with Drupal

Development: falling action

abstract class DrupalTestCase extends PHPUnit_Framework_TestCase { }

*https://github.com/weitzman/upal

User acceptance tests that leverage Drupal*

Page 28: Automated testing with Drupal

Development: falling actionThere are people working on this:

Page 29: Automated testing with Drupal
Page 30: Automated testing with Drupal

Drupal culture

Phpunit needs to be leveraged for Drupal

understanding

Page 31: Automated testing with Drupal

Development: the resolution

• protected function drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'skip comment approval')) {

• // Create a role with the given permission set.• if (!($rid = $this->drupalCreateRole($permissions))) {• return FALSE;• }• // Create a user assigned to that role.• $edit = array();• $edit['name'] = $this->randomName();• $edit['mail'] = $edit['name'] . '@example.com';• $edit['roles'] = array($rid => $rid);• $edit['pass'] = user_password();• $edit['status'] = 1;•

$account = user_save(drupal_anonymous_user(), $edit);

• $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));

• if (empty($account->uid)) {• return FALSE;• }

• // Add the raw password so that we can log in as this user.• $account->pass_raw = $edit['pass'];• return $account;• }

Let’s make it work.