by for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15
Agenda TDD as an industrial practice TDD as a teaching tool Teaching TDD 2
Software Testing as a general practice Untested code generally doesn’t work. Informal survey showed that 50-75% of developers time is spent testing. Typically, making the testing more efficient will improve developer productivity. 3
Unit Testing - overview Testing of the smallest unit of behavior – Function – Method – Class Usually very fast – Mock out external resources (DB, network, etc.) – Sometimes mock internal code 4
TDD != Unit Testing Purpose of TDD is to push code in a specific way: – It must behave in accordance with the test. – It must be designed in a testable way. Purpose of unit testing is to find unit level bugs. 5
TDD Test Driven Development – Write the test. – Write the code. – Refactor the code. Some terms – Red- a failing test. – Green – a passing test. – Refactor – changing code in an effort to improve certain aspects without changing its behavior. 6
TDD Write a test – The test should fail since we haven’t written the code yet. Write the code – Write only enough code to get green. Refactor – Modify the code to be clean, efficient, stylized, etc. 7
TDD 8
Write a test Make sure you see red first. You know the test is actually testing something. – And not just tautologically passing. You’ve defined in unambiguous terms what you expect the code to do. 9
Write the code Write only as much code as you need in order to get the test to pass. Writing any more code than that would be creating untested code. 10
Refactor This is really a critical step since the code probably is ugly. You have sufficient test automation in place to assure that refactoring does not break existing functionality. 11
TDD Advantages The resulting code is testable. The code has a significant part of the unit test suite already constructed. We are in a good position to optimize the code and know if we will break it. The tests provide clear examples of how to use the code. 12
TDD Limits I’ve had a lot of success with TDD, but it doesn’t ‘work’ for every programming effort. It works really well when you have clear and relatively straight-forward requirements. I’ve not had much luck when the code is very complex. I found it difficult to incrementally build the code; I found it easier to ‘just build it.’ 13
TDD in the classroom Providing test cases 14 TDD is a good practice all SW engineers should know. Early on, I’ll provide the tests. – Clear requirements. – Like early math assignments, you get well defined problems to solve. Word problems (requirements -> test cases) come later. TDD encourages student to think about behavior of code in an observable way.
TDD as a learning tool Requiring test cases Having students create test cases first helps student get the assignment correct by breaking it down into observable behaviors. TDD tests provide clear example of what the code does. It helps in the grading process because it’s often easier to see the evolution of the code. 15
Teaching TDD Possible in nearly every language. Introduce concepts. Bootstrap the students. Walk them through the process. 16
TDD in C Really? Isn’t TDD a new approach and isn’t C an old language? Yes, Yes, Yes. 17
TDD in C By some measures, C is still the most popular language in use. C is not often thought of as an easily testable language, so showing TDD in C helps remove that misconception At the heart of many critical programs is C. It really needs to be tested well. 18
TDD Demo 19
Nunit Demo 20 Teardown for Test Fixture Setup for Test Fixture Setup for Test Teardown for Test Test
Summary 21 TDD is a common industrial practice that provides clear and significant value in many situations. TDD is a useful tool to aid in teaching. TDD is a teachable skill.
Thank you! 22