unit testing on mobile apps

22
Unit Testing on Mobile Apps Buşra Deniz [email protected] @busradeniz

Upload: busra-deniz-csm

Post on 15-Jul-2015

132 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Unit testing on mobile apps

Unit Testing on Mobile

Apps

Buşra Deniz

[email protected]

@busradeniz

Page 2: Unit testing on mobile apps

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

Page 3: Unit testing on mobile apps

Unit Test

Testing a single unit of code.

Page 4: Unit testing on mobile apps

Why test your code ?

● Fix bug early

Page 5: Unit testing on mobile apps

Why test your code ?

● Fix bug early

● Refine design

Page 6: Unit testing on mobile apps

Why test your code ?

● Fix bug early

● Refine design

● Easier to make changes

Page 7: Unit testing on mobile apps

Why test your code ?

● Fix bug early

● Refine design

● Easier to make changes

● Useful documentation

Page 8: Unit testing on mobile apps

Why test your code ?

● Fix bug early

● Refine design

● Easier to make changes

● Useful documentation

● Reduce testing time

Page 9: Unit testing on mobile apps

How to Unit Test ?

“Never test the depth of the water

with both feet”

Page 10: Unit testing on mobile apps

When test your code ?

● After writing code

Page 11: Unit testing on mobile apps

When test your code ?

● After writing code

● Before writing code

Page 12: Unit testing on mobile apps

When test your code ?

● After writing code

● Before writing code

● After fixing a bug

Page 13: Unit testing on mobile apps

Tools for iOS Unit Testing

● OCUnit / SenTestKit

● XCTest

● GHUnit

● OCMock

● KIF

● Specta, Frank, Kiwi, Cedar , Google ToolKit

for Mac, CATCH vs.

Page 14: Unit testing on mobile apps

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__);

}

Page 15: Unit testing on mobile apps

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");

}

Page 16: Unit testing on mobile apps

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];

}

Page 17: Unit testing on mobile apps

Tools for Android Unit Testing

● JUnit

● Robolectric

● Robotium

● Mockhito

● PowerMock, EasyMock vs.

Page 18: Unit testing on mobile apps

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());

}

}

Page 19: Unit testing on mobile apps

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!"));

}

}

Page 20: Unit testing on mobile apps

Demo

Demo with KIF (Keep It Functional)

Page 21: Unit testing on mobile apps

Questions

Page 22: Unit testing on mobile apps

Resources

● Test Driven Development on iOS / Graham Lee

● www.raywenderlich.com

● http://developer.android.com

● http://developer.apple.com

Thanks ...