1 / 25

JUnit

JUnit. Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures. Objectives: . What is Unit Testing?. Writing tests for small, discretely-defined portions of code Run and Analyze the Tests Make it part of Development Process Run on a Regular Basis

tiptonm
Download Presentation

JUnit

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. JUnit Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures Objectives:

  2. What is Unit Testing? • Writing tests for small, discretely-defined portions of code • Run and Analyze the Tests • Make it part of Development Process • Run on a Regular Basis • Result is better confidence in the quality of Code

  3. Why Unit Test? • Increase Robustness of your Code • Reduce the time and cost of delivering a system • Measure your progress of development • Early discovery of regressions • Identify unintended side effects • Focus your development effort

  4. What is JUnit? • Open source framework for unit testing written by Erich Gamma and Kent Beck. • Assertions for testing expected results • Test fixtures for sharing common test data • Test suites for easily organizing and running tests • Based on Annotations • Textual test runner • org.junit.runner.JUnitCore

  5. JUnit Goals • Provide a framework that encourages developers to write tests • Overcome the “lack of time” argument • Use familiar tools • Create tests that retain their value over time • Test should be reusable by others • Leverage existing tests to create new ones • Test Fixtures

  6. Writing Testing Code • Debugger Statements • Runtime Expressions • Print Statements • Test Scripts • Tests as Objects • Familiar to Developers • Established Methodology

  7. Writing a Test Case • Steps for running a Test Case using JUnit • Define a class to contain test methods • Annotate test methods with @Test • To check a value, call assertTrue() and pass a boolean that is true if the test succeeds

  8. Simple Test Case import java.util.*; import org.junit.Test; import static org.junit.Assert.*; public class MathTest { @Test public void addition() { double result= 3 + 2; assertTrue(result == 5.0); } }

  9. Alternative Ways to Execute the Test • Test Suite import org.junit.runner.*; import org.junit.runners.*; import static org.junit.runners.Suite.*; @RunWith(value=Suite.class) @SuiteClasses(value={MathTest.class}) public class AllTests { ... // this class can be empty }

  10. TestCase Lifecycle • Methods marked with @Before • Invoked prior to each test • Methods marked with @After • Invoked after each test • For each test method in a TestCase class, the JUnit framework performs these basic steps: • Create a new instance of the TestCase class • Invoke @Before method(s) of the TestCase • Invoke the test method • Invoke @After method(s) of the TestCase • Release the reference to the TestCase instance, which then becomes garbage

  11. Class Scoped set-up and tear-down • static methods marked with @BeforeClass • Run exactly once before all the test methods in the class run • static methods marked with @AfterClass • Run exactly once after all the test methods in the class run • For expensive initializations and clean-ups • Database connections • Network connections • Instead of re-creating before each and every test, create it only once and tear it down once • Test cases can run much faster

  12. Fixtures • Used to establish baseline testing environment • Can be shared by many different tests and test cases. • To create a fixture: • Create a TestCase class • Add an instance variable for each part of the fixture • Provide @Before method to initialize the variables • Provide @After method to release any permanent resources you allocated in initialization

  13. Fixture Example import java.util.*; import org.junit.*; import static org.junit.Assert.*; public class MathTest { protected double fVal1; protected double fVal2; @Before public void setUp() { fVal1 = 2.0; fVal2 = 3.0; } @Test public void addition() { double result= fVal1 + fVal2; assertTrue(result == 5.0); } }

  14. Test Code • Contained within test cases • Test methods must be public, must take no arguments, and must return void. • Typically named testXXX public void testEmptyCollection(){} public void testOneItemCollection(){}

  15. Determining Success or Failure • Part of testing is to run and analyze the test to determine success or failure • Assert Methods throw AssertionError • assertEquals() • assertTrue() • assertNotNull() • Several overloaded versions in org.junit.Assert • Uncaught exceptions are captured by test runner

  16. Testing Exceptions • Use annotation to declare expected exceptions • If exception is not thrown (or a different exception is thrown), the test will fail. @Test(expected=ArithmeticException.class) public void divideByZero() { int n = 2/0; }

  17. Ignored Tests • Use annotation to declare ignored tests • For excessively complex or slow tests not currently required @Ignore public void testInternet() { // screenscrape all web pages }

  18. Timed Tests • Use annotation to declare timeout parameter • If test takes longer than the specified number of milliseconds to run, the test fails. @Test(timeout=500) public void retrieveData() { // database access }

  19. Test Design • Create Repeatable Tests • Run Unit Test after code modification • Guard against regressions • “code a little, test a little” • Write tests as you develop • Write tests to help you debug • Constantly check your tests against your code

  20. What to test • Critical Areas of your code • Parts that are most likely to break • get/set methods? • Forwarding methods? • JSP with no business logic? • Refactored Code • Code that changes the value of an object or creates some kind of side effect.

  21. Test Suite • Runs more than one test at the same time • Programmatic container for TestCase classes • Runs test cases as a logical group • Can contain other TestSuites

  22. Creating a Test Suite import org.junit.runner.*; import org.junit.runners.*; import static org.junit.runners.Suite.*; @RunWith(value=Suite.class) @SuiteClasses(value={MathTest.class, AnotherTest.class}) public class AllTests { @BeforeClass public static void… ... @AfterClass public static void… }

  23. Test Runner - JUnitCore • Executes tests • Text output • Period (.) indicates passed test • .E indicates failed test • .I indicates ignored test • JUnitCore will run any number of test classes java –classpath .;junit-4.4.jar org.junit.runner.JUnitCore TestA TestB TestC

  24. Test Runner Output • Text Output

  25. Summary • Test Options • JUnit Testing Framework • TestRunners • Test Cases and Test Suites • Test Fixtures

More Related