Документация
HTML CSS PHP PERL другое

Section 18.6.  Debugging and Testing

 
Previous
Table of Contents
Next

18.6. Debugging and Testing

Add new test cases before you start debugging.

The first step in any debugging process is to isolate the incorrect behaviour of the system, by producing the shortest demonstration of it that you reasonably can. If you're lucky, this may even have been done for you:


    To: DCONWAY@cpan.org
    From: sascha@perlmonks.org
    Subject: Bug in inflect module

    Zdravstvuite,

    I have been using your Lingua::EN::Inflect module to normalize terms in a
    data-mining application I am developing, but there seems to be a bug in it,
    as the following example demonstrates:

        use Lingua::EN::Inflect qw( PL_N );

        print PL_N('man'), "\n";       # Prints "men", as expected
        print PL_N('woman'), "\n";     # Incorrectly prints "womans"

Once you have distilled a short working example of the bug, convert it to a series of tests, such as:


    use Lingua::EN::Inflect qw( PL_N );
    use Test::More qw( no_plan );

    is(PL_N('man') ,  'men',   'man -> men'     );
    is(PL_N('woman'), 'women', 'woman -> women' );

Don't try to fix the problem straightaway. Instead, immediately add those tests to your test suite. If that testing has been well set up, that can often be as simple as adding a couple of entries to a table:


    my %plural_of = (
        'mouse'         => 'mice',
        'house'         => 'houses',
        'ox'            => 'oxen',
        'box'           => 'boxes',
        'goose'         => 'geese',
        'mongoose'      => 'mongooses',
        'law'           => 'laws',
        'mother-in-law' => 'mothers-in-law',

        
# Sascha's bug, reported 27 August 2004...
'man' => 'men', 'woman' => 'women', );

The point is: if the original test suite didn't report this bug, then that test suite was broken. It simply didn't do its job (i.e., finding bugs) adequately. So fix the test suite first...by adding tests that cause it to fail:

    
    > perl inflections.t


    ok 1 - house -> houses
    ok 2 - law -> laws
    ok 3 - man -> men
    ok 4 - mongoose -> mongooses
    ok 5 - goose -> geese
    ok 6 - ox -> oxen
    not ok 7 - woman -> women
    #     Failed test (inflections.t at line 20)
    #          got: 'womans'
    #     expected: 'women'
    ok 8 - mother-in-law -> mothers-in-law
    ok 9 - mouse -> mice
    ok 10 - box -> boxes
    1..10
    # Looks like you failed 1 tests of 10.

Once the test suite is detecting the problem correctly, then you'll be able to tell when you've correctly fixed the actual bug, because the tests will once again stop failing.

This approach to debugging is most effective when the test suite covers the full range of manifestations of the problem. When adding test cases for a bug, don't just add a single test for the simplest case. Make sure you include the obvious variations as well:


    my %plural_of = (
        'mouse'         => 'mice',
        'house'         => 'houses',
        'ox'            => 'oxen',
        'box'           => 'boxes',
        'goose'         => 'geese',

        'mongoose'      => 'mongooses',
        'law'           => 'laws',
        'mother-in-law' => 'mothers-in-law',

        
# Sascha's bug, reported 27 August 2004...
'man' => 'men', 'woman' => 'women', 'human' => 'humans', 'man-at-arms' => 'men-at-arms', 'lan' => 'lans', 'mane' => 'manes', 'moan' => 'moans', );

The more thoroughly you test the bug, the more completely you will fix it.

    Previous
    Table of Contents
    Next
    © 2000- NIV