Why do we need software testing?

Why do we need to test our program?

When people talk about the importance of software testing, common examples they give frequently are military software, aircraft, etc. That is not concrete enough to understand why we need to test our software.

Here I provide an example from daily programming work. You don't need to work for a military project to understand why we need software testing.

The example provided here is capitalize(String str) method from org.apache.commons.lang3.StringUtils which is from Apache Commons Lang library.

The following method capitalizes a given string. Suppose you write this method for your project. How do you know if this method is reliable and always return what it should return? We need to test! That is, given all different kinds of input, we see if it returns expected results. The returned results can either be good or bad, but they should be expected.

public static String capitalize(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    return new StringBuilder(strLen)

The testing code for this method is below. I have removed the code for testing other methods.

The testing code tests different kinds of inputs, such as null, an empty string, a single-char-string, and results of another method. In this way, we can assert this method is reliable.

public void testCaseFunctions() {
    assertEquals(null, StringUtils.capitalize(null));
    assertEquals("capitalize(empty-string) failed", "", StringUtils.capitalize("") );
    assertEquals("capitalize(single-char-string) failed", "X", StringUtils.capitalize"x") );
    // reflection type of tests: Sentences.
    assertEquals("capitalize(uncapitalize(String)) failed",
                     SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)) );
    // reflection type of tests: One word.
    assertEquals("capitalize(uncapitalize(String)) failed",
                     FOO_CAP, StringUtils.capitalize(StringUtils.uncapitalize(FOO_CAP)) );

The assertEquals method is from JUnit framework which is the defacto standard of unit testing.


1. Commons Lang JavaDoc
2. JUnit assertEquals method

Category >> Software Testing  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. ccna training in pune on 2016-11-19

    Thanks for posting this useful content, Let me share this, . CCNA training in pune

Leave a comment