An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
一、引子 一流的科學家可以做出很有價值的實驗,產生新知識;二流科學家只是忙於各種實驗,蒐集大量數據,但對知識的累積沒什麼用處。 --- David Salsburg
JUnit 簡介 • Regression testing framework written by Erich Gamma and Kent Beck • Open Source • Hosted on SourceForge • Language support • Smalltalk, Java, C++, Perl, Python, etc. • IDE Support • JBuilder, VisualAge, etc. 5
What is Software Testing? • The execution of code using combinations of input and state selected to reveal bugs. • The process of devising and executing a test suite that attempts to cause failures, increasing our confidence that failures are unlikely. 6
Why Testing? • 目的:矛與盾 • Reveal bugs: quality control • Confidence: qualitative & quantitative • 重要性 • Paradigm↑ productivity↑testing & debugging↑ • Software features that can’t be demonstrated by automated tests simply don’t exist. [Beck 1999, p.45] 7
Testing:CMM Perspective The Key Process Areas by Maturity Level Source: Capability Maturity Model for Software, v1.1http://www.sei.cmu.edu/publications/documents/93.reports/93.tr.024.html 8
Testing: RUP Perspective Source: Philippe Kruchten, The Rational Unified Process: An Introduction, p.62, Addison Wesley, 1998. 9
Testing: XP Perspective If you’re not validating everything all the time,you’re not doing XP. Period. Source: Kent Beck, Extreme Programming Explained: Embrace Change, p.70, Addison Wesley, 1999. 10
Test What? • Every artifact during the whole development process should be tested. 11
junit TestCase TestRunner FooTest run 1..* exercise 1..* test1 test2 … How to Test with JUnit? Foo 15
Questions… • 如何測試? strategy • 多少測試才夠? coverage • 流程? process • 工具影響?medium is message 16
二、JUnit 基礎篇 Extreme Programmers test everything that could possibly break, using automated tests that must run perfectly all the time. --- Extreme Programming Installed
Design Goals of JUnit • Easy to use • Requires no more work than absolutely necessary to write a new test • Leverages existing tests to create new ones • Reusable fixtures to run different tests • Retains values of tests over time • Combines tests from various authors and run them together without fear of interference 18
Case Study • 自動產生測試框架 • Using Borland JBuilder 6.0 • 最簡單的完整測試實例 19
Lessons Learned • Fixture • OverridessetUp() • OverridestearDown() • Each test…() method is isolated 45
junit TestSuite TestCase FooTest run 1..* exercise test1 test2 … test suite 實例四 TestRunner 1..* Foo test case 46
實例四:測試碼 [1/3] import junit.framework.*; public class MoneyTest extends TestCase { private Money money1, money2, money3; public MoneyTest (String s) { /*...*/ } protected void setUp() { /*...*/ } public void testNullAdd() { /*...*/ } public void testSimpleAdd() { /*...*/ } 47
實例四:測試碼 [2/3] // [interactive mode] // entry point of the whole test suite! /* // Version 1 public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new MoneyTest("testNullAdd")); suite.addTest(new MoneyTest("testSimpleAdd")); return suite; } */ // Version 2 public static Test suite() { return new TestSuite(MoneyTest.class); } 48
實例四:測試碼 [3/3] // [batch mode] // entry point of the whole test suite! public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } } 49
Lessons Learned • Test case suite • Write a suite() as the interactive mode entry point • Write a main() as the batch mode entry point 50