Test2::Manual::Testing::Introduction - Introduction to testing with Test2.
This tutorial is a beginners introduction to testing. This will take you through writing a test file, making assertions, and running your test.
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.
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.
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".
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.
[Prereqs / TestRequires]
Test2::V0 = 0.000060
my %WriteMakefileArgs = (
...,
"TEST_REQUIRES" => {
"Test2::V0" => "0.000060"
},
...
);
test_requires 'Test2::V0' => '0.000060';
my $build = Module::Build->new(
...,
test_requires => {
"Test2::V0" => "0.000060",
},
...
);
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;
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.