Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Testing With JUnit.

Similar presentations


Presentation on theme: "Computer Science 209 Testing With JUnit."— Presentation transcript:

1 Computer Science 209 Testing With JUnit

2 Why Test? I don’t have time, I’ve got a deadline to meet
The more pressure I feel, the fewer tests I will write Fewer tests lead to less accurate and less stable code Less accurate and less stable code puts more pressure on me . . .

3 Integrate Testing and Coding
Code a little, test a little, code a little, test a little . . . Write test units before coding the pieces to be tested Get immediate feedback and fix bugs as you code

4 Tester Programs Test individual classes where possible, then interactions Bottom-up approach A main method displays expected results before actual results of running methods

5 Testing Freecell Lots of things to test!
public class FreeCellTester{ public static void main(String[] args){ FreeCellModel = new FreeCellModel(); System.out.println(model); } // Now what?? Lots of things to test! Smaller pieces (deck, cards, particular types of cells) should be tested first

6 Problems With Tester Programs
Must visually inspect a long output stream to check results (tedious and error-prone) Must create code to produce visually useful output Tests of methods are not modularized: One long sequence that is hard to maintain

7 JUnit Provides “outside influence” in the form of a framework for unit testing Highly modular, can create a suite of test cases for each class and add to them incrementally Assertion-based: A failed test halts with an error message, so verification is automated

8 The Basic Framework Write a testing class that extends TestCase
import junit.framework.*; public class CardTest extends TestCase{ // Will test the Card class using JUnit } Write a testing class that extends TestCase TestCase is defined in the junit.framework package You should write a similar class for each of the classes that are unit-tested in your system

9 Add a Test Case import junit.framework.*; public class CardTest extends TestCase{ public void testgetRank(){ Card c = new Card(2, Suit.spade); assertTrue(c.getRank() == 2); assertTrue(c.getRank() == 3); } A test case is a method that tests the functionality of another method testGetRank is the test case for getRank assertTrue expects true and halts the test if it gets false

10 Testing equals import junit.framework.*; public void testequals(){ Card c1 = new Card(2, Suit.spade); Card c2 = new Card(3, Suit.heart); assertEquals(c1, c1); assertEquals(c1, new Card(2, Suit.spade)); assertTrue(! c1.equals(c2)); } assertEquals runs the equals method with its two parameters Be sure to test all of the possible options of equality

11 Establish a Fixture import junit.framework.*; public class CardTest extends TestCase{ private Card c1, c2; protected void setUp(){ c1 = new Card(12, Suit.spade); c2 = new Card(12, Suit.heart); } // Tests for getRank and equals can use c1 and c2 A fixture captures a common context for several test cases This context consists of a set of objects that can be initialized once in a setup method and made visible to all test cases Some fixtures can consist of many objects, so this really saves time

12 Create a Suite of Test Cases
import junit.framework.*; public class CardTest extends TestCase{ // Code for fixture and test cases public CardTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(); suite.addTest(new CardTest("testgetRank")); suite.addTest(new CardTest("testequals")); return suite; public static void main(String[] args){ junit.textui.TestRunner.run(suite()); Create an instance of CardTest for each test case method and pass the name of the method to the constructor Add each case to a TestSuite object Run the suite in the main method Be sure that the names of the test case methods are spelled correctly!

13 Create a Suite of Test Cases
import junit.framework.*; public class CardTest extends TestCase{ // Code for fixture and test cases public CardTest(String s){ super(s); } public static Test suite(){ TestSuite suite = new TestSuite(CardTest.class); return suite; public static void main(String[] args){ junit.textui.TestRunner.run(suite()); Create an instance of CardTest for each test case method and pass the name of the method to the constructor Add each case to a TestSuite object Run the suite in the main method Alternatively, pass the class of the test cases to another Suite constructor

14 Running a Test For a simple batch-style text interface:
>java PatronTest For a GUI that allows you to track and run tests for multiple classes: >java junit.swingui.TestRunner

15 Guidelines for Testing
Write tests as you develop, when you imagine how the code will run Factor the test code to reflect the factoring of your system Keep the old tests running, and always run all of your tests

16 Guidelines for Testing
Avoid writing print statements; write tests instead Focus on tests that are useful When you add functionality to the system During debugging, write a test that will succeed if the code works, then debug with it

17 Guidelines for Testing
Run every test at least once a day Set them up so that you can’t go home unless they’re all 100% You’ll be more aggressive about refactoring because you won’t fear breaking the code


Download ppt "Computer Science 209 Testing With JUnit."

Similar presentations


Ads by Google