Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
TesterViera edited this page Aug 26, 2012 · 4 revisions

Test First Java

The goal of the tester library provides a support for test-first design and unit testing in Java that is easily accessible to a novice programmer — with no new syntax, no new concepts other than what is normally covered in every introductory curriculum. The sample data and the test methods are defined in an class that basically plays the role of the client to the programmer’s code.

Motivation

The majority of introductory programming courses that use Java provide only a minimal or no introduction to unit testing. The main reason for this is that using the JUnit testing framework requires that students learn yet another set of techniques and tricks, and that students need to understand early on the nuances of defining equality of two objects (e.g. are the two objects identical, or do they represent the same values, or do they refer to identical objects, or objects whose values are the same...)

Our tester library provides a novice-friendly environment for the design of tests for Java programs. The tests are written as a code that the program user would write. The programmer defines an Examples class that behaves as a client to the student’s code: it includes the sample data definitions for all classes defined in the program that is to be tested, and the test methods for all methods defined in the program. The test methods consist of method invocations (by sample data objects defined in this class) paired with the expected results. For methods that have no side effects this is very straightforward. For methods that have effects (and may be void, i.e. not producing any values) the test method consists of setting up the needed data, invoking the method, and then evaluating the expected effects.

The tester library has been used successfully in courses that follow a variety of curricula — whether one starts with static functions, uses BlueJ textbook and curiculum, emphasizes objects-first, or, as we do, focuses on the design of classes and class hierarchies from the beginning.

Additional materials that cover more advanced sotfware testing concepts have been used in our courses and presented at our summer faculty development workshops.

Watch the tester web site for tutorials on the use of the tester library in a variety of contexts, for additional tutorials on software testing and follow us on Twitter at TestFirstJava.


A Simple Example

Here is a very simple example of the use of the tester library, together with a sample output from running the tests.


For the beginner, the library assumes that all methods that invoke tests are defined within one class, typically named , though any name may be used. (For a more advanced programmer the library supports a number of other options for where the test cases are defined, as well as the type of test cases that are supported.)

Related test cases are defined within a method with the name that starts with and consumes one argument of the type .

A test case is constructed as an invocation of the method by an instance of the class given as the method argument. (Additional test case scenaria may use other similar methods, e.g. , , , etc.)

The tester library identifies all test methods, invokes each of them, and reports the results. The test report starts with the (pretty-printed) display of all data defined in the class and is followed by a report of the number of tests performed, the number of tests that failed, and a report on every failed test.


Running the tests for the following class: `

    import tester.*;
       
   class SimpleExamples{
    String hello = "hello";
    String world = "world";
    
    void testStrings(Tester t){
      t.checkExpect(5, new Integer(5));
      t.checkExpect(hello.equals(world), false);
      t.checkExpect(hello.length(), world.length(), "OK");
      t.checkExpect(hello, world, "will fail");
    }
  }

` produces the following output:

`

  Tester Prima v.1.5.1 - 17 June 2012

  ----------------------------------

  Tests defined in the class: SimpleExamples:

  ---------------------------

  SimpleExamples:

  ---------------
   
   new SimpleExamples:1(
    this.hello =  "hello"
    this.world =  "world")
  ---------------
   
  Ran 4 tests.

  1 test failed.

   
  Failed test results:

  --------------
   

  Error in test number 4

  will fail

  tester.ErrorReport: Error trace:

          at SimpleExamples.testStrings(Examples.java:11)
          at SimpleExamples.main(Examples.java:16)


  actual:.......................................... expected:

  "hello".......................................... "world"
   
   
   
  --- END OF TEST RESULTS ---

`

Please see the Overview page for details how to set up and run the tests as well as a complete set of samples.

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.