Home > Articles > Web Services > WebSphere

  • Print
  • + Share This
This chapter is from the book

Unit Testing Requirements

dw2.jpg

Unit tests are one of those line items I see on every project plan, but that rarely get performed, mostly because they are ill defined by the industry as a whole. We like to talk about unit testing. Often unit testing is described as that magical thing that developers do right before the code moves to another stage in the project, like Q/A or UAT. Because we continue to discuss unit tests (real or imagined) in our projects, it must mean that we think it is an important topic, an important line item that we can use to improve the quality and reliability of our code. If only we knew the half of it!

Most people who are involved with software projects of any type understand the need for good testing at all levels. Heck, for that matter, any user who has ever had to deal with a buggy piece of software can understand the need. Testing is designed to catch problems before software ships or a system goes live. In some cases releasing buggy software can annoy the user and may result in your being labeled a bad programmer or your company identified as one who produces bad software. In more extreme cases, a buggy piece of code can result in lost revenue to a company, or may even be life threatening.

You need to understand what unit testing is used for—to remove bugs. What is a bug? Sometimes this definition is difficult to pin down. In general, any function that returns incorrect results could be considered a bug. But in most software products, this definition includes any action or inaction that does not comply with known requirements. Rarely are the product requirements written to incorporate every possible input and output combination, so in many cases developers have to make do. Some obvious and not-so-obvious cases that developers try to handle when building a part of the application can include

  • Not returning incorrect results that could be interpreted as correct when input parameters are not entered correctly.
  • Null or blank entries in a form.
  • Incorrect entries in a form.
  • Handling purposeful, malicious entries in a form. This form of security testing will not specifically be discussed here.
  • Testing a piece of functionality many times with similar input data to ensure that results are consistent.
  • Entering invalid input and data to ensure that errors and exceptions are handled gracefully and explanations are useful to the user.

Many of these types of tests are not enumerated within the requirements or test scenarios and are often something that the developer just assumes he or she must do. In addition, nonfunctional tests must often be performed, which includes making sure the results of a test are not only accurate for one user, but returns consistently accurate results as many users access the same code, or that the results displayed to each user of the system are correct for that user only. Developing an application that works correctly at this level can be a difficult task. For example, consider the following:

In order to improve performance of an application the team decides it will cache the data being used as much as possible, which results in the ability of the application to handle more users as load and scalability tests are performed. However, some users get the wrong results on their screen. Perhaps they are seeing the results from the user before them. This may be disastrous, for example, in the case of payroll forms or personal information.

You see from this simple example that a balance needs to be achieved between performance and functional correctness; however, without testing this balance can never happen.

Many types of testing can be performed within software development. Figure 2.5 illustrates a realistic view of the types of testing generally defined as necessary on a development project.

Figure 2.5

Figure 2.5 Levels of development testing

Different project teams often put a different emphasis on some areas. Many projects focus more on performance while others may not give performance a second thought, at least not until the site is live. Chapter 8, “Catching and Performance,” focuses on performance testing. Most of the layers you should readily understand, but for clarity’s sake I enumerate them here:

  • Unit testing: You can think of a unit as the most basic structural element of a system. This general definition is the one mostly followed in this book. In many languages a unit could be defined as a function or a method, but it also may be thought of as a bit broader, such as a unit of functionality or behavior.
  • Function/integration testing: Functional testing, sometimes called black-box or closed-box testing, evaluates the results or data derived by a system without regard to how the system is built. For most web-based applications, functional testing assumes that a user enters data into a page and evaluates the returned results. While on the surface this testing is similar to unit testing, the scope is normally set at a business function level. Integration testing means testing combined components in a single environment, such as hardware and software components, including different pieces of an application that are designed to work together. These two types of testing often work hand in hand and so are together in this circle.
  • Performance testing: Performance testing is not so much about a single user’s results as the ability to provide the correct results to many users within a given time period. Ensuring speed and minimizing system overhead leads directly to the ability of a system to handle a large number of users. This characteristic is commonly known as system scalability. Performance testing and tuning is a fine and sometimes dark art and requires skills both in development and infrastructure to achieve good results.
  • User acceptance testing: User acceptance testing means ensuring that the system meets or exceeds end user expectations. Even the most finely tuned or best-working system is worthless if it doesn’t do what the user expects or is too hard to use. Ideally, test scripts are built based on the original requirements of the system to see whether the end product matches what was originally defined.

Whether you perform all of these types of tests, or even add more testing layers, it is important to know what you are expecting to gain from a testing cycle. Without this end result in mind you are wasting time and money and possibly jeopardizing the success of the product.

What Is a Unit?

You can typically consider a unit as the first link in the chain consisting of several types of tests. This testing provides a way for developers, within the comfort of their own world, to assure themselves that the smallest unit or component of a system functions as expected. This assurance also provides the base for additional higher-level testing as the system moves away from the center circle in Figure 2.5 to a broader, more aggregate view of the system.

A simple definition of unit testing is the ability to test the smallest unit or component of code as possible. While it is the essence of what you want to accomplish, it can be a little misleading. Most of the testing that occurs within a project, if testing occurs at all, happens in large chunks at the functional or user acceptance level. Looking behind the glass one can rationalize using more fine-grained ways to validate the behavior of code. If assurance can be gotten that the behavior of individual components is correct, then it stands to reason that as those components are brought together, or integrated, then they will function correctly at the higher level.

So unit testing can actually make code better and ensure that a component behaves as expected. But there is more to it than that. A pleasant side-effect of creating and running unit tests on a continuous basis is that it can help ensure that ongoing changes made to code don’t have the unexpected effect of breaking earlier working pieces.

On some projects I have developers write out manually the list of tests that their code should be tested with, before they start coding. This step is the minimum developers should do for any piece of functionality they are coding. It also helps ensure that the developer understands the requirements correctly, and the list can feed into later QA or UAT type test scenarios.

Realistically, our components are too modular to be considered a single unit. However, going to the other extreme and testing at the method level may be too fine grained. The approach walks a fine line in trying to determine what to test, and how to get valid results.

The Trouble with Web Testing

Before the web and server-side programming, the definition of unit testing was not as difficult to pin down. Most applications had a main() method of some type that could be run on the command line. Building unit tests that had knowledge of what the application was trying to do was a simple matter of building test classes that ran the program, with the distinct advantage of being able to see inside the code. With the advent of the web and J2EE programming, components now run inside a container that creates or instantiates classes and then calls specific methods as required. In addition the container provides objects of its own as parameters to methods that contain the input and output values for processing.

Writing tests for web application code from within a main() method is often worthwhile. Much of the business logic within a web application can still be called from outside the web container. Much time can be saved by testing as much as possible outside the container or using a mock framework of some sort to test these components. You can also leverage in-container testing frameworks like Cactus to help you accomplish your goals.

Agile Approaches

Agile developers are some of the biggest proponents of unit testing and as such many support the idea of test-first development. Test-first development is a great way to ensure that a developer fully understands what he or she is creating before actually coding it. Generally, this testing process can be automated using a testing framework such as JUnit. Of course, all this testing can come at a price.

Not all testing frameworks are created alike, so developer training or some system configuration may be necessary before test-first development becomes seamless within your environment. Also, if the development effort is taking advantage of an application framework such as a portal or e-commerce server, the testing framework may not integrate well within the environment. Finally, to effectively conduct test-based development the entire team needs to be skilled enough to design and code appropriately.

Many developers or architects when initially confronted with the idea of unit testing and JUnit are convinced that it is the way to go. I know that I have spent many hours looking for the right approach to unit and in-container testing. While these strategies are valid, one person learning a new approach versus trying to get an entire team to adopt a strategy and perform it correctly are two different things. Be cautious in this area and be sure that what you propose is doable and will add value to the project.

  • + Share This
  • 🔖 Save To Your Account