Testing Hello World

Blog entries
2008 Sep 04

Living on the border.
2008 Sep 02

TAP - Test Anything Protocol.
2008 Aug 31

Padre - the journey I..
2008 Aug 21

Who needs and IDE for Perl anyway?.
2008 Aug 09

Padre project web site.
2008 Jul 27

Padre.
2008 Jul 23

White Camel.
2008 Jul 18

Name a Perl IDE - get a Perl book or YAPC ticket.
2008 Jul 09

QA Hackathon in Israel.
2008 Jul 01

OSDC Israel 2009 - Call for organizers.
2008 Jun 11

Selenium on Ubuntu 8.04 (Hardy).
2008 Jun 09

Testing Hello World.
2008 Jun 08

Wifi is working again!.
2008 Jun 07

CPANTS update.
2008 Jun 04

Frequent Internet blackouts.
2008 Jun 03

Upgrading to Ubuntu 8.04 Hardy on Compaq (HP) nc6400. .
2008 May 24

Test Automation Tips.
2008 May 22

Open Source IDE for Perl.
2008 May 21

This week in Ruby.
2008 May 21

Being included on Planet Perl.
2008 May 14

Adding tag cloud to the blog.
2008 May 14

Ubuntu 7.04 (beta) Feisty Fawn on Compaq (HP) nc6400.
2008 May 13

Test automation using Perl master class in Chicago.
2008 May 13

Adding tags to the blog.
2008 May 09

Automated Testing in PHP, Python, Ruby and Perl.
2008 Apr 03

Strawberry Perl for Windows.
2008 Apr 01

Oslo Hackathon day -4.
2008 Mar 28

Blogging about Perl outside the community?.
2008 Mar 27

OSCON Proposals rejected.
2008 Mar 26

Preparing for the QA Hackathon in Oslo.
2008 Mar 25

Missing licenses on CPAN modules?.
2008 Mar 24

License of Perl Modules on CPAN.
2007 Dec 24

Joining Technorati?.
2007 Dec 24

Regular Expressions in Perl 5.10.
2007 Dec 24

Switching in Perl 5.10.
2007 Dec 24

Smart Matching in Perl 5.10.
2007 Dec 24

What's new in Perl 5.10? say, //, state.
2007 Dec 23

The Zulo interview was published.
2007 Dec 08

Frequency of programming languages on LinkedIn.
2007 Dec 06

Interview in Zulo.
2007 Dec 06

Sun Startup Essentials Launch.
2007 Aug 25

Testing PostgresSQL.
2007 Aug 25

Testing Pugs and Perl 6.
2007 Aug 22

Testing Ruby.
2007 Aug 22

Testing GHC, the Glasgow Haskell Compiler.
2007 Aug 22

Testing NUT, the Network UPS Tools.
2007 Aug 21

Testing SQLite .
2007 Aug 20

Smoked Parrot.
2007 Aug 20

Quality Assurance of Perl 5.
2007 Jul 09

Using mod_perl for szabgab.com.
2007 Jul 07

Quality Assurance and Automated Testing in Open Source Software.
2007 Jul 07

Add tags to CPAN modules via CPAN::Forum .
2007 Jun 15

Windows on VMware.
2007 Jun 13

Reducing the social gap of the information age.
2007 May 25

Moving to a new server.
2007 May 04

Preparing an application for distribution.
2007 May 01

Spreadsheet::ParseExcel is looking for a maintainer.
2007 Apr 28

CPAN Modules in Linux Distributions.
2007 Apr 18

Version control of single files using Subversion.
2007 Apr 13

Testing results, Perl and CPAN module availability.
2006 Aug 05

Perltraining.org split into two.
2006 Jul 23

Upgrading Ubuntu to 6.06, (Dapper Drake).
2006 Jul 22

Ginger Spam Salad.
2006 Jul 20

Automating the blog.
2006 Jul 19

Wish list: search engine for Perl related sites.
2006 Jul 19

Perltraining.org .
2006 Jul 19

More blog related issues.
2006 Jul 19

Starting a blog.

 

home | blog

Testing Hello World

Published on 2008.06.09 at 20:25:22

Tags: testing, newsletter, test automation, perl, python, ruby, php, java, javascript, tap


This entry was first sent out as part of the Test Automation Tips. Visit here to subscribe.

Almost every introductory level programming book starts with the example how to print Hello World.

Let's see the examples:

Perl

Create a file called hello_world.pl with the following content:

   print "Hello World\n";

and then type on the command line

   $ perl hello_world.pl

Python

Create a file called hello_world.py with the following content:

   print "Hello World"

and run

   $ python hello_world.py

Ruby

Create the file hello_world.rb that looks like this:

    puts "Hello World";

and run

    $ ruby hello_world.rb

PHP

In PHP it is very similar but we usually put the HTML tags around the code as the most common environment of PHP is the web server. Actually when installing PHP you also get a command line executer of PHP so you can execute your PHP script like this:

    $ php hello_world.php

The code, including the regular PHP embedding sequence looks like this:

    <?php
    echo "Hello World\n";
    ?>

Java

It is a bit more work in Java.

Save the following code in hello_world.java

    class hello_world {
      static public void main( String args[] ) {
        System.out.println( "Hello World" );
      }
    }

Then compile it using

    $ javac hello_world.java

that should create another file called hello_world.class Run it as

    $ java hello_world

Javascript

JavaScript alone cannot print to the screen it relies on the environment where it is embedded. In its most common place - the web browser - one would rely on the Document Object Model (DOM) of the browser, hence hello_world.js contains the following:

    document.write("Hello World");

In order to run that we would probably create an HTML file called hello_world.html containing the following line

   <script src="hello_world.js" type="text/javascript"></script>

Opening this with a browser will display Hello World.

Other languages

If you really feel the urge to try it in other languages, there is a site listing the Hello World program in 373 programming languages

Testing Hello World

That's nice, but how do we know that our freshly created hello_world program works properly?

We can of course execute it manually (first compiling and linking and doing other magic where necessary) but we are here to automate that.

So we are going to write an automated test for this application.

Preparations

We would like to be able to execute our hello_world "application" regardless of the language it was written in. So we need to do some extra work. If you are on a Linux or Unix machine what you need to do it to put the appropriate sh-bang line in the hello_world program. So you put one of the following lines as the first line of relevant scripts.

    #!/usr/bin/perl
    #!/usr/bin/python
    #!/usr/bin/php
    #!/usr/bin/ruby

for example your perl script will look like this:

    #!/usr/bin/perl

    print "Hello World\n";

Then you need to make the files executable by typing

    $ chmod +x hello_world.*

For Java we need some extra work. Create a file called java.sh and add the following:

    #!/bin/sh
    javac hello_world.java
    java hello_world


Dealing with the JavaScript version I postpone to a later edition.

So now if the current directory (.) is in you PATH variable you can execute the scripts just by typing their name. You don't need to give the name of the Perl/Python/Ruby/PHP interpreter any more as it is given on the sh-bang line. We can even execute the Java version with a single command.

In Microsoft Windows similar effect might be achieved by connecting the action of the extension to the appropriate command.

So now that we can execute all the versions with a single command, let's write a test script.

Test script in Perl

Let's create a file called hello_world.t and put in the following content:

    use strict;
    use warnings;
    
    use Test::Simple tests => 1;
    
    $ENV{PATH} = '.'; 
    
    my $output = qx{@ARGV};
    
    ok($output eq "Hello World\n");


The first two lines of the script are just standard Perl best practices.

    use Test::Simple tests => 1;

loads the basic testing module of Perl and declares that there is going to be one test unit (assertion).

With the following line

    $ENV{PATH} = '.'; 

we make sure that when we run the external script via the shell, the shell will have the current directory in the PATH.

    my $output = qx{@ARGV};

Executes a command its name was received on the command line. The output of the command will go in the $output variable.

The last line is where we are checking if the output was equal to the expected string including the newline at the end.

    ok($output eq "Hello World\n");

The result of the condition (a true or false value) is the parameter of the ok() function we received when we included the Test::Simple module. It does not do much. Just prints "ok" or "not ok" followed by a sequence number.

So now we can run

    perl hello_world.t  hello_world.pl

and we will see

    1..1
    ok 1

We can also run

    perl hello_world.t  hello_world.py

or

    perl hello_world.t  hello_world.rb

or

    perl hello_world.t  hello_world.php

or even

    perl hello_world.t  java.sh

Each one will print exactly the same results, as each one is a correct implementation of "Hello World"

    1..1
    ok 1

The first line of the output comes from the declaration when we said that we are planning to run one test unit.

The second line was printed by the ok() function as it received a true value.

TAP

The output we saw declaring the expected number of test units and the oks is a simple example of TAP - the Test Anything Protocol used in all the testing tools in Perl. You can read about it more here: http://www.testanything.org/ but I am going to discuss it more in detail later on.

In short we are expected to declare the number of test units in the beginning of our test script and then for each unit we are trying to verify we should call ok() with a boolean value. True if the unite was successful, False if it failed.

Caveats

The application we are testing might also print something to the standard error (STDERR) which we did not capture. We'll look at it later.

If the application "hangs" the test will "hang". We need to think about that too.

Other Uses

The above was a slight too long introduction how to test a command line application written in any language.

Of course a Hello World program is quite simple but it is actually the same issue I am facing with the examples I have in my training classes. I have hundreds of small scripts that should do something - or in some case should break in a particular way - to show a topic. I occasionally change something in the example code. How can I know that it did not break or if it still breaks in the way I want it to?

We'll talk about it next time.


This entry was first sent out as part of the Test Automation Tips. Visit here to subscribe.

Comments?

Instead of enabling comments here, please write your comment in your own blog and send me the link so I can add it here.

Last Update: Tue Sep 25 17:06:26 2007