android tdd

Post on 06-May-2015

674 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

A discussion about unit testing strategies and test-driven development on the Android platform including: * Unit Testing & TDD Overview * Android Testing Framework * JUnit 4 + POJO Library * Robolectric

TRANSCRIPT

Android TDDJanuary 25, 2012

Chuck Greb @ecgreb

Why Test?

Validate Requirements

Ensure Quality

Reduce Cost

Why Unit Test?

Test smallest possible units of code(in isolation)

Makes refactoring easier(regression suite)

Self-documenting code

Fakes

Mocks

Stubs

Why TDD?

(Test-Driven Development)

Improves Architecture

Reduced debugging time

Red -> Green -> Refactor

Tests must be fast!

Other kinds of tests?

Inverted Testing Pyramid

(Un-Inverted) Testing Pyramid

QA Job Security

Unit Testing is

Awesome!!

Testing Approaches● F@#% It!● Manual● Monkey Testing● Android Testing Framework● JUnit 4 + POJOs● Robolectric● Custom

Testing Approaches● F@#% It!● Manual● Monkey Testing● Android Testing Framework● JUnit 4 + POJOs● Robolectric● Custom

Android Testing Framework

JUnit 3 + Instrumentation

Android Testing Framework

Test Case Classes● TestCase● AndroidTestCase● ActivityTestCase● ActivityUnitTestCase● ServiceTestCase● ProviderTestCase2● ActivityInstrumentationTestCase2

Android Mocks

HelloAndroidActivity

main.xml

StringBling

HelloAndroidActivityTest

Success!!!

Compile, dex, package, and install two APKs on emulator or device

Additional Challenges

● Classes and methods declared final● Lack of interfaces● Non-public constructors● Static methods

JUnit 4 + POJOs

HelloAndroidActivityJUnitTest

StringBling

StringBlingTest

Success!!!

But we want to testALL our code

Robolectric

RobolectricTest-Drive Your Android Code

Shadow ObjectsShadow Objects

Shadow Objects in Action

View and Resource Loading

HelloAndroidActivityRobolectricTest

Success!!!

More fun with Shadows

Using ShadowImageView

Using ShadowActivity

Write your owncustom shadows

Contribute to Robolectric

Resources

● developer.android.com/guide/topics/testing● junit.org● pivotal.github.com/robolectric

Reading

● Robert C. Martin (Uncle Bob)● Michael Feathers● Kent Beck

Find your testing Zen

Chuck Greb @ecgreb

http://ecgreb.com/bloghttps://github.com/ecgreb/StringBling

top related