Some Simple Examples PreviousNext

First some simple examples to get the flavor of how one uses getest: let's consider a class with a function that concatenates two strings and returns the result. This example is simple, but getest works the same way for testing more realistic classes.

class CONCAT1

create

    make

feature {NONE} -- Initialization

    make
            -- Create a new string concatenator.
        do
        end

feature -- Basic operations

    concat (s1, s2: STRING): STRING
            -- Concatenate s1 and s2.
        require
            s1_not_void: s1 /= Void
            s2_not_void: s2 /= Void
        do
            create Result.make (s1.count + s2.count)
            Result.append_string (s1)
            Result.append_string (s1)
        ensure
            concat_string_not_void: Result /= Void
        end

end 

In order to test this class, one just has to write a simple test case. The test case class will inherit from class TS_TEST_CASE (from the test harness cluster of the Gobo Eiffel Test Library) which will provide testing facilities to exercise Eiffel code, such as routines assert and assert_equal. The test class will be equipped with a test routine test_concat which makes several assertions about the expected results of the feature concat from the tested class.

class TEST_CONCAT1

inherit

    TS_TEST_CASE

create

    make_default

feature -- Test

    test_concat
            -- Test feature concat.
        local
            c: CONCAT1
        do
            create c.make
            assert_equal ("toto", "toto", c.concat ("to", "to"))
            assert_equal ("foobar", "foobar", c.concat ("foo", "bar"))
        end

end 

The source code for this example can be found in $GOBO/library/test/example/concat1.

Once this test case class has been written, one can run the test suite as follows:

getest getest.<compiler>

where <compiler> is either ge, ise or se depending on the Eiffel compiler used to compile the test suite. Alternatively one can use the following shorthand:

getest --<compiler>

which is equivalent to the command-line above. Here is the output I got when running getest with the ISE Eiffel compiler:

$ cd $GOBO/library/test/example/concat1
$ getest --ise
Preparing Test Cases
Compiling Test Cases
Running Test Cases

Test Summary for xconcat1

# Passed:     0 test
# FAILED:     1 test
# Aborted:    0 test
# Total:      1 test (2 assertions)

Test Results:
FAIL:  [TEST_CONCAT1.test_concat] foobar
   expected: foobar
   but  got: foofoo

Oh well, it looks like we have just found a bug! We can easily see that the third line in the routine concat from class CONCAT1 should have been:

Result.append_string (s2)

The source code for this example with the bug fixed can be found in $GOBO/library/test/example/concat2. Here is what we get when we run getest again:

$ cd $GOBO/library/test/example/concat2
$ getest --ise
Preparing Test Cases
Compiling Test Cases
Running Test Cases

Test Summary for xconcat2

# PASSED:     1 test
# Failed:     0 test
# Aborted:    0 test
# Total:      1 test (2 assertions)

That's better!


Copyright © 2001-2016, Eric Bezault
mailto:
ericb@gobosoft.com
http:
//www.gobosoft.com
Last Updated: 28 December 2016

HomeTocPreviousNext