1 / 12

Presentation Outline

Presentation Outline. What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests TestCase TestSuite Organizing The Tests Running The Tests. What is JUnit ?.

jeb
Télécharger la présentation

Presentation Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests TestCase TestSuite Organizing The Tests Running The Tests

  2. What is JUnit ? • JUnit is an open source Java testing framework used to write and run repeatable tests. • It is an instance of the xUnit architecture for unit testing frameworks. • It is used by the developer who implements unit tests in Java. • JUnit is Open Source Software, released under the IBM's Common Public License Version 1.0 and hosted on SourceForge • The official JUnit home page is http://junit.org/.

  3. Why Use JUnit? • JUnit tests allow you to write code faster while increasing quality. • JUnit is elegantly simple. • JUnit tests check their own results and provide immediate feedback. • JUnit tests can be composed into a hierarchy of test suites. • Writing JUnit tests is inexpensive. • JUnit tests increase the stability of software. • JUnit tests are developer tests. • JUnit tests are written in Java. • JUnit is free.

  4. JUnit Features • Assertions for testing expected results • Test fixtures for sharing common test data • Test suites for easily organizing and running tests • Graphical and textual test runners

  5. Design of JUnit JUnit is designed around two key design patterns: • The Command pattern and • A TestCase is a command object. • Any class that contains test methods should subclass the TestCase class. • A TestCase can define any number of public testXXX() methods. • When you want to check the expected and actual test results, you invoke a variation of the assert() method. • The Composite pattern • TestCase instances can be composed into TestSuite hierarchies that automatically invoke all the testXXX() methods defined in each TestCase instance. • A TestSuite is a composite of other tests, either TestCase instances or other TestSuite instances. • The composite behavior exhibited by the TestSuite allows you to assemble test suites of test suites of tests, to an arbitrary depth, and run all the tests automatically and uniformly to yield a single pass or fail status.

  6. Downloading JUnit • The latest version of JUnit is available at http://download.sourceforge.net/junit/ • Setting the classpath C:\junit3.8.1\junit.jar; • The following documents are included in the JUnit distribution in the doc directory: • JUnit Test Infected: Programmers Love Writing Tests • Unit Cookbook • JUnit - A Cook's Tour • JUnit FAQ

  7. Steps for writing TestCases • Define a subclass of TestCase. • Override the setUp() method to initialize object(s) under test. • Override the tearDown() method to release object(s) under test. • Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. • Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. • Optionally define a main() method that runs the TestCase in batch mode.

  8. Example of Writing a TestCase import java.util.*; import junit.framework.*; public class CmpE287 extends TestCase { //define the parameters public CmpE287(String name) // construct CmpE287 with a specified name { super(name); } protected void setUp() { // Set up the test fixture which is called before every test case method. } protected void tearDown() { // Tears down the test fixture which is called after every test case method. (optional) }

  9. Example of Writing a TestCase (cont.) public void testInsert() { // tests the insert() method } public void testDelete() { // tests the delete() method } public static Test suite() { // Assembles and returns a test suite for all the test methods of this test case. TestSuite suite = newTestSuite(CmpE287.class); return suite; } public static void main(String args[]) // run the test { junit.textui.TestRunner.run(suite()); } }

  10. Organizing The Tests • Create test cases in the same package as the code under test. • To avoid combining application and testing code in your source directories, create a mirrored directory structure aligned with the package structure that contains the test code. • For each Java package in your application, define a TestSuite class that contains all the tests for validating the code in the package. • Define similar TestSuite classes that create higher-level and lower-level test suites in the other packages (and sub-packages) of the application. • Make sure your build process includes the compilation of all tests. This helps to ensure that your tests are always up-to-date with the latest code and keeps the tests fresh.

  11. Running the Tests • JUnit provides both a textual and a graphical user interface. • Both user interfaces indicate how many • tests were run • any errors or failures, and • a simple completion status (time). • The textual user interface • junit.textui.TestRunner • displays "OK" if all the tests passed and • failure messages if any of the tests failed. • The graphical user interface • junit.swingui.TestRunner • displays a Swing window with a green progress bar if all the tests passed or • a red progress bar if any of the tests failed. • E.g.C:\junit3.8.1\ java junit.swingui.TestRunner CmpE287

  12. Example of graphical user interface

More Related