Unit Tests

Creating a test file
Testing
True()
False()
Is()

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-2010 OTRS AG, http://otrs.org/
# --
# $Id: unit-tests.xml,v 1.2 2010-08-26 14:59:57 mb Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;
use utf8;

use vars qw($Self);

use Kernel::System::User;
use Kernel::System::CalendarEvent;

$Self->{UserObject} = Kernel::System::User->new(%{$Self});
$Self->{EventObject} = Kernel::System::CalendarEvent->new(%{$Self}, UserID => 1);


my $EventID = $Self->{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()',
);

[..]
        

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.0.x CVS
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>
        

True()

This function tests whether the return value of the function 'EventAdd()' in the variable $EventID is valid.

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

False()

This function tests whether the return value of the function 'EventAdd()' in the variable $EventID is invalid.

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

Is()

This function tests whether the variables $A and $B are equal.

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