Search code examples
yuiyui3

Can YUI3 Test asserts display the line number of failure?


I just started using YUI3 Test module (http://yuilibrary.com/yui/docs/test/).

I have testcases with many asserts that verify state. If one of the assert fails, the TestConsole indicates an assert failed, but doesn't indicate which of the many asserts in the test failed. It would be great to have the failure message report the line number.

The browser exception actually contains the JS failure line number, but the YUI3 Test class filters this out and throws its own exception, which doesn't seem to contain the line number. Is there an easy way to add this reporting while still taking advantage of the YUI3 Test class as a harness??


Solution

  • I will start with the tl;dr

    YUI3 does not provide any intrinsic way to display the line number of a failed test. I suppose it would be possible to manipulate Error constructors such that you could interrogate them; however, the problem is that Error.lineNumber is only supported in certain browsers (I believe it is Mozilla only).

    Even if that did work, you'd end up realizing that this is a bit convoluted. You'd have to always be sure to do:

    throw new Error*(...);
    

    In your calling code, you'd always have to do:

    try {...} catch(e) { /* e.lineNumber */ }
    

    And even if this all worked and you were willing to do this, I wouldn't recommend it.

    The real answer

    The root of the problem is that you seem to have multiple Asserts in your test methods. Developers that are trying to be pragmatic will sometimes tell you that "one assertion per test method" is unreasonable and dogmatic. It is very attractive to think that multiple assertions per test method is fine...until it isn't.

    I'm sure there are times where multiple assertions are better, but I haven't seen it yet. I've been testing for years now and here is what I've found:

    1. I've given multiple asserts per method a try, and each time I've been bitten by the problem of not knowing which assertion failed. No cargo-culting here...I've tried both, and out of the two methodologies, only one has not bitten me.
    2. One assertion per test forces you to really think about what/how you are testing.

    Reading: