|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
General Java Testing Java Applets
Testing Java Applets
By: Roger Hayes
Mar. 1, 1997 12:00 AM
Why Test Applets? An Applet is a Java class. When we test it, we are trying to show that it is a good Applet; specifically, that it has certain good properties, and lacks certain bad properties. The important properties of an applet can be categorized under two broad headings: functionality and portability.
Functionality Our testing should find out if our applet class, together with the other classes that make up our Applet program, does what we wrote it to do. There is no general rule for this; functionality testing depends on the function our applet performs. The minimum standard for our class, that it extends the java.applet.Applet class, is checked by the Java compiler.
Portability
Testing Narrowly defined, testing is the art of running a program with some controlled input under some kind of observation scheme in order to show that the program has certain properties. With Java programs, it is possible instead to determine certain properties of a program without running it, by inspecting the class files. For example, if the class file contains no references to any methods of any classes of the Java.io package, then the program will not do I/O. When possible, a static check is much simpler and more reliable than a dynamic test. However, there are many properties that cannot be checked statically; for example, does a program respond correctly to resize events? Coverage An important concept from testing theory is the notion of coverage. Roughly speaking, coverage is a measure of how good your test is. The simplest coverage measure is statement coverage; if every statement in your program has been executed in a test run, you have achieved 100 percent statement coverage. Other common coverage measures are branch coverage and path coverage. If you have access to a coverage measurement tool, you can use it to drive your testing effort by deciding on a specific coverage measure as a goal for your test plan. Available coverage tools for Java include JavaScope from SunTest and "TCAT for Java" from Software Research, which work by instrumenting the source code for a class.
Test Strategy
Functionality Testing While discussing functionality testing, I will generally discuss function testing. I presume that the special functionality that your applet provides is in the form of methods that are not part of the Applet interface. If this is not true, if all the interesting work your Applet does is in the implementation of methods from the Applet interface, then a GUI testing tool (described in this section), should suffice for your functionality testing.
There are several ways to do function testing:
Adding a main() method The simplest kind of main does all the testing itself, as shown in Listing 1. This kind of test is simple to run and interpret and simple to repeat, but is at best somewhat inconvenient to write. It requires enumerating the test cases and expected results in the test driver, which may be a lot of code to add to your Applet. It also requires the main to create an instance of the tested class, which may be difficult. The example shows a very simple kind of check on the result of a method: comparing it to a tabulated result. This is not the only kind of check that can be done. Other, less direct, checks on the result of a method are possible, or required, in many cases.
double r = cubeRoot(f);
Use a Test Generator A test program generator works by taking a specification of the required behavior of the program and of the data to use for testing, and using those specifications to generate a test program. JavaSpec, in particular, uses fragments of Java code as specifications. Essentially, JavaSpec automates the bookkeeping required to implement the test driver and the test scorecard, leaving to you the interesting work of creating the test data and the checking code. In addition, JavaSpec provides a framework for the structure of the test, helping you to create comprehensive test data sets and helping you with the structure of the checking code.
GUI Testing Recall that the purpose of testing is to show that a program has certain properties. The utility of GUI testing depends strongly on what properties you are trying to show. If you are trying to show that a program obeys buttons pushes and displays data, then GUI testing is indispensable. If you are trying to show that a program updates a database or calculates a square root correctly, then GUI testing may not be the most direct method.
GUI testing is generally best for two kinds of testing: Safety checking is done by driving the Applet through a sequence of actions, trying to exercise all of the states of the system.
GUI Testing Tools If you do decide to use a GUI testing tool, you must choose between a platform-specific GUI testing tool and a Java GUI testing tool. A platform-specific GUI test tool, like XRunner or WinRunner from Mercury Interactive, QA Partner from Segue, QC/Replay from Centerline, SQA Robot from SQA, or CAPBAK/X from Software Research, tests at the level of native code. A Java GUI testing tool, like JavaSTAR from SunTest, tests at the level of the Java AWT. The advantage of a platform-specific tool is that it can test the complete operation of your applet in a specific browser on a specific platform, including the interaction of your applet, the browser's Java implementation and the native window system. The disadvantage is that the tool and the test are tied to the specific platform and applet viewer or browser used. A Java GUI testing tool has the advantage of producing portable, cross-platform tests and of itself being portable, so that the test can be created on any platform. Its disadvantage is that it relies on the correctness of the Java implementation. SunTest's GUI testing tool, JavaSTAR, has the additional feature, which we think an advantage, that the scripting language is Java; the test scripts are Java programs that can be edited as required. Generally, the platform-specific tools have a proprietary scripting language for describing tests, which must be learned for effective use of the tool.
Self-checking Code
private void assert(boolean cond, String msg) {
Portability Testing Portability has various aspects. Fundamentally, portability testing is a check that you have stayed within the parts of the language that will work the same in every environment, or that you have worked around the differences. An Applet may run on a WebTV, on a multiprocessor Solaris workstation with a 24-bit framebuffer, on a PC with no sound card, or on a JavaStation running JavaOS. The goal of portability testing is to check that your Applet will perform its function in all these environments. The most important resource for improving the portability of your Applet is Javasoft's "100% Pure Java" program [http://www.javasoft.com/100percent], which provides information, training, testing and certification aimed at improving the portability of Java programs. A good portability test requires a sanity check, static checking and dynamic testing.
Sanity Check Its purpose is to catch errors so fundamental that they would prevent the more detailed test from running, and also to catch errors so unexpected that they are not checked in the test plan.
Static Checking
Dynamic Testing . If your Applet does any of these things, you should make sure that your firewall testing covers those parts of the program. For example, if your Applet plays a sound when a button is pushed, then make sure that your GUI test script includes a push of that button. Specific Problem Areas Using the test planning and creation techniques described above, check that your applet doesn't run into these known problem areas. Layout If your applet uses a layout manager, try resizing it; if it changes the contents of a Container, exercise that part of the code in various browsers. Threads If your applet creates a thread or synchronizes, it may behave differently on different Java implementations, or even at different times of day. This class of problem is, unfortunately, almost impossible to check by testing; there are just too many program states to check, and too little control over what state the program is in. The best cure is prevention: think hard about multithread behavior. For example, if your program uses synchronization locks, define a global lock order and obey the rule that locks must be acquired in order (no thread may acquire a lock unless it holds the preceding lock). This rule prevents deadlock.
Test Automation
Repeatability
Portability
Completeness
Conclusion Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
|
||||||||||||||||||||||||||||||||||||||||||||||||