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

15
Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II 1
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

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

CSE 116 Introduction to Computer Science for Majors II 1

Cell phones off

Name signs out – congrats Sean!

CSE 116 Introduction to Computer Science for Majors II 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 II 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 II 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 II 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 II 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 II 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 CourseRosterresign(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 CourseRosterresigns() - returns the number of Students in CourseRoster marked as resignedmaxEnrollment() – returns the maximum number of spots available

CSE 116 Introduction to Computer Science for Majors II 8

On to Eclipse!

CSE 116 Introduction to Computer Science for Majors II 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 II 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.

CSE 116 Introduction to Computer Science for Majors II 11

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’.

CSE 116 Introduction to Computer Science for Majors II 12

Let Eclipse add the JUnit4 library to the project’s build path.

CSE 116 Introduction to Computer Science for Majors II 13

Eclipse generates a skeletal class definition for ‘ClassRosterTests’.

CSE 116 Introduction to Computer Science for Majors II 14

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.

CSE 116 Introduction to Computer Science for Majors II 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!