OLD | NEW |
1 # Writing Layout Tests | 1 # Writing Layout Tests |
2 | 2 |
3 _Layout tests_ is a bit of a misnomer. This term is | 3 _Layout tests_ is a bit of a misnomer. This term is |
4 [a part of our WebKit heritage](https://webkit.org/blog/1452/layout-tests-theory
/), | 4 [a part of our WebKit heritage](https://webkit.org/blog/1452/layout-tests-theory
/), |
5 and we use it to refer to every test that is written as a Web page (HTML, SVG, | 5 and we use it to refer to every test that is written as a Web page (HTML, SVG, |
6 or XHTML) and lives in | 6 or XHTML) and lives in |
7 [third_party/WebKit/LayoutTests/](../../third_party/WebKit/LayoutTests). | 7 [third_party/WebKit/LayoutTests/](../../third_party/WebKit/LayoutTests). |
8 | 8 |
9 [TOC] | 9 [TOC] |
10 | 10 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 specification URL (in `<link rel="help">`) is almost always relevant, and is | 331 specification URL (in `<link rel="help">`) is almost always relevant, and is |
332 incredibly helpful to a developer who needs to understand the test quickly. | 332 incredibly helpful to a developer who needs to understand the test quickly. |
333 | 333 |
334 Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, | 334 Below is a skeleton for a JavaScript test embedded in an HTML page. Note that, |
335 in order to follow the minimality guideline, the test omits the tags `<html>`, | 335 in order to follow the minimality guideline, the test omits the tags `<html>`, |
336 `<head>`, and `<body>`, as they can be inferred by the HTML parser. | 336 `<head>`, and `<body>`, as they can be inferred by the HTML parser. |
337 | 337 |
338 ```html | 338 ```html |
339 <!doctype html> | 339 <!doctype html> |
340 <meta charset="utf-8"> | 340 <meta charset="utf-8"> |
341 <title>JavaScript: the true literal</title> | |
342 <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> | 341 <link rel="help" href="https://tc39.github.io/ecma262/#sec-boolean-literals"> |
343 <meta name="assert" value="The true literal is equal to itself and immutable"> | 342 <meta name="assert" value="The true literal is equal to itself and immutable"> |
344 <script src="/resources/testharness.js"></script> | 343 <script src="/resources/testharness.js"></script> |
345 <script src="/resources/testharnessreport.js"></script> | 344 <script src="/resources/testharnessreport.js"></script> |
346 <script> | 345 <script> |
347 'use strict'; | 346 'use strict'; |
348 | 347 |
349 // Synchronous test example. | 348 // Synchronous test example. |
350 test(() => { | 349 test(() => { |
351 const value = true; | 350 const value = true; |
(...skipping 16 matching lines...) Expand all Loading... |
368 }).then(value => { | 367 }).then(value => { |
369 assert_true(value); | 368 assert_true(value); |
370 }); | 369 }); |
371 }, 'The literal true used to resolve a Promise'); | 370 }, 'The literal true used to resolve a Promise'); |
372 | 371 |
373 </script> | 372 </script> |
374 ``` | 373 ``` |
375 | 374 |
376 Some points that are not immediately obvious from the example: | 375 Some points that are not immediately obvious from the example: |
377 | 376 |
378 * The `<meta name="assert">` describes the purpose of the entire file, and | 377 * The `<meta name="assert">` describes the purpose of the entire file. |
379 is not redundant to `<title>`. Don't add a `<meta name="assert">` when the | |
380 information in the `<title>` is sufficient. | |
381 * When calling an `assert_` function that compares two values, the first | 378 * When calling an `assert_` function that compares two values, the first |
382 argument is the actual value (produced by the functionality being tested), and | 379 argument is the actual value (produced by the functionality being tested), and |
383 the second argument is the expected value (known good, golden). The order | 380 the second argument is the expected value (known good, golden). The order |
384 is important, because the testing harness relies on it to generate expressive | 381 is important, because the testing harness relies on it to generate expressive |
385 error messages that are relied upon when debugging test failures. | 382 error messages that are relied upon when debugging test failures. |
386 * The assertion description (the string argument to `assert_` methods) conveys | 383 * The assertion description (the string argument to `assert_` methods) conveys |
387 the way the actual value was obtained. | 384 the way the actual value was obtained. |
388 * If the expected value doesn't make it clear, the assertion description | 385 * If the expected value doesn't make it clear, the assertion description |
389 should explain the desired behavior. | 386 should explain the desired behavior. |
390 * Test cases with a single assertion should omit the assertion's description | 387 * Test cases with a single assertion should omit the assertion's description |
391 when it is sufficiently clear. | 388 when it is sufficiently clear. |
392 * Each test case describes the circumstance that it tests, without being | 389 * Each test case describes the circumstance that it tests, without being |
393 redundant. | 390 redundant. |
394 * Do not start test case descriptions with redundant terms like "Testing" | 391 * Do not start test case descriptions with redundant terms like "Testing" |
395 or "Test for". | 392 or "Test for". |
396 * Test files with a single test case should omit the test case description. | |
397 The file's `<title>` should be sufficient to describe the scenario being | |
398 tested. | |
399 * Asynchronous tests have a few subtleties. | 393 * Asynchronous tests have a few subtleties. |
400 * The `async_test` wrapper calls its function with a test case argument that | 394 * The `async_test` wrapper calls its function with a test case argument that |
401 is used to signal when the test case is done, and to connect assertion | 395 is used to signal when the test case is done, and to connect assertion |
402 failures to the correct test. | 396 failures to the correct test. |
403 * `t.done()` must be called after all the test case's assertions have | 397 * `t.done()` must be called after all the test case's assertions have |
404 executed. | 398 executed. |
405 * Test case assertions (actually, any callback code that can throw | 399 * Test case assertions (actually, any callback code that can throw |
406 exceptions) must be wrapped in `t.step_func()` calls, so that | 400 exceptions) must be wrapped in `t.step_func()` calls, so that |
407 assertion failures and exceptions can be traced back to the correct test | 401 assertion failures and exceptions can be traced back to the correct test |
408 case. | 402 case. |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 manual steps to a minimum, and having simple and clear instructions that | 503 manual steps to a minimum, and having simple and clear instructions that |
510 describe all the configuration changes and user gestures that match the effect | 504 describe all the configuration changes and user gestures that match the effect |
511 of the Blink-specific APIs used by the test. | 505 of the Blink-specific APIs used by the test. |
512 | 506 |
513 Below is an example of a fairly minimal test that uses a Blink-Specific API | 507 Below is an example of a fairly minimal test that uses a Blink-Specific API |
514 (`window.eventSender`), and gracefully degrades to a manual test. | 508 (`window.eventSender`), and gracefully degrades to a manual test. |
515 | 509 |
516 ```html | 510 ```html |
517 <!doctype html> | 511 <!doctype html> |
518 <meta charset="utf-8"> | 512 <meta charset="utf-8"> |
519 <title>DOM: Event.isTrusted for UI events</title> | |
520 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted"> | 513 <link rel="help" href="https://dom.spec.whatwg.org/#dom-event-istrusted"> |
521 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events"> | 514 <link rel="help" href="https://dom.spec.whatwg.org/#constructing-events"> |
522 <meta name="assert" | 515 <meta name="assert" |
523 content="Event.isTrusted is true for events generated by user interaction"> | 516 content="Event.isTrusted is true for events generated by user interaction"> |
524 <script src="../../resources/testharness.js"></script> | 517 <script src="../../resources/testharness.js"></script> |
525 <script src="../../resources/testharnessreport.js"></script> | 518 <script src="../../resources/testharnessreport.js"></script> |
526 | 519 |
527 <p>Please click on the button below.</p> | 520 <p>Please click on the button below.</p> |
528 <button>Click Me!</button> | 521 <button>Click Me!</button> |
529 | 522 |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 * The `http/` directory hosts tests that require an HTTP server (see above). | 874 * The `http/` directory hosts tests that require an HTTP server (see above). |
882 * The `resources/` subdirectory in every directory contains binary files, such | 875 * The `resources/` subdirectory in every directory contains binary files, such |
883 as media files, and code that is shared by multiple test files. | 876 as media files, and code that is shared by multiple test files. |
884 | 877 |
885 *** note | 878 *** note |
886 Some layout tests consist of a minimal HTML page that references a JavaScript | 879 Some layout tests consist of a minimal HTML page that references a JavaScript |
887 file in `resources/`. Please do not use this pattern for new tests, as it goes | 880 file in `resources/`. Please do not use this pattern for new tests, as it goes |
888 against the minimality principle. JavaScript and CSS files should only live in | 881 against the minimality principle. JavaScript and CSS files should only live in |
889 `resources/` if they are shared by at least two test files. | 882 `resources/` if they are shared by at least two test files. |
890 *** | 883 *** |
OLD | NEW |