Unit tests for HTML Output?

后端 未结 9 1209
-上瘾入骨i
-上瘾入骨i 2021-02-19 00:39

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

I try to keep my HTML and my PHP separate - i.e. HTML includes

9条回答
  •  梦如初夏
    2021-02-19 01:22

    I found the SimpleTest framework to be very useful, usually i use it for integration-tests and PhpUnit for unit-tests. They spare me a lot of manually submitted formulars, which i would do otherwise over and over again.

    It became my habit to follow this points, when doing such integrations tests:

    1. Try not to repeat tests that are already done with real unit-tests. If for example you have a unit-tested validating function for email addresses, it doesn't make sense to submit all kind of invalid email addresses. Only check once if you are redirected with an error message.
    2. Do not compare the resulting HTML with a complete reference output, you would have to update your tests with every redesign of your pages. Instead check only crucial parts with $webTestCase->assertText('...'); or $webTestCase->assertPattern('/.../');.

    With some tiny helper functions, you can gain a lot of robustness. The following function will open a page and checks if the page was opened successfully without warnings. Since there is no compiler for PHP that can give out warnings at design time, you can at least make sure that your code will not produce errors or warnings.

    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));
      }
    }
    

    Such tests will give you not much of work, you can start with very light tests, but they give you instantly feedback if something fails, with only one mouse click.

提交回复
热议问题