200 likes | 353 Views
S pecial I nterest G roup on C omputer S cience E ducation. Test-Driven Design for Introductory OO Programming. ACM SIGCSE’09 , March 3–7, 2009, Chattanooga, Tennessee, USA. Viera K.Proulx College of Computer Science Northeastern University Boston, MA.
E N D
Special Interest Group on Computer Science Education Test-Driven Design for Introductory OO Programming ACM SIGCSE’09, March 3–7, 2009, Chattanooga, Tennessee, USA. Viera K.Proulx College of Computer Science Northeastern University Boston, MA
Does Your Program Perform as Expected? • We never know until we test it. • But WHEN do you start planning your test, • BEFORE writing the program or • AFTERthe program has been written? • The professional programming community has realized that if we plan our test AFTERthe coding stage, we often tailor our tests to the code, not the original problem statement. • Thus, TDD has been developed to address this problem.
Test-Driven Design • TDD has been shown to • increase the productivityof programming teams and • improve the qualityof the code. However, most of the introductory classes provide • NO definition of WHAT IS TEST and • NO help on HOW TO DESIGN YOUR TEST WHY??? TDD sounds GREAT!!!
Why don’t educators introduce TDD into the classes? • As a beginner, there are so much to learn. • Syntax, grammar , inheritance, polymorphism…… • To introduce TDD means they have to learn extra stuffs like: • Whole new design concept which is more advanced than the traditional ones. • A new testing framework such as JUnit, which provides full functionalities for • Defining test cases and • Evaluating the test results.
http://www.chemistryland.com/CHM151S/00-Intro/02-Succeed/Obstacles/StepsLeftOut.jpghttp://www.chemistryland.com/CHM151S/00-Intro/02-Succeed/Obstacles/StepsLeftOut.jpg
The benefits of developing testing skills • By developing testing skills hand-in-hand with programming skills, the students can • Practice good programming style • Since they have to make their code test-ableand easy-to-test. • Understand the programming concepts they are learning. • Since they have to know what to test.
Introduce test concept to novice programmer • We can start from this simple example: We want to test if this method works as we expected it to be.
Test design in the functional style For the beginners, forget about the complex assertEqualsmethod in JUnit framework, we can define our own assertEquals( checkExpecthere ) with easy-to-use functionality.
Transition to the real world • The previous approach works for simple class with simple data structures. • However, when it comes to testing complex data structures like a Binary Search Tree (BST), we got to redesign our testing strategy. • Think about evaluating several testscomparing 2 BSTs with 6 nodes, 7 leaves, and where the data in each node consists of 3 fields……
Time to bring in the testing library • We can design a simple testing library to address the previous problem. • The key features of this simple testing library: • Automating the comparison of the values of arbitrary instances of all classes. • Produce the test results with display of the actual and expected values of each failed test.
Let’s take a look at an example of the simple testing library ( called “tester” )
This is the class we want to test Actually, what we really want to test is this functionality.
This is the testing class The testing library package Each test case is specified as an invocation of a method in the Tester class. Several test cases are typically grouped together in a method with a name that starts with “test”.
The testing report Class fields and the corresponding values Number of tests performed and number of failed tests For each failed test, display the actual and expected values
User defined equality • If students want to define the equality by themselves, they can implement the same method from the ISameinterface: • The “tester” will compare two objects in a class if it implements the ISame interface.
Summary (1/2) • It’s all about standing in the student’s shoes. • When it comes to teaching the concept of testing, experienced programmers might just want to tell the rookies to use tools like JUnit. • However, it is a very BIG step for the rookies to cross basic OO knowledge to complex unit testing framework.
Summary (2/2) • This paper provides a step-by-step approach to introduce the testing concept to students: • In the beginning, they know nothing about testing. • Then they know how to test their class in functional way. • For complex test cases, the author suggests the instructor to provide an easy-and-simple-to-use testing library, teaching the students the concept of testing automation. • For advanced purposes, the students can also define the equality comparison by themselves through ISame interface implementation. • Hopefully by following these works the students can transit smoothly to using professional tools like JUnit.