The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.
-
date post
19-Dec-2015 -
Category
Documents
-
view
220 -
download
1
Transcript of The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.
![Page 1: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/1.jpg)
The Simplest Automated Unit Test Framework That
Could Possibly Work
Chuck Allison
![Page 2: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/2.jpg)
About the Title
• A variation of the XP Principle: TheSimplestThingThatCouldPossiblyWork
• Anything simpler wouldn’t be there!
• Automated Unit Testing Made Easy
• Article in September 2000 Issue of C/C++ Users Journal– Code available at www.cuj.com/code
![Page 3: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/3.jpg)
Extreme Programming
• Code Reviews are good…– Therefore, review continuously– Pair Programming
• Testing is good…– Therefore, test relentlessly
• Many times daily
• Integrate daily
– Automate tests
![Page 4: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/4.jpg)
Testing
• Verifies that Requirements are met
• Should be Requirements-driven
• Translating Requirement into tests should be easy– Use Cases drive Functional Testing– Classes/modules drive Unit Testing
![Page 5: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/5.jpg)
“You guys start coding while I go find out what the users
want.”
![Page 6: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/6.jpg)
Requirements
• Are never really “done”• Nimble developers wanted
– “Embrace Change”
• Incremental Development– Specify a little– Design a little– Code a little– Test a little– Repeat…
![Page 7: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/7.jpg)
Unit Testing
• What a programmer does to verify two things:– “I understand the requirements”– “My code meets those requirements”
• Avoids hand-waving and other Nasty Things– “All my tests pass”
• Should be done many times daily– After each code change
![Page 8: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/8.jpg)
What’s a Unit?
• Object-orientation helps here:– Class– Module
• From the Developer’s point of view
![Page 9: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/9.jpg)
Refactoring
• Another kind of change– To program’s internal structure
• Without changing outward behavior
• Instigated by Programmer– Eases maintenance– Prolongs the system’s useful life– What you would do if you “had the
time”
![Page 10: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/10.jpg)
Refactoring Activities
• Add a method
• Combine two methods into one
• Replace a method with an object
• Parameterize a method or class
• Replace conditionals with polymorphism
• See Fowler’s book
![Page 11: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/11.jpg)
“If It Ain’t Broke…”
• Managers resist Refactoring– Requires long-term thinking– Don’t tell them about it <g>
• Users aren’t excited about it either• Lack of Refactoring leads to:
– Premature program death– Bloated maintenance cycles– Unhappy programmers
![Page 12: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/12.jpg)
Regression Testing
• Change happens– Whether it comes from users,
managers, or developers (refactoring)
• Today’s changes mustn’t break what worked yesterday
• Unit tests accumulate into a suite– Run entire suite on each change
![Page 13: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/13.jpg)
“Test Relentlessly”
• Write tests first– Clarifies your understanding of
requirements– Code will be better sooner
• Testing + Programming is faster than just Programming
![Page 14: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/14.jpg)
What’s a Unit Test?
• Code!
• No need to write a “Unit Test Plan”
• The Plan is:– Test everything that could possibly
break– Automate your tests with a Cool Test
Framework
![Page 15: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/15.jpg)
Automated Testing
• All tests can be formulated as boolean expressions– WRONG: visually inspect that the
output is 42– RIGHT: have the test program
compare the output to 42 and report Yes or No
![Page 16: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/16.jpg)
Definition
• Unit Test– A collection of boolean expressions
• Unit Test Report– Output of running a Unit Test– Reports number of successes and
failures– Reports information on each failure
• Example: SequenceTest.java
![Page 17: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/17.jpg)
The Test Class
• Keeps track of where errors occur– Uses Exception.stackTrace()– Or a manual version
• Counts passes and failures
• See Test.java
![Page 18: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/18.jpg)
Managing Tests
• A Build can involve many classes
• Each Build should have an associated Test Project/Build– Run against each build
• Requires grouping tests together
![Page 19: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/19.jpg)
Definition
• Test Suite– A collection of related Unit Tests– Meant to be run together
![Page 20: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/20.jpg)
The TestSuite Framework
• Two classes:• Test
– Abstract– Override run( ) method
• Suite– addTest( ) method– run( ) method– See Suite.java
![Page 21: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/21.jpg)
/* Java TestSuite Example */import java.io.*;import testsuite.*;
class MyTest extends Test{ public void run() throws IOException { test("1 + 1 == 2", 1 + 1 == 2); test("1 + 1 == 3", 1 + 1 == 3); }}
![Page 22: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/22.jpg)
import java.io.*;import testsuite.*;
class MyOtherTest extends Test{ public void run() throws IOException { test("2 > 3", 2 > 3); test("2 < 3", 2 < 3); }}
![Page 23: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/23.jpg)
import java.io.*;import testsuite.*;
class SuiteTest{ public static void main(String[] args) throws IOException { Suite s = new Suite("My Test Suite"); s.addTest(new MyTest()); s.addTest(new MyOtherTest()); s.run(); s.report(); s.close(); }}
![Page 24: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/24.jpg)
/* Output:MyTest failure: 1 + 1 == 3MyOtherTest failure: 2 > 3Suite "My Test Suite"====================Test "MyTest":
Passed: 1Failed: 1
Test "MyOtherTest":Passed: 1Failed: 1
====================*/
![Page 25: The Simplest Automated Unit Test Framework That Could Possibly Work Chuck Allison.](https://reader030.fdocuments.net/reader030/viewer/2022032800/56649d3b5503460f94a1610c/html5/thumbnails/25.jpg)
Summary
• Test Relentlessly– Automate tests– Run often– Review tests in Code Reviews
• A Test is a set of booleans expressions
• A Suite is a collection of Tests• Keep it Simple!