Unit Tests

Creating a test file
Prerequisites for testing
Testing
UnitTest API
True()
False()
Is()
IsNot()
IsDeeply()
IsNotDeeply()

OTRS provides unit tests for core modules.

Creating a test file

The test files are stored in .t files under /scripts/test/*.t. For example the file /scripts/test/Calendar.t for the Calendar Module.

A test file consists of the function call of the function to be tested and the analysis of the return value. Example (/scripts/test/Calendar.t):

# --
# Calendar.t - Calendar
# Copyright (C) 2001-2018 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

use strict;
use warnings;
use utf8;

use vars qw($Self);

use Kernel::System::CalendarEvent;

my $EventObject = $Kernel::OM->Get('Kernel::System::CalendarEvent');


my $EventID = $EventObject->EventAdd(
    Title => 'Some Test',
    StartTime => '1977-10-27 20:15',
    EndTime => '1977-10-27 21:00',
    State => 'public',
    UserIDs => [1],
);

$Self->True(
    $EventID,
    'EventAdd()',
);

[..]
        

Prerequisites for testing

To be able to run the unit tests, you need to have all optional Perl modules installed, except those for different database backends than what you are using. Run bin/otrs.CheckModules.pl to verify your module installation.

You also need to have an instance of the OTRS web frontend running on the FQDN that is configured in your local OTRS's Config.pm file. This OTRS instance must use the same database that is configured for the unit tests.

Testing

To check your tests, just use "bin/otrs.UnitTest.pl -n Calendar" to use /scripts/test/Calendar.t.

shell:/opt/otrs> bin/otrs.UnitTest.pl -n Calendar
+-------------------------------------------------------------------+
/opt/otrs/scripts/test/Calendar.t:
+-------------------------------------------------------------------+
ok 1 - EventAdd()
=====================================================================
Product:   OTRS 3.2.x git
Test Time: 0 s
Time:      2010-04-02 12:58:37
Host:      yourhost.example.com
Perl:      5.8.9
OS:        linux
TestOk:    1
TestNotOk: 0
=====================================================================
shell:/opt/otrs>
        

UnitTest API

OTRS provides a small and simple API for unit testing that was used in the previous example. Here we'll list the most important functions, please also see the online API reference of Kernel::System::UnitTest .

True()

This function tests whether given scalar value is a true value in Perl.

$Self->True(
    $EventID,
    'EventAdd()',
);
                

False()

This function tests whether given scalar value is a false value in Perl.

$Self->False(
    $EventID,
    'EventAdd()',
);
                

Is()

This function tests whether the given scalar variables are equal.

$Self->Is(
    $A,
    $B,
    'Test Name',
);
                

IsNot()

This function tests whether the given scalar variables are inequal.

$Self->IsNot(
    $A,
    $B,
    'Test Name',
);
                

IsDeeply()

This function compares complex data structures for equality. $A and $B have to be references.

$Self->IsDeeply(
    $A,
    $B,
    'Test Name',
);
                

IsNotDeeply()

This function compares complex data structures for inequality. $A and $B have to be references.

$Self->IsNotDeeply(
    $A,
    $B,
    'Test Name',
);