The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Tk - Testing Tk widgets.

SYNOPSIS

use Test::More tests => 5;
use Test::Tk;

BEGIN { use_ok('Tk::MyWidget') };

createapp(@options);

my $widget;
if (defined $app) {
   $widget = $app->MyWidget->pack;
}

push @tests,
   [sub { return defined $widget }, 1, 'Created MyWidget'],
   [sub { return 1 }, 1, 'A demo test'],
;

starttesting;

DESCRIPTION

This module aims to assist in the testing of Perl/Tk widgets.

createapp creates a MainWindow widget and places it in the variable $app. It sets a timer with delay $delay to start the internal test routine.

starttesting launches the main loop and sets a timer with delay $delay to start the internal test routine.

When testing is done the MainWindow is destroyed and the test script continues.

You can set a command line parameter show to test command on the command line. eg perl -Mblib t/My-Test.t show. The application will not terminate so you can visually inspect it.

It will perform two tests. You need to account for these when you set your number of tests.

If you are not on Windows and no XServer is running, all tests will be skipped.

EXPORT

$app

Holds the reference to the MainWindow object. If you are not on Windows and no XServer is running, the MainWindow will not be created and $app remains undefined. Do not change this variable.

$delay

Default value 100. The delay time between creating the test app and start of the testing. You may want to increase this value in case all tests succeed but your test program still throws an error.

$mwclass

Default value Tk::MainWindow. You can set it to a derived class.

$quitdelays

Default value 200. This is the delay set between the termination of tests and the desctruction of the main widow. If your desktop goes bonkers while testing, you main want to increase this value. But I think you're good.

@tests

Each element of @tests should contain a list of three elements.

A reference to a sub

The sub should return the expected value for the test to succeed. It should always return something in scalar context.

Expected value

This can be a simple scalar but also the reference to a list or a hash. You may even specify a complexer data structure.

Description

A brief description of the test so you know which test passed or failed.

$show

By default 0. Is set when the show option is given at the command line. You can overwrite this by setting or clearing this yourself.

createapp(@options)

Creates the MainWindow object and tests if successfull. Places the object in B>$app>.

hashcompare(\%hash1, \%hash2)

Depricated, use Test::Deep or Data::Compare.

listcompare(\@list1, \@list2)

Depricated, use Test::Deep or Data::Compare.

pause($miliseconds)

Pauses the app for $miliseconds, allowing background processes to finish.

starttesting

Launches the main loop and sets a timer with delay $delay to start the internal test routine.

testaccessors($obj, 'method1, 'method2', ...)

Quickly set up accessor tests. Just to be clear, an accessor is something like this.

sub Value {
   my $self = shift;
   $self->{VALUE} = shift if @_;
   return $self->{VALUE}
}

SEE ALSO

Test::More Test::Deep

AUTHOR

Hans Jeuken, <hanje at cpan dot org@>

TODO

This should also work for Tcl::pTk widgets. However, the testing of this module during install is done with Tk. So this is set as a prerequisite. A duplicate module with slightly different defaults for Tcl::pTk is thinkable.

COPYRIGHT AND LICENSE

Copyright (C) 2023 - 2024 by Hans Jeuken

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.