Приглашаем посетить
Экономика (economics.niv.ru)

Section 8.5.  Test::Builder::Tester

Previous
Table of Contents
Next

8.5. Test::Builder::Tester

If you have a particularly perverse mind, you may now be thinking, "So what do the tests for Test::Builder look like?" Well, even more perverse minds have got there first, and Test::Builder has its own test suite creation module, rather predictably called Test::Builder::Tester. (And here it bottoms out, as Test::Builder::Tester contains enough functionality to test itself.)

The basic premise of Test::Builder::Tester is this: you first declare what output you expect to see from your Test::Builder module for a particular test; then you run the test in a controlled manner, producing the output for real; then your actual test compares the expected output with the real output. This may seem a little meta until you see an example, so let's look at one now.

We're writing a test script for our new Test::Fuzzy module; we begin by using Test::Fuzzy and also Test::Builder::Tester to provide the meta-testing functions and to state our test plan.

    use Test::Fuzzy;
    use Test::Builder::Tester tests => 1;

We're going to run two tests that should pass, so we declare that we expect to see two successful results:

    test_out("ok 1");
    test_out("ok 2");

This tells Test::Builder::Tester what to expect on its standard output.

Now we actually run the two tests that should pass:

    is_fuzzy("motches", "matches");
    is_fuzzy("fuzy",    "fuzzy");

All being well, this should output:

    ok 1
    ok 2

since they do match approximately. But in this case, the actual output is not written to the screen but stashed away by Test::Builder::Tester so that it can be compared against our predictions.

The final stage is to see whether or not the test output that's been stashed away really did meet our prediction:

    test_test("Two trivial tests passed OK");

If it did indeed output the right thing, then Test::Builder::Tester finally does output something to the screen, like so:

    ok 1 - Two trivial tests passed OK

There are extensions to Test::Builder::Tester, such as Test::Builder::Tester::Color, which allows it to disambiguate between, for instance, expected and unexpected failures by means of color-coding, but if you're going that deeply into metatesting, you'd probably be best learning the ropes for yourself.

    Previous
    Table of Contents
    Next