| Selecting the tests to run using IExamples interface | |||||||||||||||||
Using the import tester.*;
public class Book {
String title;
String author;
int pages;
boolean hardcover;
public Book(){
this.title = "No Book";
this.author = "No One";
this.pages = 0;
this.hardcover = false;
}
public Book(String title, String author, int pages, boolean hardcover){
this.title = title;
this.author = author;
this.pages = pages;
this.hardcover = hardcover;
}
// compute the cost of this book
public int cost(){
return pages * (1 / 2);
}
// produce a book like this one, but with the given title
public Book changeTitle(String title){
return new Book(title, this.author, this.pages, this.hardcover);
}
// test the method 'changeTitle'
public void testTitleChange(Tester t){
t.checkExpect(this.changeTitle("New Title"),
new Book("New Title", this.author, this.pages, this.hardcover));
}
// test the method 'cost'
public void testCost(Tester t){
t.checkExpect(this.cost(), this.pages * (1 / 2));
}
}Following the basic variant, you would declare this class as the
example class either by using the Now let's change our class definition slightly: public class Book implements IExamples{ //...
and we add our tests method:
// run all tests
public void tests(Tester t) {
testCost(t);
testTitleChange(t);
}
And now the Tester will run this tests method.
It is now much simpler to see which tests are being run, and alter which tests are processed.
Simply comment out a test, and it will be ignored. This makes it much easier to maintain your test code!
Before, you would have had to find and comment out an entire test method; in larger classes
or classes that contain numerous tests, that can be an annoying process.
Keep in mind that now, any methods marked with the
@TestMethod
annotation are not processed as test methods; you must add these to this tests method as well.
Likewise, any test not contained within tests is ignored.
On the other hand, if you use the If you attempt to use the Java Annotations method of declaring tests, you must also be careful.
If a class you have marked with | ||||||||||||||||||
| last updated on Fri Apr 1 14:26:43 EDT 2011 | generated with DrRacket |