Algorithms and OOD (CSC 207 2014S) : Outlines
Held: Friday, 24 January 2014
Back to Outline 03 - An Overview of Java.
On to Outline 05 - Classes and Objects.
We begin to explore the process of unit testing, focusing on the
- A few thoughts on testing.
- An example: Testing exponentiation.
- A few notes on test-driven development.
- Class goes faster if you check out the class code when you arrive in the room.
- I sometimes forget to update the lists of readings/labs and the link
to the current reading.
- You should let me know if you don't see them online.
- You can always get them from the schedule.
- I think I've responded to all of the writeups I've received.
- Lab writeup: Exercises 3f and 4c.
- Subject: CSC 207 Writeup 3: Unit Testing (YOUR NAME(s) HERE)
- Due before class on Monday.
- Reading on classes for Monday's class.
- Notes from the review session are posted under eboards.
- HW 2 is due next Wednesday at 10:30 p.m.
- Groups (2-4) required, but you can choose partners.
- Extra credit:
- CS Table Friday at noon: 3D Printing (of body parts?).
- Theatre Gigante Friday evening.
- Anything else?
- Happy 40th anniversary Mac!
A Brief Introduction to Unit Testing
- How do you know your code works?
- Typically, you run "experiments" on your code ("When I give this input,
I should get this output. Do I?")
- Many people call these "tests", but I try to distinguish the two.
- But experiments are not a great approach.
- A human is doing the comparison; that's a job better suited to
- You are unlikely to document your experiment (or at least your
expected output), which means that you'll have to regenerate it latter.
- You are likely to have to build a framework to do your experiments.
- Looking at output by hand means that you generally won't conduct
- What's alternative? Tests.
- A test typically checks whether a function has an expected output
on a given input (just like an experiment, except that the computer
does the comparison).
- A test is represented by executable code. Running it again is
easy (and it's often self-documenting).
- Tests generally have only one of two outputs: Success or
"expected A, got B".
- Since the computer does most of the work, and since the tests persist,
you are comfortable writing bigger tests and running them more often.
- In courses, you can share tests, so your code gets challenged by
- It takes a long time to become good at writing tests. We'll develop
those skills across the semester.
- In Java, JUnit is one of the popular frameworks for doing this kind
of testing. And it's nicely integrated into Eclipse.
- I'm not going to discuss JUnit because you'll learn more writing test
code than watching me write test code.
An Example: Testing Exponentiation
What tests would you write for a function that computes x^n, where
x and n are integers?
- Unit testing is a key aspect of many agile software development
- Often, unit testing is a key aspect of test-driven development (TDD).
- There are two general approaches to TDD.
- Repeatedly write a test and make your procedure meet that test
until you can't think of any more tests to write.
- Write a lot of tests up front and then write your procedure.
Then write a few more to stress your particular implementation
(or because you've learned something).
- I prefer the latter. I find that if you write a lot of tests in advance,
you have a better sense of what the procedure is supposed to do.
Lab: Unit Testing
Do the lab.
Be prepared to reflect.