Print Page | Close Window

Functional Testing

Printed From: One Stop Testing
Category: Types Of Software Testing @ OneStopTesting
Forum Name: Functional Testing @ OneStopTesting
Forum Discription: Discuss All that is need to be known about Functional Software Testing and its Tools.
URL: http://forum.onestoptesting.com/forum_posts.asp?TID=233
Printed Date: 17Jan2025 at 5:46am


Topic: Functional Testing
Posted By: Manoj
Subject: Functional Testing
Date Posted: 23Feb2007 at 4:07pm

This section is dedicated to implementing testing frameworks for performing functional tests against your code.

Unit Testing Using JUnit

The tool of choice in the Java software development industry for implementing Unit tests is http://junit.sourceforge.net/ - JUnit . Before diving into the details of building a JUnit test, let's review what JUnit is and what benefits it provides.

JUnit is a simple regression testing framework that enables you to write repeatable tests. Originally written by Erich Gamma and Kent Beck, it has been embraced by thousands of developers and has grown into a collection of unit testing frameworks for a plethora of technologies. http://www.junit.org/ - JUnit.org hosts support information and links to the other JUnit derivations.

JUnit presents the following benefits to your unit testing:

  • It helps you code faster. how many times have you written debug code inside your classes to verify values or test functionality? JUnit eliminates this by allowing you to write test cases that are closely related, but using centralized and external classes.
  • JUnit is simple. if you have to spend too much time implementing your test cases, you won't do it. Therefore the JUnit creators made it as simple as possible.
  • A single result. Rather than generate loads of reports, JUnit gives you a single pass/fail result; upon failure, it shows you the exact point where it failed.
  • A hierarchical testing structure. Test cases test specific functionality, while test suites execute multiple test cases. JUnit supports test suites of test suites, so when developers build test cases for their classes, it is easy to assemble them into a test suite at the package level — and then extrapolate that in parent packages, etc. The result is that a single top-level test execution can exercise hundreds of unit test cases!
  • JUnit tests are written by developers. The tests are written by the same person that wrote the code, so the tests accurately test the intricacies of the code that the developer knows can be problematic. This differs from a QA-written test that exercises the external functionality of the component or use case; this test exercises the internal functionality.
  • JUnit tests are written in Java, making the integration of test cases with code seamless.
  • JUnit is free. JUnit is open source and licensed under the http://www.opensource.org/licenses/cpl.php - Common Public License Version 1.0 so you are free to use it in your applications.

Now that you're fired up and ready to dive into JUnit (maybe I'm a little too excited, but you're going to have fun, I promise!), let's look at its architecture. The architecture of JUnit is really quite simple and can be described by two primary components: TestCase and TestSuite.

All code that tests the functionality of your class or classes must extend junit.framework.TestCase. It can implement one or more tests by defining public void methods that start with test and accept no parameters, for example:

public void testMyFunctionality() { ... }

If there are multiple tests, then you have the option of initializing and cleaning up the environment before and between tests by implementing the following two methods: setUp() and tearDown(). In setUp() you initialize the environment and in teardown() you clean up the environment. Note that these methods are called between each test to ensure that there are no side effects between test cases; they are truly independent.

Inside each TestCase "test" method, you can create objects, execute functionality, etc. and then test the return values of those functional elements against expected results. If the return values are not as expected then the test fails, otherwise it passes. The mechanism that JUnit employs to test actual values against expected values is a set of assert methods:

  • There are assertEquals() methods for each primitive type.
  • assertTrue() and assertFalse() test boolean values.
  • assertNull() and assertNotNull() test whether an object is null.
  • assertSame() and assertNotSame() test object equality.

In addition, a fail() method (that you can call anywhere in your test case) can immediately mark a test as failing.




Print Page | Close Window