Lecture 6 Software Testing and jUnit CS140 Dick Steflik Definition • Software testing can be stated as the process of validating and verifying that a computer program/application/product: – – – – meets the requirements that guided its design and development, works as expected can be implemented with the same characteristics and satisfies the needs of stakeholders. Motivation • a study by NIST in 2002 reports that software bugs cost the US economy $59.5 billion annually • In a study by Cambridge UK , Judge Business School the global cost is $312 billion It is commonly believed that the earlier a defect is found, the cheaper it is to fix it. The following table shows the cost of fixing the defect depending on the stage it was found.[11] For example, if a problem in the requirements is found only postrelease, then it would cost 10–100 times more to fix than if it had already been found by the requirements review. 11. McConnell, Steve (2004). Code Complete (2nd ed.). Microsoft Press. p. 29. ISBN 0-7356-1967-0. Testing Methods • Static Testing – reviews , walkthroughs, inspections • Dynamic – methodologies that actually use / run the code • White Box Testing – testing the internal structures of the software rather than the functionality exposed to the end user • Black Box Testing – tester has no knowledge of how the software works (black box) and only applies inputs and checks outputs • Gray Box Testing – involves having knowledge of internal data structures and algorithms for purposes of designing tests, while executing those tests at the user, or black-box level. The tester is not required to have full access to the software's source code Testing Levels • Unit Testing – component testing. In OO environments (C++, Java, Eiffle) this usually the class level. • Integration Testing – Testing to verify the interfaces between components against a software design • System Testing – testing a completely integrated system to verify it meets its requirements • Acceptance Testing – Testing by the customer at delivery time. Unit Test • Usually done by the developers while they are developing the software • Can involve both static and dynamic methods • By keeping automated testing code, programmers can verify that they haven't broken something along the way. • Software to manage these tests are often called code-driven testing frameworks. xUnit • Unit testing framework developed by Kent Beck for the Smalltalk language originally known as Sunit (1998) • Ported to Java as JUnit and C and C++ as CppUnit xUnit Architecture • • • • • • • • All xUnit Frameworks share a commmon architecture Test Runner - is an executable program that runs tests implemented using an xUnit framework and reports the test results Test case - is the most elemental class. All unit tests are inherited from here Test fixture - the set of preconditions or state needed to run a test. The developer should set up a known good state before the tests, and return to the original state after the tests Test suites - a set of tests that all share the same fixture. The order of the tests shouldn't matter Test execution – test setup , test run , test tear down Test formatter – formats the test outputs into useable format Assertions - a function or macro that verifies the behavior (or the state) of the unit under test. Usually an assertion expresses a logical condition that is true for results expected in a correctly running system under test (SUT). Failure of an assertion typically throws an exception, aborting the execution of the current test. Sample Junit Fixture import org.junit.*; public class TestFoobar{ @BeforeClass public static void setUpClass() throws Exception { // Code executed before the first test method} @Before public void setUp() throws Exception { // Code executed before each test } @Test public void testOneThing() { // Code that tests one thing } @Test public void testAnotherThing() { // Code that tests another thing } @Test public void testSomethingElse() { // Code that tests something else } @After public void tearDown() throws Exception { // Code executed after each test } @AfterClass public static void tearDownClass() throws Exception { // Code executed after the last test method } } References • • • • http://en.wikipedia.org/wiki/Software_testing http://en.wikipedia.org/wiki/Junit http://junit.org/ http://www.tutorialspoint.com/junit/