Test Driven Devlopment
From BriansWiki
Contents |
[edit] Presenter
[edit] Paul Ken
Built CFUnit and tartan framework
[edit] Traditional Testing
Problems:
- Not comprehensive enough
- Test written after code is done
- Test written by other programmers
- Test based on out of date documentation
- Not automated
- One fix may cause other problems that go unseen
[edit] What is test driven development
Testing is not just about Testing It is not extreme development
[edit] Style of Development
- Exhaustive suite of programmer test
- No code without tests
- Write tests first
- Test determine the code
[edit] Exhaustive suite of programmer test
Programmer test exhibited behavior Define what it means for the code to work
Exhaustive suite of tests is implied since all code in the system is expected to have associated test
[edit] No Code without Test
No Features without tests Provides a safety net Little bit of test followed by just enough functionality to satisfy the test This is an incremental process Do the simplest thing that could possibly work, sometime this means harcoding but this doesn’t work over the long haul. Don’t worry about making sure that your tests have corresponding functionality Failing tests make a great to do list
[edit] Refactoring
The process of changing code without changing its external behavior; changing how it works, not what it does After a simple test is passed, go back and write a more complex test Tests provide a safety net to refactor
[edit] When to refactor
Dumplication Unclear intent Code Smell
- Comments
- Data class
- Duplicate code
- Inappropriate intimacy
- Large class
- Lazy class
- Long method
- Switch statements
- Shotgun surgery
[edit] cfcUnit
Written for ColdFusion Based on xUnit family , ie jUnit flexUnit, jsUnit etc.. Runs on cf 6.1
[edit] Overview
Test testCase Test Suite – collection of cases Assert TestFailure Test Result Test Listener
[edit] Writing as Test Case
A single class that extends he TestCase class Contains methods that follow this convention
- Begin with test
- Access public
- ReturnType Void
[edit] Fixture
- Setup
- teardown
The test case is another cfc that extends the TestCase class, creates the object, sets the values exposed by the class and then compares the results with the expected results. The test case class is smart enough to iterate over the class and run all tests with the prefix test.
[edit] Errors and failures
[edit] Two types
- Pass or fail – the test itself returned an unexpected result
- Failure or error – there was an external error generated (like file not found)
If a test case is not implemented, you should set the return value to "test not implemented".
The step by step development would be:
- Create Test CFC
- Add all the empty Tests for your intended class
- Run and fail to build a to do list
- Go back and implement the test methods one by one, Creating a fleshed-out test then implementing the method (lather, rinse, repeat).
[edit] Setup a test fixture
Add a setup function at the start of the test cfc and use it to initialize your component and set it to the variables scope in the cfc, then all the tests can use the same instance. The test function uses either an AssertTrue or AssertFalse to send the message to the CFUnit test Frame.
[edit] Organizing the test code
Create separate but identical hierarchies for your test cases.
There is also CFUnit Runs with Ant, but it is not as flexible as CFCUnit which can be extended and run in different clients.
CFCUnit includes Mach II, but it runs independent – it is only used by the test runner application.
Source: http://cfcunit.org Blog: http://www.pjk.us/paul
