Поиск…


Пример тестирования модуля Perl

Ниже приведен простой пример тестового сценария Perl, который дает некоторую структуру, позволяющую тестировать другие методы в тестируемом классе / пакете. Сценарий создает стандартный вывод с простым текстом «ok» / «not ok», который называется TAP (Test Anything Protocol).

Как правило, команда подтверждения запускает скрипт (сценарии) и суммирует результаты теста.

#!/bin/env perl
# CPAN
use Modern::Perl;
use Carp;
use Test::More;
use Test::Exception;
use Const::Fast;

# Custom
BEGIN { use_ok('Local::MyPackage'); }

const my $PACKAGE_UNDER_TEST => 'Local::MyPackage';

# Example test of method 'file_type_build'
sub test_file_type_build {
    my %arg    = @_;
    my $label  = 'file_type_build';
    my $got_file_type;
    my $filename = '/etc/passwd';

    # Check the method call lives
    lives_ok(
        sub {
            $got_file_type = $PACKAGE_UNDER_TEST->file_type_build(
                filename => $filename
            );
        },
        "$label - lives"
    );

    # Check the result of the method call matches our expected result.
    like( $got_file_type, qr{ASCII[ ]text}ix, "$label - result" );
    return;
} ## end sub test_file_type_build

# More tests can be added here for method 'file_type_build', or other methods.


MAIN: {

   subtest 'file_type_build' => sub {
      test_file_type_build();
      # More tests of the method can be added here.
      done_testing();
   };

   # Tests of other methods can be added here, just like above.


   done_testing();
} ## end MAIN:

Лучшая практика

Тест-скрипт должен тестировать только один пакет / класс, но для проверки пакета / класса может использоваться множество скриптов.

Дальнейшее чтение



Modified text is an extract of the original Stack Overflow Documentation
Лицензировано согласно CC BY-SA 3.0
Не связан с Stack Overflow