|
NAMETest2::Manual::Testing::Introduction - Introduction to testing with Test2.DESCRIPTIONThis tutorial is a beginners introduction to testing. This will take you through writing a test file, making assertions, and running your test.BOILERPLATETHE TEST FILETest 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.
DIST CONFIGYou 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 ASSERTIONSThe 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 TESTTest 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.RUN DIRECTLY$ perl -Ilib t/example.t Which should produce output like this: # Seeded srand with seed '20161028' from local date. ok 1 - 1 is true, so this will pass 1..1 If the test had failed ("ok(0, ...)") it would look like this: # Seeded srand with seed '20161028' from local date. not ok 1 - 0 is false, so this will fail 1..1 Test2 will also set the exit value of the script, a successful run will have an exit value of 0, a failed run will have a non-zero exit value. USING YATHThe "yath" command line tool is provided by Test2::Harness which you may need to install yourself from cpan. "yath" is the harness written specifically for Test2.$ yath -Ilib t/example.t This will produce output similar to this: ( PASSED ) job 1 t/example.t ================================================================================ Run ID: 1508027909 All tests were successful! You can also request verbose output with the "-v" flag: $ yath -Ilib -v t/example.t Which produces: ( LAUNCH ) job 1 example.t ( NOTE ) job 1 Seeded srand with seed '20171014' from local date. [ PASS ] job 1 + 1 is true, so this will pass [ PLAN ] job 1 Expected asserions: 1 ( PASSED ) job 1 example.t ================================================================================ Run ID: 1508028002 All tests were successful! USING PROVEThe "prove" command line tool is provided by the Test::Harness module which comes with most versions of perl. Test::Harness is dual-life, which means you can also install the latest version from cpan.$ prove -Ilib t/example.t This will produce output like this: example.t .. ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.06 CPU) Result: PASS You can also request verbose output with the "-v" flag: $ prove -Ilib -v t/example.t The verbose output looks like this: example.t .. # Seeded srand with seed '20161028' from local date. ok 1 - 1 is true, so this will pass 1..1 ok All tests successful. Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.06 cusr 0.00 csys = 0.08 CPU) Result: PASS THE "PLAN"All tests need a "plan". The job of a plan is to make sure you ran all the tests you expected. The plan prevents a passing result from a test that exits before all the tests are run.There are 2 primary ways to set the plan:
ADDITIONAL ASSERTION TOOLSThe Test2::V0 bundle provides a lot more than "ok()", "plan()", and "done_testing()". The biggest tools to note are:
SEE ALSOTest2::Manual - Primary index of the manual.SOURCEThe source code repository for Test2-Manual can be found at https://github.com/Test-More/Test2-Suite/.MAINTAINERS
AUTHORS
COPYRIGHTCopyright 2018 Chad Granum <exodist@cpan.org>.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
Visit the GSP FreeBSD Man Page Interface. |