Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.

Similar presentations


Presentation on theme: "Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease."— Presentation transcript:

1 Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease of writing and running JUnit test cases and test suites.

2 Summary Summary 1. What is JUnit? 2. Why use a Testing Framework? 3. Step One: Write a Test Case 4. Step Two: Write a Test Suite 5. Step Three: Organize the Tests 6. Step Four: Run the Tests 7. Testing Idioms 8. Resources 1. What is JUnit? 2. Why use a Testing Framework? 3. Step One: Write a Test Case 4. Step Two: Write a Test Suite 5. Step Three: Organize the Tests 6. Step Four: Run the Tests 7. Testing Idioms 8. Resources

3 What is JUnit? What is JUnit? A Framework: implements testing in Java Explicit Testing: Provides a simple way to test specific areas of an application. Extensible: Can be employed to test a hierarchy of program code either singularly or as multiple units A Framework: implements testing in Java Explicit Testing: Provides a simple way to test specific areas of an application. Extensible: Can be employed to test a hierarchy of program code either singularly or as multiple units

4 Why Use a Testing Framework? Why Use a Testing Framework? Forces you to explicitly declare the results of specific program execution route Aids debugging by writing a test which expresses the desired result and then debug until the test succeeds Comprehensive test suites for core project components make it possible to immediately see the effect of modifications Forces you to explicitly declare the results of specific program execution route Aids debugging by writing a test which expresses the desired result and then debug until the test succeeds Comprehensive test suites for core project components make it possible to immediately see the effect of modifications

5 Why Use JUnit? Why Use JUnit? JUnit tests promotes faster coding while increasing code quality It’s elegantly simple: Running tests is as fast and easy as compiling your code. JUnit tests check their own results and provide immediate feedback. JUnit tests can be composed into a hierarchy of test suites. JUnit tests increase the stability of software. JUnit tests are developer tests. JUnit tests are written in Java. JUnit integrates with Ant to automate testing. JUnit is Open Source and Free! JUnit tests promotes faster coding while increasing code quality It’s elegantly simple: Running tests is as fast and easy as compiling your code. JUnit tests check their own results and provide immediate feedback. JUnit tests can be composed into a hierarchy of test suites. JUnit tests increase the stability of software. JUnit tests are developer tests. JUnit tests are written in Java. JUnit integrates with Ant to automate testing. JUnit is Open Source and Free!

6 Step One: Write a Test Case Step One: Write a Test Case To write a test case: 1. Define a subclass of TestCase. 2. Override the setUp() method to initialize object(s) under test. 3. Override the tearDown() method to release object(s) under test. 4. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. 5. Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. 6. Optionally define a main() method that runs the TestCase in batch mode. To write a test case: 1. Define a subclass of TestCase. 2. Override the setUp() method to initialize object(s) under test. 3. Override the tearDown() method to release object(s) under test. 4. Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results. 5. Define a static suite() factory method that creates a TestSuite containing all the testXXX() methods of the TestCase. 6. Optionally define a main() method that runs the TestCase in batch mode.

7 Step Two - Write a Test Suite Step Two - Write a Test Suite To write a test suite: 1. Define a subclass of TestCase. 2. Define a static suite() factory method that creates a TestSuite containing all the tests. 3. Optionally define a main() method that runs the TestSuite in batch mode. To write a test suite: 1. Define a subclass of TestCase. 2. Define a static suite() factory method that creates a TestSuite containing all the tests. 3. Optionally define a main() method that runs the TestSuite in batch mode.

8 Step Three - Run the Tests Step Three - Run the Tests Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. JUnit provides both a textual and a graphical user interface. 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. Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. JUnit provides both a textual and a graphical user interface. 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.

9 Step Three - Run the Tests Step Three - Run the Tests In general, TestSuite and TestCase classes should define a main() method which employs the appropriate user interface. The tests we've written so far have defined a main() method employing the textual user interface. To run our test case using the textual user interface as defined by the main() method, use: java com.company.test.ShoppingCartTest Alternatively, the test case can be run with the textual user interface using: java junit.textui.TestRunner com.company.test.ShoppingCartTest or with the Swing GUI using: java junit.swingui.TestRunner com.compant.test.ShoppingCartTest In general, TestSuite and TestCase classes should define a main() method which employs the appropriate user interface. The tests we've written so far have defined a main() method employing the textual user interface. To run our test case using the textual user interface as defined by the main() method, use: java com.company.test.ShoppingCartTest Alternatively, the test case can be run with the textual user interface using: java junit.textui.TestRunner com.company.test.ShoppingCartTest or with the Swing GUI using: java junit.swingui.TestRunner com.compant.test.ShoppingCartTest

10 Step Four - Organize the Tests Step Four - Organize the Tests The last step is to decide where the tests will live within our development environment. Here's the recommended way to organize tests: 1. Create test cases in the same package as the code under test. 2. Avoid combining application and testing code in your source directories. 3. Define a TestSuite class per package. 4. Define “Master TestSuites.” 5. Integrate testing into a daily build process. The last step is to decide where the tests will live within our development environment. Here's the recommended way to organize tests: 1. Create test cases in the same package as the code under test. 2. Avoid combining application and testing code in your source directories. 3. Define a TestSuite class per package. 4. Define “Master TestSuites.” 5. Integrate testing into a daily build process.

11 Step Four - Organize the Tests Step Four - Organize the Tests By creating a TestSuite in each Java package, at various levels of packaging, you can run a TestSuite at any level of abstraction. Example: Define a com.mydotcom.AllTests that runs all the tests in the system and com.mydotcom.ecommerce.EcommerceTestSuite that runs only those tests validating the e-commerce components. The testing hierarchy can extend to an arbitrary depth. By creating a TestSuite in each Java package, at various levels of packaging, you can run a TestSuite at any level of abstraction. Example: Define a com.mydotcom.AllTests that runs all the tests in the system and com.mydotcom.ecommerce.EcommerceTestSuite that runs only those tests validating the e-commerce components. The testing hierarchy can extend to an arbitrary depth. Example Test Hierarchy: AllTest (Top-level Test Suite) SmokeTestSuite (Structural Integrity Tests) ECommerceSuite ShoppingCartTestCase CreditCardTestCase CAuthorizationTestCase CaptureTestCase VoidTestCase UtilityTestSuite MoneyTestCase DatabaseTestSuite ConnectionTestCase TransactionTestCase LoadTestSuite (Performance and Scalability Tests) DatabaseTestSuite ConnectionPoolTestCase ThreadPoolTestCase

12 Testing Idioms Testing Idioms The software does well those things that the tests check. Test a little, code a little, test a little, code a little... Make sure all tests always run at 100%. Run all the tests in the system at least once per day (or night). Tests areas of code with highest probability of breakage. The software does well those things that the tests check. Test a little, code a little, test a little, code a little... Make sure all tests always run at 100%. Run all the tests in the system at least once per day (or night). Tests areas of code with highest probability of breakage.

13 Testing Idioms Testing Idioms Write tests that have the highest possible return on your testing investment. If you find yourself debugging using System.out.println(), write a test to automatically check the result instead. When a bug is reported, write a test to expose the bug. The next time someone asks you for help debugging, help them write a test. Write unit tests before writing the code and only write new code when a test is failing. Write tests that have the highest possible return on your testing investment. If you find yourself debugging using System.out.println(), write a test to automatically check the result instead. When a bug is reported, write a test to expose the bug. The next time someone asks you for help debugging, help them write a test. Write unit tests before writing the code and only write new code when a test is failing.

14 Resources Resources JUnit homepage - http://www.junit.org “Java Tools for eXtreme Programming” JUnit homepage - http://www.junit.org “Java Tools for eXtreme Programming”


Download ppt "Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease."

Similar presentations


Ads by Google