Changes between Version 25 and Version 26 of Writing Layout Tests for DumpRenderTree

Oct 27, 2011 4:49:11 PM (13 years ago)

How to write good test case (edit 1), mostly text cases and 'do you need a pixel test'


  • Writing Layout Tests for DumpRenderTree

    v25 v26  
    3535'''Note:''' You get more points if your test works in ANY browsers.
    37 === fast ===
     37=== Fast ===
    3939WebKit has several thousands layout tests that are run in parallel. If your layout test take a lot time, it will slow down everyone's testing. Also DumpRenderTree has a default timer of several seconds before it considers that a test timed out. '''This is true even if you are using waitUntilDone / notifyDone.'''
    4747=== Understandable & clear ===
    49 ''FIXME: Should have some tips here about how to make a test which is easy to know when it succeeds, things like a 100x100 green block, just printing SUCCESS or FAILURE, and using `dumpAsText()`.''
     49A good reading on how to write awesome test cases is the [ CSS2.1 Test Case Authoring Guidelines]. Especially read the ''section: 4. Writing ideal tests''
     51Now in the context of WebKit, test should at least indicate:
     52 * which bug it belongs to. That means the bug number or the bugzilla URL (better as you can paste it in a browser) but also the bug title to see what is tested.
     53 * what is the condition for success or failure.
     55Bad test:
     59    .block { color: red; }
     61<p class="block"></p>
     64This is bad because it misses all the WebKit information but also because it uses '''red''' when it means passing. Red should be reserved for failures.
     66'''Note:''' This test is badly formatted and if this is not something your test needs, it is better to avoid it.
     68Better test:
     71<!DOCTYPE html>
     75    .block { color: green; }
     79<p> Bug <a href="">XXXX</a>: WebKit does not apply class-selector</p>
     80<p> For this test to pass, you should see a green rectangle below. </p>
     81<p class="block"></p>
     86This test is a huge improvement but we can actually go one step further! (see the next section about writing pixel tests on that)
    5288One last comment (FIXME: move it to a better section):
    5490 * Tests should not access the Internet. Avoid `http:` URLs in `src` and `href` attributes, in CSS properties and in XMLHttpRequest. Testing WebKit's network layer should be done using the HTTP test facility, to be described below.
    5792== Pixel test tips ==
     94=== Do you really need a pixel test? ===
     96That should be your first question. Pixel tests are a burden on every port as any small chance in the engine could lead to your test needing a new pixel result. That does not mean that pixel tests are bad, they are invaluable for catching visual regressions but there are way to avoid dumping the pixel while checking the behavior you need!
     98Let's take the example from the previous section and make it better.
     100Even better test:
     103<!DOCTYPE html>
     107    .block { color: green; }
     110if (window.layoutTestController)
     111   layoutTestController.dumpAsText();
     113function log(message)
     115    var console = document.getElementById('console');
     116    console.appendChild(document.createTextNode(message);
     117    console.appendChild(document.createElement('br'));
     120function checkBlockColor()
     122    var block = document.getElementsByClassName('block')[0];
     123    var color = document.defaultView.getComputedStyle(block, null).getPropertyValue('color');
     124    if (color === 'rgb(0, 255, 0)')
     125        log('PASSED');
     126    else
     127        log('FAILED: color was not green but ' + color);
     129window.addEventListener('load', checkBlockColor, true);
     133<p> Bug <a href="">XXXX</a>: WebKit does not apply class-selector</p>
     134<p> For this test to pass, you should see a green rectangle below and / or you should see PASS below. </p>
     135<p id="console"></p>
     136<p class="block"></p>
     140As you can see, we did not need the pixels to get the information. The idea is that in this case, we were testing CSS and not the painting part of WebKit so querying the property directly would give us the result we need without having to dump the pixels.
     142=== How to write portable pixel tests ===
    59144 * Do not use fonts other than those bundled with Mac OS X (usually there's no need to specify font families in a test). The only exception to this is the ''Ahem'' font.