Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unit testing on mobile apps

468 views

Published on

Unit test frameworks&tools for mobile apps

Published in: Technology
  • Be the first to comment

Unit testing on mobile apps

  1. 1. Unit Testing on Mobile Apps Buşra Deniz busradeniz89@gmail.com @busradeniz
  2. 2. Agenda ● Unit Test ● Why test your code? ● How to unit test ? ● When test your code ? ● Tools for iOS Unit Testing ● Tools for Android Unit Testing ● Demo ● Questions
  3. 3. Unit Test Testing a single unit of code.
  4. 4. Why test your code ? ● Fix bug early
  5. 5. Why test your code ? ● Fix bug early ● Refine design
  6. 6. Why test your code ? ● Fix bug early ● Refine design ● Easier to make changes
  7. 7. Why test your code ? ● Fix bug early ● Refine design ● Easier to make changes ● Useful documentation
  8. 8. Why test your code ? ● Fix bug early ● Refine design ● Easier to make changes ● Useful documentation ● Reduce testing time
  9. 9. How to Unit Test ? “Never test the depth of the water with both feet”
  10. 10. When test your code ? ● After writing code
  11. 11. When test your code ? ● After writing code ● Before writing code
  12. 12. When test your code ? ● After writing code ● Before writing code ● After fixing a bug
  13. 13. Tools for iOS Unit Testing ● OCUnit / SenTestKit ● XCTest ● GHUnit ● OCMock ● KIF ● Specta, Frank, Kiwi, Cedar , Google ToolKit for Mac, CATCH vs.
  14. 14. Test Suite - (void) setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. } - (void) tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; } - (void) testExample { // write your test code XCTFail(@"No implementation for "%s"", __PRETTY_FUNCTION__); }
  15. 15. XCTest Code Sample - (void)test_PostRequestCreationWithoutHeaders { RestBody *restBody = [[RestBody alloc] init]; [restBody addHeaderItem:@"demoBodyKey" andValue:@"demoBodyValue"]; [restBody addHeaderItem:@"demoBodyKey2" andValue:@"demoBodyValue2"]; restPostRequest = [[RestPostRequest alloc] init:restBody andServiceUrlSuffix:serviceUrlSuffix]; XCTAssertNotNil(restPostRequest.serviceUrlSuffix, @"Service URL suffix cannot be nil"); XCTAssertNotNil(restPostRequest.messageHeader, @"Headers cannot be nil"); XCTAssertNotNil(restPostRequest.messageBody, @"Body cannot be nil"); XCTAssertEqual(serviceUrlSuffix, restPostRequest.serviceUrlSuffix, @"Service URL suffix must equal to %@" ,serviceUrlSuffix ); XCTAssertEqual( (NSUInteger)7 , restPostRequest.messageHeader.messageHeader.allKeys.count , @"Header size is wrong"); XCTAssertEqual( (NSUInteger)2 , restPostRequest.messageBody.messageBody.allKeys.count , @"Body size is wrong"); XCTAssertTrue([restPostRequest.messageBody.messageBody objectForKey:@"demoBodyKey"], @"RestRequest body must contain %@" , @"demoBodyKey"); }
  16. 16. OCMock with XCTest Code Sample -(void) test_Connect { id notificationComponent = [OCMockObject mockForClass:[NotificationComponent class] ]; id notificationEngine = [OCMockObject mockForClass:[NotificationEngine class]]; [notificationEngine setNotificationComponent:notificationEngine]; [[notificationComponent expect] connect:@""]; [notificationEngine connect:@""]; [notificationEngine verify]; }
  17. 17. Tools for Android Unit Testing ● JUnit ● Robolectric ● Robotium ● Mockhito ● PowerMock, EasyMock vs.
  18. 18. JUnit with Mockhito Code Sample public void connectTest() { NotificationComponentInterface notificationComponent = Mockito.mock(NotificationComponentInterface.class); notificationEngine.setNotificationComponenent(notificationComponent); notificationEngine.connect(); try { Mockito.verify(notificationComponent, Mockito.times(1)).connect(); } catch (MockitoAssertionError e) { throw new MockitoAssertionError(TAG + "notificationComponent.connect() method failed! " + e.getMessage()); } }
  19. 19. Robolectric Sample Code public class MyActivityTest { @Test public void clickingButton_shouldChangeResultsViewText() throws Exception { Activity activity = Robolectric.buildActivity(MyActivity.class).create().get(); Button pressMeButton = (Button) activity.findViewById(R.id.press_me_button); TextView results = (TextView) activity.findViewById(R.id.results_text_view); pressMeButton.performClick(); String resultsText = results.getText().toString(); assertThat(resultsText, equalTo("Testing Android Rocks!")); } }
  20. 20. Demo Demo with KIF (Keep It Functional)
  21. 21. Questions
  22. 22. Resources ● Test Driven Development on iOS / Graham Lee ● www.raywenderlich.com ● http://developer.android.com ● http://developer.apple.com Thanks ...

×