📜 ⬆️ ⬇️

Testing Perl programs for beginners. Test :: Simple

Test :: Simple is a basic, very simple module that is used to write tests.

The module allows you to use for testing only one function - ok () . If the capabilities of this function are not sufficient, it is recommended to use Test :: More. Tests written using Test :: Simple are fully compatible with Test :: More.

The test results are output in the TAP (Test Anything Protocol) format.
')

Connection Test :: Simple


When connecting Test :: Simple, you should inform the program in advance how many tests you plan to perform:
use Test :: Simple tests => 23;


By the number of tests is meant how many special testing functions will be launched during program execution.
For example, how many times the function ok () will be launched.

If the specified number and the number of tests actually performed do not match, the user will receive an error.

Example (Execution of one test in the program, with 2x pre-set) :

#! / usr / bin / perl

use Test :: Simple tests => 2;

ok (1 + 1 == 2, '1 + 1 = 2');

Displaying test results :

% perl test_simple.pl
1..2
ok 1 - 1 + 1 = 2
# Looks like you planned 2 tests but only ran 1.
%

When you start the testing program, Test :: Simple displays the format string “1 ... M”, where M is the number of tests that are supposed to be performed during the testing process.

OK()


ok () is the main and only testing function provided by Test :: Simple. Allows you to check the success of your program, function, part of the program code.

Syntax ok () :
ok ($ test_var eq $ ok_value, 'test_var eq ok_value');

The function processes the conditional expression passed to it. If the processing result is positive (true), the test will be considered as passed. Depending on the result, the function will display the message “ok” or “not ok” with the serial number of the test.

As the second argument of the function, you can specify a brief description of the tests. When displaying test results, the specified description will be displayed in one line with the results of a specific test.

Example :
#! / usr / bin / perl

use Test :: Simple tests => 1;

ok (1 + 1 == 2, 'Summation 1 + 1');

The output of the program :
% perl test_simple.pl
1..1
ok 1 - Summation 1 + 1


Using such short descriptions makes it easy to find the necessary lines in the program code and make corrections. In addition, the descriptions are useful when developing and using tests in a team.

Source: https://habr.com/ru/post/30626/


All Articles