Unit tests for HTML output?

It may be a dumb question, but do you do unit tests to output the HTML of your PHP functions / scripts?

I'm trying to keep my HTML and my PHP separate - that is, HTML includes placeholders and functions for certain repeating elements (tabular data / any kind of loop output), but I'm not sure how to do this.

Is there a standard way to solve such problems, or is it mainly a matter of using standard unit tests for functions that create inserted content, and then to make sure that it looks correct in the / W 3C Validator browser?

Thank.

Edit: I think the consequence of this would be: are there any similar tests that even deserve attention? If you correctly separate your content and structure, then you really will only test a few inclusions in very limited scenarios (presumably, anyway). Does it really stand on the full pages of full hands to have a file for comparison?

+12
source share
8 answers

Based on my experience in testing HTML, I now follow these three basic rules:

1. Do not check the HTML result for the correct template. You modify the HTML output too often, and you end up wasting time saving your tests.

2. HTML. HTML ( HTML, ), HTML . : , , - HTML. , № 1.

3. , HTML. HTML, , , . , .

PHP HTML5. Validator.nu. PHPUnit .

, :

$validator=new HTML5Validate();

// Validate (returns TRUE or FALSE)
$result=$validator->Assert('<p>Hello World</p>'); 

// Get explanation of what wrong (if validation failed)
print $validator->message; 
+7

HTML . , PHP, , , .

, , , .

, for , , Separation of Concerns, for - . , for , for.

, , . , , .

HTML, .

- , , .

.

  • .
  • (1) .

unit test, , [ ] .

+4
+1

SimpleTest framework , PhpUnit . , , .

:

  • , . , , , . , .
  • HTML- , . $webTestCase->assertText('...'); $webTestCase->assertPattern('/.../');.

. , . PHP , , , .

public static function openPageWithNoWarnings($webTestCase, $page, $landingPage = null)
{
  // check that page can be opened successfully
  $webTestCase->assertTrue($webTestCase->get($page));

  // check that there are no PHP warnings
  $webTestCase->assertNoPattern('/(warning:|error:)/i', 'PHP error or warning on page!');

  // check if landed on expected page (maybe a redirect)
  if (!empty($landingPage))
  {
    $url = $webTestCase->getUrl();
    $file = basename(parse_url($url, PHP_URL_PATH));
    $webTestCase->assertEqual($page, $file,
      sprintf('Expected page "%s", got page "%s".',  page, $file));
  }
}

, , , - , .

+1

. , , - phpQuery, . , h3 ~ - ~. div , - ie6, .

, .

0

(, CakePHP Helpers) HTML. , HTML . .

PHPUnit assertTag() .

, ; , unit , -. (Selenium), , .

0

.

,

ob_start();
function_which_produces_some_output();
$this->assertEquals( ob_get_clean(), '<p>Expected Output Here</p>');
0

Source: https://habr.com/ru/post/1784697/


All Articles