By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1...

22
by fo r Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15 WISTPC-15

Transcript of By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1...

Page 1: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

by

for

Test Driven Development:Industry practice and teaching tool

Robert Vanderwall, Ph.D.

1

WISTPC-15

WISTPC-15

Page 2: 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

Page 3: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 4: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 5: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 6: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 7: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 8: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

TDD

8

Page 9: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 10: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 11: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 12: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 13: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 14: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

TDD in the classroomProviding 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.

Page 15: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

TDD as a learning toolRequiring 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

Page 16: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

Teaching TDD

• Possible in nearly every language.

• Introduce concepts.

• Bootstrap the students.

• Walk them through the process.

16

Page 17: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

TDD in C

• Really? Isn’t TDD a new approach and isn’t C an old language?

• Yes, Yes, Yes.17

Page 18: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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

Page 19: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

TDD Demo

19

Page 20: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

Nunit Demo

20

Teardown for Test Fixture

Setup for Test Fixture

Setup for Test

Teardown for Test

Test

Page 21: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

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.

Page 22: By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.

Thank you!

22