function TestBase::assert

Internal helper: stores the assert.

Parameters

$status: Can be 'pass', 'fail', 'exception', 'debug'. TRUE is a synonym for 'pass', FALSE for 'fail'.

string|\Drupal\Component\Render\MarkupInterface $message: (optional) A message to display with the assertion. Do not translate messages: use \Drupal\Component\Render\FormattableMarkup to embed variables in the message text, not t(). If left blank, a default message will be displayed.

$group: (optional) The group this message is in, which is displayed in a column in test output. Use 'Debug' to indicate this is debugging output. Do not translate this string. Defaults to 'Other'; most tests do not override this default.

$caller: By default, the assert comes from a function whose name starts with 'test'. Instead, you can specify where this assert originates from by passing in an associative array as $caller. Key 'file' is the name of the source file, 'line' is the line number and 'function' is the caller function itself.

18 calls to TestBase::assert()
AggregatorTestBase::createFeed in core/modules/aggregator/src/Tests/AggregatorTestBase.php
Creates an aggregator feed.
GenericCacheBackendUnitTestBase::testDelete in core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
Tests Drupal\Core\Cache\CacheBackendInterface::delete().
GenericCacheBackendUnitTestBase::testGetMultiple in core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
GenericCacheBackendUnitTestBase::testSetGet in core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
GenericCacheBackendUnitTestBase::testValueTypeIsKept in core/modules/system/src/Tests/Cache/GenericCacheBackendUnitTestBase.php
Tests data type preservation.

... See full list

File

core/modules/simpletest/src/TestBase.php, line 328

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

protected function assert($status, $message = '', $group = 'Other', array $caller = NULL) {
    if ($message instanceof MarkupInterface) {
        $message = (string) $message;
    }
    // Convert boolean status to string status.
    if (is_bool($status)) {
        $status = $status ? 'pass' : 'fail';
    }
    // Increment summary result counter.
    $this->results['#' . $status]++;
    // Get the function information about the call to the assertion method.
    if (!$caller) {
        $caller = $this->getAssertionCall();
    }
    // Creation assertion array that can be displayed while tests are running.
    $assertion = [
        'test_id' => $this->testId,
        'test_class' => get_class($this),
        'status' => $status,
        'message' => $message,
        'message_group' => $group,
        'function' => $caller['function'],
        'line' => $caller['line'],
        'file' => $caller['file'],
    ];
    // Store assertion for display after the test has completed.
    $message_id = $this->storeAssertion($assertion);
    $assertion['message_id'] = $message_id;
    $this->assertions[] = $assertion;
    // We do not use a ternary operator here to allow a breakpoint on
    // test failure.
    if ($status == 'pass') {
        return TRUE;
    }
    else {
        if ($this->dieOnFail && ($status == 'fail' || $status == 'exception')) {
            exit(1);
        }
        return FALSE;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.