Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1.

Similar presentations


Presentation on theme: "Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1."— Presentation transcript:

1 Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1

2 Test Driven Development (TDD) write tests before code tests are executable requirements/specifications use unit testing tool to automate testing –test suite run at the click of a button –test results verified by testing tool We will use JUnit 4, installed for Eclipse CSE 116 Introduction to Computer Science for Majors II2

3 Basic structure of test For a given computation: –determine the expected value (the value which should be produced if the software is working correctly) –determine the actual value (the value which the software is actually computing) –compare the two: if they agree, the test passes if they disagree, the test fails CSE 116 Introduction to Computer Science for Majors II3

4 JUnit report If all tests pass, JUnit “green bars” If one or more tests fail, JUnit “red bars” –A report is given for each failed test –Click on failed test to navigate to the test code CSE 116 Introduction to Computer Science for Majors II4

5 Writing tests before code Think of test cases before writing code –make sure tests are a reflection of WHAT code should do –test must NOT be a reflection of HOW code solves the problem Let Eclipse generate stub code for application code that is needed for tests to compile CSE 116 Introduction to Computer Science for Majors II5

6 Our problem Develop a registration system for a course offering. Courses can enroll up to a maximum number of students. Students can add/drop/resign a course. Remember: this is an exercise to review some key CSE115 concepts, as well as introduce TDD CSE 116 Introduction to Computer Science for Majors II6

7 Write tests for a class that must have the following functionality: A CourseRoster holds Students. Student constructor takes first name and last name of student, as Strings. For example: Student("Jane","Smith") CourseRoster has methods: CourseRoster(int maxEnrollment) – creates a CourseRoster with the indicated maximum enrollment add(Student x) - ensures that x is in CourseRoster, as long as there is room in the course drop(Student x) - ensures that x is NOT in CourseRoster resign(Student x) - if x is in CourseRoster, marks them as resigned - if x is NOT in CourseRoster, does nothing enrollment() - returns the number of Students in CourseRoster resigns() - returns the number of Students in CourseRoster marked as resigned maxEnrollment() – returns the maximum number of spots available CSE 116 Introduction to Computer Science for Majors II7

8 On to Eclipse! CSE 116 Introduction to Computer Science for Majors II8

9 What we did with Eclipse today Created a new Java project Added two packages to the ‘src’ folder: –code  this is where the code for the application will reside –tests  this is where the tests for the application will reside We added a JUnit4 test case to the tests package CSE 116 Introduction to Computer Science for Majors II9

10 10 We created a new project, ‘FA10-CSE116-TDD’, and added two packages, ‘code’ and ‘tests’. The ‘code’ package will be for the application code, while the ‘tests’ package will be for our JUnit tests.

11 CSE 116 Introduction to Computer Science for Majors II11 Next we added a JUnit test case. JUnit tests. Make sure the test case (which is really just a Java class) is in the ‘tests’ package; give it a name (here ‘ClassRosterTests’.

12 CSE 116 Introduction to Computer Science for Majors II12 Let Eclipse add the JUnit4 library to the project’s build path.

13 CSE 116 Introduction to Computer Science for Majors II13 Eclipse generates a skeletal class definition for ‘ClassRosterTests’.

14 CSE 116 Introduction to Computer Science for Majors II14 Now we are ready to start writing our very first unit test. A unit test is written as a void method with an empty parameter list; the ‘@Test’ annotation identifies this as a test method to the JUnit tool.

15 Next steps? Next class we will show what the innards of the test method looks like. The basic idea is to exercise the code we are planning on writing, asking it to compute an answer for us, and comparing that to the known correct answer. Stay tuned! CSE 116 Introduction to Computer Science for Majors II15


Download ppt "Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1."

Similar presentations


Ads by Google