- Create a new PHP class that defines a test
- Provide Drupal with meta-data about your test
- Run your test to see if it works
Background
Hello World
- Start by creating a simple helloworld module directory in sites/all/modules/custom/helloworld/
Add a helloworld.info file with the following contents:
name = Hello World description = An example module to demonstrate using SimpleTest core = 7.x
Add a helloworld.module file with the following contents:
<?php /** * @file * Hello World demonstrates the use of SimpleTest for Drupal 7. */ /** * Implements hook_menu(). */ function helloworld_menu() { $items = array(); $items['helloworld'] = array( 'title' => 'Hello World', 'access callback' => TRUE, 'page callback' => 'helloworld_hello_page', 'type' => MENU_NORMAL_ITEM, 'menu' => 'navigation', ); return $items; } /** * Page callback for /helloworld. */ function helloworld_hello_page() { return t('Hello World. Welcome to Drupal.'); }
- In the Administration menu, go to Modules (admin/modules).
- Enable the Hello World module.
- In the navigation menu navigate to Hello World (helloworld), where you should now see the text "Hello World: Welcome to Drupal." displayed on the page.
Extending DrupalWebTestCase
- Start by creating a new file: helloworld/tests/helloworld.test, we will add the code that makes up our test case to this file.
- All functional tests in Drupal should extend the DrupalWebTestCase class which will provide the utility functions used to drive the test browser, helper functions for interacting with a Drupal application, and all the assertions we will use to perform our actual tests.
Add the following to your helloworld.test file:
<?php /** * @file * Tests for the hello world module. */ class HelloworldTests extends DrupalWebTestCase { }
Add a getInfo() method to your HelloworldTests class. This method returns an array that provides meta-data about our test that the SimpleTest module can use when displaying our test in the UI. This method is required in order for your test to work.
/** * Metadata about our test case. */ public static function getInfo() { return array( // The human readable name of the test case. 'name' => 'Hello World', // A short description of the tests this case performs. 'description' => 'Tests for the Hello World module.', // The group that this case belongs too, used for grouping tests together // in the UI. 'group' => 'Hello World Group', ); }
Add a setUp() method to HelloworldTests. This method is called when the system is preparing the environment for running our tests. It allows us to perform any setup steps required. In this case, we need to ensure that our module is enable during setup so that the page we want to test is available. We can do this by calling the parent classes's setUp() method and passing it an array of modules we want enabled.
/** * Perform any setup tasks for our test case. */ public function setUp() { parent::setUp(array('helloworld')); }
Add a test*() method to our class. Each test case can have one or more methods whose names are prefixed with the string test. Each of these methods will be automatically called by the SimpleTest test runner, and should contain the various assertions required in order to demonstrate that this test is passing. This example will use the DrupalWebTestCase::drupalGet() method in order to tell the internal browser to navigate to the URL /helloworld. And then use the DrupalWebTestCase::assertText() method to verify that the string, "Hello World ...", exists somewhere in the HTML output of the page the internal browser is currently looking at.
public function testHelloWorld() { // Navigate to /helloworld. $this->drupalGet('helloworld'); // Verify that the text "Hello World ...", exists on the page. $this->assertText('Hello World. Welcome to Drupal.', 'The page content is present.'); }
Finally, we need to tell Drupal about our new test. Update your helloworld.info file and add the following line so that after clearing the cache the registry will contain a reference to our new HelloworldTests class.
files[] = tests/helloworld.test
Run your tests
- Clear the cache so Drupal locates our new test(s). Navigate to Configuration > Performance (admin/config/development/performance) in the Toolbar, and then clicking the "Clear all caches" button.
- Navigate to Modules (admin/modules) in the Toolbar. Enable the Testing module in the Core section.
- Navigate to Configuration > Testing (admin/config/development/testing).
- Select the checkbox for the "Hello World" test to run all tests in this group, or alternately tip the drop down open and choose the individual test case you want to run.
- Click the "Run tests" button.
- A new page will open and display a progress bar while your tests run.
- Once completed, you will be directed to a page with a table that shows the results of your tests.
Summary
Source: https://goo.gl/MGzNMi
We do Drupal development
Go to our Drupal page!