Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
How to make SUnit Tests- Daniel Kester
SUnits are a nice way to test your code quickly and make sure it all works the way you want it to.
1. Make a new class.
2. Overwrite Set Up
- This class must be a subclass of TestCase (XProgramming.SUnit.TestCase).
- Style wise, your class name should be ______Test, where _____ is the class you are testing.
- The instance variables of your test class are going to be the objects you are using for your tests, so you will probably want at least 2 of each different kind of object involved.
3. Create Tests
- The method setUp is called before each test to set up all of your instance variables the way you want.
- Just use getters and setters here, since you haven't tested the other methods yet!
4. Run Tests
- Each new instance method should be called test_________. This is how the SUnit knows it is a test.
- You should have one test for each nontrivial method in your class.
- Some useful methods for SUnit tests:
- self assert:(truth statement) passes if the truth statement is true and fails if it is false.
- self deny:(truth statement) passes if the truth statement is false and fails if it is true.
- collection includes: object returns true if object is in collection and false otherwise.
- To run the tests, type TestRunner open. into your workspace.
- Pick a test class from the drop down menu and click run to only test that class.
- Click run all to test all classes at once.
- A method can have 3 results:
- passed: All the asserts and denies in the test method passed.
- failed: An assert or deny in the test method failed.
- error: There was an error trying to do the test method.
- To see what the problem with a method is, select it from the bottom drop down box and click debug to bring up its error message.
- Clicking refresh will reload your code, so if you made any changes they will be accounted for next time you run the tests.
- Try to account for every possible case with your tests. It's not much help if you only know your test works if you are adding a correct object into an existing collection.
- If you notice yourself making all of your setUp methods the exact same, try this instead. Make a class called DomainTest and create its setUp method and instance variables. Then make all of your specific test classes be subclasses of DomainTest. That way, all the instance variables are already created and you don't have to keep overwriting setUp. I don't know if there is some kind of bad style, but it works if you need to save some time.
- I know there is a parcel that will integrate the TestRunner into your system browser, but I don't know its name. If you really want it, ask your professor.
- If for some reason none of this works, make sure you have the SUnit and SUnit UI parcels installed.
- Make these tests early! They are much less useful if you have already spent hours debugging half your code than if you are just starting to test.
Link to this Page
- Cases last edited on 30 July 2011 at 2:33 am by r59h132.res.gatech.edu