Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

Download from Wow! eBook <www.wowebook.com>

CHAPTER 25 TESTING

Continued

Function

Description

$this->xpath($xpath)

This function executes an XPath search on the

 

contents of the internal browser as defined in the

 

$xpath parameter.

$this->getAllOptions(SimpleXMLElement

This function returns all option elements,

$element)

including nested options, in a select. The $element

 

parameter defines which element to retrieve the

 

options for.

$this->drupalGetMails($filter = array())

This function returns an array that contains all of

 

the e-mails sent during the test case. You can filter

 

which e-mails are returned by defining key/value

 

pair filters.

$this->getSelectedItem(SimpleXMLElement

This function returns the value from the element

$element)

defined in the $element parameter.

Test Assertions

Assertions are functions that verify whether certain conditions are true—for example, that a field on the current page holds a specific value. You can think of assertions as the validation that the actions performed by the foregoing functions delivered the results that you expected. Each assertion either passes or fails based on whether what you are examining is true or false. Table 25-2 defines the assertion functions that you can use to verify your test results.

Table 25-2. Test Assertions

 

Assertion

Description

$this->assertTrue($result, $message = FALSE,

This function asserts that the variable $result

$group = ‘Other’)

resolves to true.

 

$age = is_integer(123);

 

$this->assertTrue($is_number, t(‘Make sure

 

that the person\’s age is an integer

 

value’));

$this->assertFalse($result, $message = ‘%’, $group

This function asserts that the variable $result

= ‘Other’)

resolves to false.

 

$foo = is_valid(‘foo’);

 

$this->assertFalse($valid, t(‘Make sure that

 

the foo is not a valid variable));

560

CHAPTER 25 TESTING

Assertion

Description

 

 

$this->assertNull($value, $message=’%’,

This function asserts that the variable $value

$group=’Other’)

resolves to NULL.

 

$result = load_my_object(-1);

 

$this->assertNull($result, t(‘Make sure we

 

get NULL when trying to load an invalid

 

object.’));

$this->assertNotNull($value, $message=’%s’,

This function asserts whether $value does not

$group=’Other)

resolve to NULL.

$this->assertEqual($first, $second, $message =

This function asserts whether $first is roughly

‘%s’, $group =’Other’)

equivalent (==) to $second.

$this->assertNotEqual($first, $second, $message =

This function asserts whether $first is not equal

‘%s’, $group=’Other’)

to $second (!=).

$this->assertIdentical($first, $second, $message =

This function asserts whether $first is identical

‘%s’, $group=’Other’)

(===) to $second.

$this->assertNotIdentical($first, $second,

This function asserts whether $first is not

$message = ‘%s’, $group=’Other’)

identical (!==) to $second.

$this->assertPattern($pattern, $message = ‘%s’,

This function asserts that the raw HTML content

$group = ‘Other’)

of the current page matches the regular expression

 

defined in $pattern.

$this->assertNoPattern($pattern, $message = ‘%s’,

This function asserts that the raw HTML content

$group = ‘Other’

of the current page does not match the regular

 

expression defined in $pattern.

$this->assertRaw($raw, $message=’%s’,

This function asserts that the HTML defined in the

$group=’Other’)

$raw parameter exists in the content on the current

 

page.

$this->assertNoRaw($raw, $message=’%s’,

This function asserts that the HTML defined in the

$group=’Other’)

$raw parameter does not exist in the content on

 

the current page.

$this->assertText($text, $message = ‘%s’,

This function asserts that the value stored in $text

$group=’Other’)

appears on the current page.

$this->assertNoText($text, $message = ‘%s’,

This function asserts that the value stored in $text

$group=’Other’)

does not appear on the current page.

561

CHAPTER 25 TESTING

Continued

Assertion

Description

 

 

$this->assertTitle($title, $message = ‘%s’,

This function asserts that the title defined in

$group=’Other’)

$title is found on the current page.

$this->assertNoTitle($title, $message = ‘%s’,

This function asserts that the title defined in

$group=’Other’)

$title is not found on the current page.

$this->assertUniqueText($text, $message=’%s’,

This function asserts that the text defined in $text

$group=’Other’)

appears once and only once on the current page.

$this->assertNoUniqueText($text, $message=’%s’,

This function asserts that the text defined in $text

$group=’Other’)

appears more than once on the current page.

$this->assertLink($label, $index = 0,

This function asserts that a link with the specified

$message=’%’, $group=’Other’)

text representation of the link ($link) exists on

 

the page. If there is more than one link on the

 

page with the same text representation, you can

 

use the $index parameter to specify which link

 

you wish to test.

$this->assertNoLink($label, $message=’%s’, $group=’Other’)

$this->assertLinkByHref($href, $index=0, $message=’%s’, $group=’Other’)

$this->assertNoLinkByHref($href, $message=’%s’, $group=’Other’)

$this->assertResponse($code, $message=’%s’)

This function asserts that no link with the specified label exists on the page.

This function asserts that a link with the given $href or partial $href exists on the page.

$this->assertLinkByHref('node/1', 0, 'A link to node 1 appears on the page');

This function asserts that no link with the given $href or partial $href exists on the page.

This function asserts that the HTTP response code for the current page matches the value assigned to $code.

$this->assertFieldById($id, $value=’ ’, $message=’%s’)

$this->assertNoFieldById($id, $value=’ ‘, $message= ‘%s’)

$this->assertFieldByName($name, $value = ‘ ‘, $message = ‘%s’)

This function asserts that a field exists on the current page with the given ID and value.

This function asserts that a field does not exist on the current page with the given ID and value.

This function asserts that a field exists on the current page with the given name and value.

562

CHAPTER 25 TESTING

Assertion

Description

 

 

$this->assertNoFieldByName($name, $value = ‘ ‘, $message = ‘%s’)

$this->assertFieldChecked($id, $message = ‘%s’)

$this->assertNoFieldChecked($id, $message = ‘%s’)

$this->assertOptionSelected($id, $option, $message = ‘%s’)

$this->assertNoOptionSelected($id, $option, $message = ‘%s’)

$this->assertFieldByXPatch($xpath, $value, $message = ‘%s’, $group = ‘Other’)

$this->assertNoFieldByXPath($xpath, $value, $message = ‘%s’, $group = ‘Other’)

$this->assertNoDuplicateIds($messsage = ‘%s’, $group = ‘Other’)

$this->pass($message = ‘%s’, $group = ‘Other’)

This function asserts that a field does not exist on the current page with the given name and value.

This function asserts that the check box with the given ID exists on the current page and is checked.

This function asserts that the check box with the given ID exists on the current page but is not checked.

This function asserts that a select list with the given ID exists on the current page with the identified option selected.

This function asserts that a select list with the given ID exists on the current page and the identified option is not selected.

This function asserts that a field exists in the current page by the given XPath.

This function asserts that a field does not exist on the current page by the given XPath.

This function asserts that each HTML ID on the page is used for just one element.

This function makes an assertion that is always positive.

$this->fail($message = ‘%s’, $group = ‘Other’)

This function makes an assertion that is always negative.

$this->error($message = ‘%s’, $group = ‘Other’)

This function makes an assertion that always yields an error condition.

Summary

The techniques and tools outlined in this chapter provide you with everything you need to set up a suite of repeatable tests for your new site. While it may seem time-consuming, and it is, the benefits of taking the time to sit down, document, and implement tests using the tools defined in this chapter will pay back manyfold as you make changes to your site and need to validate that a change in one section of your site didn’t impact functionality elsewhere. As you saw in the example I was able to execute 241 tests in under 2 minutes on the Blog module using the testing framewor, just try doing that manually, over and over again while sitting down at a browser.

563

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]