📜 ⬆️ ⬇️

Testing Perl programs for beginners. Introduction

Why "for beginners"? Because, this manual was compiled by me and for me, in the process of studying the specifics of testing in general and testing of Perl programs - in particular. I do not pretend to the absolute completeness of the presentation of the topic, but perhaps this program will be useful to many novice programmers and will indicate the direction for further independent study of the testing process.

This is only the first part of the manual. Introduction :). If the community is interested, publish and continue.

What is testing


Testing is an activity that allows you to identify how the behavior of a software product is as expected under various conditions, input data and environment.
')
By testing products, the developer minimizes the likelihood of errors during product commissioning.

Testing can be carried out both in manual mode and in automatic mode. In the manual mode, the tester, using the product interface, enters arbitrary data, with or without errors, checks the software response to pressing buttons, etc. However, if changes are made to the program, all testing will have to be repeated anew.

In the automatic mode, the developer creates a series of tests for his software product, which emulates the client's actions, sending some data to the program interface, and analyzes his response to the expected one. Such tests, written once, can be used throughout the entire life cycle of a software product, and if the program’s functionality is supplemented with new developments, it will be easy to check if the functionality of the old components is not violated during the development process.

The advantages of automated tests:


All of the above does not mean that you should abandon "manual" testing and completely switch to automatic. You need a reasonable combination of both options.

An automatic test can test the functionality and performance of the program, but it cannot control the user's behavior (which can be completely illogical). Therefore, for the most qualitative testing, after successful completion of development and automatic testing, manual testing should be carried out, which will analyze the user's actions and reveal difficulties that arise when working with the program interface.

Perl Testing


To create test programs in Perl, the most commonly used modules are Test :: Simple , Test :: More and Test :: Harness . These modules work according to the TAP protocol.

Test :: More and Test :: Simple allow the programmer to create scripts that test the behavior of specified modules, program components, etc. The output of information about test results is carried out in a very compact form, in the TAP format.

Test :: Harness, in turn, provides an opportunity to streamline the management of the created test scripts (it is very important if the scripts are over 50, 100, 1000 ...). Allows you to write a program that will run a group of test scripts for execution, take from them, summarize and analyze the results. And also, display them to the user in a convenient format.

Since version 5.8, the Test :: More and Test :: Harness modules are part of the Perl core. For earlier versions of Perl, these modules are available in CPAN.

Your first test


For example, let's write a function that returns the unforgettable: “Hello, World!” And test its work.

Function file hello.pl:
#! / usr / bin / perl

sub hello {
return "Hello, world!";
}

one;

We test the hello () function, the script is called by the name of the function being tested - hello.t
#! / usr / bin / perl

use Test :: More tests => 2;

require_ok ('hello.pl');

ok (hello () eq "Hello, world!", "Function hello () return 'Hello, world!'");

Thanks to require_ok we will test the ability to connect our script through require. If this test is successfully passed, require_ok will automatically make hello.pl available for use by running the require command for the specified script.

The first argument, ok (), is a conditional expression. ok () receives the result of the hello () function and compares with the standard. If the data obtained and the reference are identical, the test will be marked in the report as passed. The second argument passed to ok () is an arbitrary, textual comment to the test (for example, what we are testing, why, what we should get as a result). Considerably easier to read test reports.

Run the test, see the test results:
% perl hello.t
1..2
ok 1 - require 'hello.pl';
ok 2 - Function hello () return 'Hello, world!'

Congratulations, you wrote your first test and successfully tested!

useful links


RUS

The Ten Most Important Perl Development Practices
Writing automated tests and the phpUnit environment.
Testing using libtap. Stig Brautaset, translated by Vladimir Kuxenok
“Organization and naming of automated tests”, Kirill Maximov
Perl: we study more deeply. Randal Schwartz. - O'Reilly

ENG

Building Testing Libraries
Perl Testing: A Developer's Notebook. By chromatic, Ian Langworth. - O'Reilly
http://petdance.com/perl/automated-testing/ (presentation)

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


All Articles