3

When testing with my SimpleTests, I want to inspect a variable. var_dump() and the likes are not being rendered in the output of Drush, Xdebug interactive debugger does not work due to the simulated browser being ran in DrupalWebTest.

How can I render, export, inspect or otherwise dump the contents of a variable in my code? I.e. the DrupalWebTest of dpm(), var_export() and such.

2
+100

The documentation for Troubleshooting Tests has a number of insights:

  1. Use debug() in Drupal 7. This will work during testing, and display the output right in your test results.
  2. Print them with a test that always passes (using something like $this->pass(var_export($var, TRUE));). It's a bit of a kludge, but it'll work in the absence of anything else.
4
  • debug() does nothing for me. Had to assert(TRUE – doublejosh Mar 11 '14 at 23:13
  • $this->pass() is functionally equivalent. Curious as to why this was downvoted. – David Watson Mar 13 '14 at 15:11
  • It doesn't work at all in my experience. – doublejosh Mar 14 '14 at 1:56
  • assertTrue(FALSE, $msg); will always be your friend though. – doublejosh Mar 28 '14 at 19:36
2

Call DrupalTestCase::assert() passing the message to show.

$this->assert(TRUE,
  t('Variable content: @variable', array('@variable' => var_export($variable, TRUE))
);

This is similar to the code used by DrupalTestCase::assertIdentical(). The difference is that in your case the first argument is TRUE because you are just outputting the content of a variable, which never fails.

protected function assertIdentical($first, $second, $message = '', $group = 'Other') {
  return $this->assert($first === $second, $message ? $message : t('Value @first is identical to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
}
0

Have you tried print_r($variable)? For a human friendly output you could try

print '<pre>';
print_r($variable);
print '</pre>';
1
  • 1
    obviously. But as stated, that output will be sent to the simulated browser and is not available on the CLI. – berkes Dec 10 '12 at 17:01
0

I can suggest 2 solutions

1) you can use watchdog.

watchdog($type, $message); Just put your variables with custom type name like:

watchdog('drush_test_run', print_r($message));

2) you can create some file log.txt and write variables into it with php file functions.

2
  • Watchdog won't work, because the database is wipte between all runs. Most test-suites will maintain the state (like the Database) after running tests; and only wipe everything clean just before running a test; not Drupal. The textfile would work, but in that case PHPs logs are a far better option. – berkes May 29 '13 at 20:26
  • yes, you are right – milkovsky Jun 2 '13 at 10:52
0

Any of these answers would be OK under most circumstances except that the problem I was having with a specific test case was do to the fact that I implemented an exec call and that function captures all output.

In my case I replaced the exec call with passthru and that fixed the issue for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.