CONTENTS

NAME

Test2::Manual::Testing::Introduction - Introduction to testing with Test2.

DESCRIPTION

This tutorial is a beginners introduction to testing. This will take you through writing a test file, making assertions, and running your test.

BOILERPLATE

THE TEST FILE

Test files typically are placed inside the t/ directory, and end with the .t file extension.

t/example.t:

use Test2::V0;

# Assertions will go here

done_testing;

This is all the boilerplate you need.

use Test2::V0;

This loads a collection of testing tools that will be described later in the tutorial. This will also turn on strict and warnings for you.

done_testing;

This should always be at the end of your test files. This tells Test2 that you are done making assertions. This is important as test2 will assume the test did not complete successfully without this, or some other form of test "plan".

DIST CONFIG

You should always list bundles and tools directly. You should not simply list Test2::Suite and call it done, bundles and tools may be moved out of Test2::Suite to their own dists at any time.

Dist::Zilla

[Prereqs / TestRequires]
Test2::V0 = 0.000060

ExtUtils::MakeMaker

my %WriteMakefileArgs = (
  ...,
  "TEST_REQUIRES" => {
    "Test2::V0" => "0.000060"
  },
  ...
);

Module::Install

test_requires 'Test2::V0' => '0.000060';

Module::Build

my $build = Module::Build->new(
    ...,
    test_requires => {
        "Test2::V0" => "0.000060",
    },
    ...
);

MAKING ASSERTIONS

The most simple tool for making assertions is ok(). ok() lets you assert that a condition is true.

ok($CONDITION, "Description of the condition");

Here is a complete t/example.t:

use Test2::V0;

ok(1, "1 is true, so this will pass");

done_testing;

RUNNING THE TEST

Test files are simply scripts. Just like any other script you can run the test directly with perl. Another option is to use a test "harness" which runs the test for you, and provides extra information and checks the scripts exit value for you.