4.1 Feature Testing


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  4.   Testing a Parser

    Content

In feature testing, you create tests that verify that your parser produces the correct translation of text. For example, you should be able to test that an arithmetic parser evaluates "7 - 3 - 1" as 3 . It is important to automate your tests, because during development a test that has previously passed can suddenly fail. For example, a new error in a tokenizer can cause a formerly effective arithmetic parser to fail.

You can write your own testing tools or use an existing framework. Several testing frameworks are available, including JUnit, which was used in writing the code for this book. JUnit, developed by Kent Beck and Erich Gamma, is freely available on the Internet.

Testing frameworks typically offer a standard way to define and execute test methods and to make assertions about these methods ' results. The framework then automatically checks such assertions as often as you run your tests.

For example, to build an automated test for an arithmetic parser, you can create a subclass of a junit.framework.TestCase . The TestCase class provides a variety of assertion methods that you can call. Your subclass might include a method that makes the call

 assertEquals(ArithmeticParser.value("7 - 3 - 1"), 3, 0); 

The assertEquals() method will throw an exception if the values of the first two parameters are not equal, or at least within the specified tolerance. In this example the tolerance is 0, so the two parameters must be exactly equal.

If you include assertion calls in a method whose name begins with the word test , the JUnit framework will comprehend that it should execute the method when conducting a test. Here is a complete testing class designed to test the ArithmeticParser class using JUnit:

 package testing;  import junit.framework.*; import sjm.examples.arithmetic.*; /** * Test <code>sjm.examples.arithmetic.ArithmeticParser * </code>. */ public class ArithmeticParserTest extends TestCase { public ArithmeticParserTest(String name) {     super(name); } public void testAssociativity()     throws ArithmeticExpressionException {     // subtraction     assertEquals(         ArithmeticParser.value("7 - 3 - 1"), 3, 0);     // exponentiation     assertEquals(         ArithmeticParser.value("2 ^ 1 ^ 4"), 2, 0.01); } public void testExpressionException() {      try {          ArithmeticParser.value(" 7 / ");          fail("should throw ArithmeticExpressionException");      }catch (ArithmeticExpressionException e) {      } } public void testInteger()     throws ArithmeticExpressionException {     assertEquals(ArithmeticParser.value("42"), 42, 0); } public void testParentheses()     throws ArithmeticExpressionException {     assertEquals(         ArithmeticParser.value(" ((3 * 7) + (11 * 3)) / 3"),         18,         0); } public void testPrecedence()     throws ArithmeticExpressionException {     assertEquals(         ArithmeticParser.value("7 - 3 * 2 + 6"), 7, 0);     assertEquals(         ArithmeticParser.value("2^1^4"), 2, 0);     assertEquals(         ArithmeticParser.value("2^3^2"), 512, 0);     assertEquals(         ArithmeticParser.value("1000+2*2^3^2/2"), 1512, 0);     assertEquals(         ArithmeticParser.value("3*2^2*3"), 36, 0); } } 

The JUnit framework includes a TestRunner class that executes all the test methods in a test class. For example, you can run TestRunner and pass it the class name testing.ArithmeticParserTest as a command line parameter. The framework detects which methods in ArithmeticParserTest begin with test and executes these methods. As TestRunner executes, a progress bar shows your tests executing and a message area points out tests that fail.

Many developers find that once they begin using an automated testing framework, the security and empowerment they obtain makes them question how they ever lived without it.


   
Top


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net