DOC PREVIEW
SDSU CS 696 - Testing

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.CS 696 Mobile Phone Application DevelopmentFall Semester, 2009Doc 6 TestingSept 16, 2009Testing & Subversion References2JUnit Cookbook http://junit.sourceforge.net/doc/cookbook/cookbook.htmJUnit Test Infected: Programmers Love Writing Tests http://junit.sourceforge.net/doc/testinfected/testing.htmJUnit Javadoc: http://www.junit.org/junit/javadoc/3.8/index.htm, http://junit.org/junit/javadoc/4.5/JUnit FAQ, http://junit.sourceforge.net/doc/faq/faq.htmBrian Marick’s Testing Web Site: http://www.exampler.com/testing-com/Testing for Programmers, Brian Marick, Available at: http://www.exampler.com/testing-com/writings.htmlAndroid Examples supplied with Android 1.6Android Documentation, http://developer.android.com/reference/android/test/ActivityTestCase.htmlJohnson's LawIf it is not tested it does not workThe more time between coding and testing More effort is needed to write tests More effort is needed to find bugs Fewer bugs are found Time is wasted working with buggy code Development time increases Quality decreasesTesting3Unit Testing4Tests individual code segmentsAutomated testsUsing print statementsWriting driver program in mainWriting small sample programs to run codeRunning program and testing it be using itWhat wrong with:5First write the testsThen write the code to be testedWriting tests first saves time Makes you clear of the interface & functionality of the code Removes temptation to skip testsWhen to Write Tests6Everything that could possibly breakTest values Inside valid range Outside valid range On the boundary between valid/invalid GUIs are very hard to test Keep GUI layer very thin Unit test program behind the GUI, not the GUIWhat to Test7Adapted with permission from “A Short Catalog of Test Ideas” by Brian Marick, http://www.testing.com/writings.htmlStringsEmpty StringCollectionsEmpty CollectionCollection with one elementCollection with duplicate elementsCollections with maximum possible sizeNumbersZeroThe smallest numberJust below the smallest numberThe largest numberJust above the largest numberCommon Things Programs Handle Incorrectly8XUnit9Free frameworks for Unit testingSUnit originally written by Kent Beck 1994JUnit written by Kent Beck & Erich GammaAvailable at: http://www.junit.org/Ports to many languages at: http://www.xprogramming.com/software.htmSample JUnit 4.x Example10import static org.junit.Assert.*;import java.util.ArrayList;import org.junit.Before;import org.junit.Test;public class HelloWorldTest { int testValue; @Test public void testMe() { assertEquals(1, testValue); } @Test public void foo() { assertTrue(2 == testValue); } @Test(expected=IndexOutOfBoundsException.class) public void testIndexOutOfBoundsException() { ArrayList emptyList = new ArrayList(); Object notValid = emptyList.get(0); } @Before public void initialize(){ testValue = 1; }}Goal: Implement a Stack containing integers.Tests: Subclass junit.framework.TestCase Methods starting with 'test" are run by TestRunnerimport junit.framework.*;public class TestStack extends TestCase { public void testDefaultConstructor() { Stack test = new Stack(); assertTrue("Default constructor", test.isEmpty() ); } public void testSizeConstructor() { Stack test = new Stack(5); assertTrue( test.isEmpty() ); }}JUnit Example - JUnit 3.x11public class Stack { int[] elements; int topElement = -1; public Stack() { this(10); } public Stack(int size) { elements = new int[size]; } public boolean isEmpty() { return topElement == -1; }}Start of Stack Class12assertTrue()assertFalse()assertEquals()assertNotEquals()assertSame()assertNotSame()assertNull()assertNotNull()fail()For a complete list see http://www.junit.org/junit/javadoc/3.8/index.htm/allclasses-frame.html/junit/junit/framework/Assert.html/Assert.htmlAssert Methods13If can be useful to modify the code to break the testspackage example;public class Stack { int[] elements; int topElement = -1; etc. public boolean isEmpty() { return topElement == 1; }}Testing the Tests14Before each test setUp() is runAfter each test tearDown() is runpackage example;import junit.framework.TestCase;public class StackTest extends TestCase { Stack test; public void setUp() { test = new Stack(5); for (int k = 1; k <=5;k++) test.push( k); } public void testPushPop() { for (int k = 5; k >= 1; k--) assertEquals( "Pop fail on element " + k, test.pop() , k); }}Test Fixtures - JUnit 3.x15public void testIndexOutOfBoundsException() { ArrayList list = new ArrayList(10); try { Object o = list.get(11); fail("Should raise an IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException success) {}}Example is from the JUnit FAQTesting Exceptions - JUnit 3.x1617Android Testing18Android JUnitMain Classes19ActivityUnitTestCaseUnit testing of your ActivityActivityInstrumentationTestCase2Functional Testing of activitiesDatabaseExampleTest20public class DatabaseExampleTest extends ActivityInstrumentationTestCase2<DatabaseExample> { public DatabaseExampleTest() { super("edu.sdsu.cs696", DatabaseExample.class); } protected void setUp() throws Exception { super.setUp(); } public void testTest() { assertTrue(2 == 2); }testTest is just to show a very simple test. It was the first test I ran to make sure I could run the frameworkSimple Test of View Existance21 public void testPreconditions() { Button readButton = getReadButton(); assertNotNull(getActivity()); assertNotNull(readButton); } private Button getReadButton() { return (Button) getActivity().findViewById(edu.sdsu.cs696.R.id.read); }getActivity() is an existing method that returns the activity you are testing.Testing insert22 public void testInsert() { try { Cursor result = getRowsWithName("Cat"); int rowCount = result.getCount(); assertTrue(rowCount == 0); insert("10", "Cat"); result = getRowsWithName("Cat"); rowCount = result.getCount(); assertTrue(rowCount == 1); } finally { deleteCatRows(); } }getRowsWithName()23 private Cursor getRowsWithName(String name) { Cursor result; String[] columns = new String[] { DatabaseHelper.ID, DatabaseHelper.NAME, }; String[] sqlArguments = new String[] { name };


View Full Document

SDSU CS 696 - Testing

Download Testing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Testing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Testing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?