TDD — how to test the HTML output? The regexps?!

0 like 0 dislike
Inspired by the topic "I do not write unit tests because ... "excuses". I decided to try to use TDD.

There is a MVC web application. Assume that the model and controller tests are covered. But how to test view function, templates, layouty, which is a direct output to stdin. It is clear that the output can be intercepted using ob_*, but here is how to test all HTML code (native PHP templates)?

I tried this:
On the main page should be a link on the user/new (registration). Written test (PHPUnit):
 testRegisterLinkForAnonymousPresent function() { $app = new App(); $this->expectOutputRegex("#.+#"); $app->run(); }

Set up the output of this link to the main — test passed

The next step is for /user/new should display a registration form with the action /user and the post method. Written test:
 testRegisterFormPresentAndValid function() { $app = new App(); $request = new Request('/user/new'); $app->setRequest($request); $this->expectOutputRegex("##"); $app->run(); }

Made form, the test is passed, but then I noticed that was a typo in the template

He began to think of the regular season and realized that it is wrong in principle, attributes can be in a different order after the form required whitespace, etc. began to draw a two-storey, then she slowly began to flow into the three-story. But still did not reach field validation, the order of which can also be arbitrary. Actually here I started to Google how to do, learned a lot about TDD, but specifically on a subject found nothing really.

Prompt, please, where to dig?

The regexps, IMHO, is not really to test complex structures. While two options:

— Check the tags not entirely, and in parts: in one test/assertions to verify that there is a form tag in principle, the second that it has an action attribute with the desired value, in the third, method is POST, the fourth that there is an input tag inside a form. To avoid three-storey expressions in parallel with the test cut the required pieces, and then to check only them, for example "#(.*)#"
— To parse the HTML and check the tags/attributes/values is already in the tree in object form.

PS This is not functional testing, in spite of what seems to be verified by the entire application, just by coincidence, the refactoring is not done to check the template separately — you need to check it work unit, the result of which is the HTML code for the right combination of tags/attributes/values/text.

In the process of picking the Zend found almost undocumented features of PHPUnit assertSelectCount(), assertSelectEquals, assertSelectRegExp(), the test now looks like this:
 testRegisterLinkForAnonymousPresent function() { ob_start(); $app = new App(); $app->run(); $result = ob_get_contents(); ob_end_clean(); $this->assertSelectEquals('a[href="/user/new"]', 'Register', 1, $result); }

Probably need to extend a class PHPUnit_Extensions_OutputTestCase to get rid of ob_*
by | 11 views

5 Answers

0 like 0 dislike
DOM methods, CSS/XPath selectors. The regular season is not suitable for parsing html, it is no longer news.
0 like 0 dislike
Can the validator check, then a school would show.
0 like 0 dislike
you can use selenium it can.
When used ZendFramework, then used the Zend_Dom_Query component
it can also be used separately from ZF, supports xpath and css queries, it is very convenient regarding regexps.
0 like 0 dislike
MAXIMO at separating layout from code, preferably using some template engine instead of the native php.
Then the formation of a html-code based on the input data will correspond to simple logic, which test is not particularly necessary.
Testing for the formation of these inputs.
0 like 0 dislike
You can consider md5 sums of the entire page or its separate parts, comparing with the standard.

Related questions

0 like 0 dislike
2 answers
asked Mar 25, 2019 by masterOk
0 like 0 dislike
2 answers
asked Apr 13, 2019 by flapflapjack
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
asked Apr 23, 2019 by nordwind2013
110,608 questions
257,186 answers
28,757 users