CSC/ECE 517 Fall 2010/ch1 2e RI: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 34: Line 34:
   public void setName(String newName) {
   public void setName(String newName) {
     name = newName;
     name = newName;
  }
}
class UserTest extends TestSuite {
  public void testNameProperty() {
    User user = new User();
    assertNull(user.getName());
    String testName = "test";
    user.setName(testName);
    assertEquals(testName, user.getName());
   }
   }
  }
  }

Revision as of 12:16, 21 September 2010

Writing Meaningful Test case

Introduction

When testing an application, whether at an object or class level with unit testing, validating object interaction through integration testing, or in system test with various functional testing methods, many fail to execute against the most integral system components. Even with almost 100% code coverage, many of the test developed focus on assertions that return inaccurate test results. Writing meaningful test cases aims at developing test for the most valuable system components along with selecting proper assertions.

Unit Test

The purpose of the unit test process is to ensure each line of code for a module functions correctly to meet functional and technical requirements. In Test Driven Development, test code is developed before or alongside application code. Automated unit tests execute application code after it is built and provide reports on test results. Furthermore, unit tests are a critical tool for any developer. They allow us to quickly and easily test our code in a rerunable, maintainable way.

Advantages and Limitations

Advantages

Unit testing provides the following advantages

  • Catch bugs at time of implementation by testing as you develop. Small units of work are easier to code and debug. If you write all of your code and start testing when coding is complete, then testing will be much more difficult.
  • Easily test changes in code. Because the unit test is designed to test the business case, if the technical implementation changes, the unit test can be rerun to ensure that the technical redesign has not changed the program's result.
  • Prove to your supervisor and yourself that the code works after refactoring or adding functionality
  • Verify 'corner' cases that may not be tested within the System Test phase
  • Demonstrate a level of quality to the client
  • Ensure that no other developer has undermined the quality of your code

Limitation

Software testing is a combinatorial problem. For example, every boolean decision statement requires at least two tests: one with an outcome of "true" and one with an outcome of "false". As a result, for every line of code written, programmers often need 3 to 5 lines of test code.[3] This obviously takes time and its investment may not be worth the effort. There are also many problems that cannot easily be tested at all – for example those that are nondeterministic or involve multiple threads. In addition, writing code for a unit test is as likely to be at least as buggy as the code it is testing.

Best Practices

Testing Trivial Code

Unit tests should test those segments of code that are likely to have defects when first developed, or are likely to have defects introduced when changed. Like all software development activities, there is a cost benefit analysis that can be applied to writing unit tests. For normal enterprise business software, it is not worthwhile to test trivial code.

Typical examples of trivial code in Java include simple getter and setter methods for properties and simple constructors.

class User {
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String newName) {
    name = newName;
  }
}

class UserTest extends TestSuite {
  public void testNameProperty() {
    User user = new User();
    assertNull(user.getName());

    String testName = "test";
    user.setName(testName);
    assertEquals(testName, user.getName());
  }
}