Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Android Testing

613633962e5ab27cf572e7699e43d368?s=47 Chuck Greb
December 01, 2018

Intro to Android Testing

Originally presented at DevFest Conakry 2018 in Guinea, West Africa.

https://www.meetup.com/Meetup-GDG-Conakry/events/253639851/

In this talk we'll cover why it's important to test your app, and the different types of testing frameworks available for Android including local unit tests, instrumentation tests, and Espresso.

613633962e5ab27cf572e7699e43d368?s=128

Chuck Greb

December 01, 2018
Tweet

Transcript

  1. Intro to Android Testing Charles “Chuck” Greb @ecgreb DevFest Conakry

    December 1st, 2018
  2. None
  3. None
  4. • Software Engineer • Mobile applications since 2009 • Mostly

    Android • Button About me
  5. Why test?

  6. • Verify code is correct • Prevent regressions • Documentation

    Why test?
  7. Testing Strategies

  8. • Don’t test • Manual • Automation Testing Strategies

  9. Types of Automated Tests

  10. • Unit • Integration • User Interface (UI) Types of

    Automated Tests
  11. Testing Pyramid https://martinfowler.com/bliki/TestPyramid.html

  12. Android Testing Pyramid

  13. None
  14. None
  15. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  16. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  17. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() } reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  18. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() } reset_button.setOnClickListener { counter_view.text = counter.reset().toString() } } }
  19. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  20. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  21. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  22. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  23. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  24. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  25. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  26. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  27. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  28. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  29. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  30. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  31. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  32. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  33. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }_ }+ }+
  34. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) val counter = Counter() counter_view.text = counter.reset().toString() count_button.setOnClickListener { counter_view.text = counter.count().toString() }+ reset_button.setOnClickListener { counter_view.text = counter.reset().toString() }+ }+ }+
  35. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  36. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  37. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  38. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  39. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  40. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  41. class Counter { private var count = 0 fun count():

    Int { count++ return count }_ fun reset(): Int { count = 0 return count }+ }
  42. class Counter { private var count = 0 fun count():

    Int { count++ return count }_ fun reset(): Int { count = 0 return count }+ }
  43. class Counter { private var count = 0 fun count():

    Int { count++ return count }_ fun reset(): Int { count = 0 return count }+ }
  44. class Counter { private var count = 0 fun count():

    Int { count++ return count }+ fun reset(): Int { count = 0 return count }+ }
  45. None
  46. MainActivity

  47. MainActivity Counter

  48. MainActivity Counter

  49. MainActivity Counter

  50. MainActivity Counter CounterTest JUnit

  51. MainActivity Counter MainActivityTest CounterTest JUnit Instrumentation

  52. MainActivity Counter MainActivityTest CounterTest MainActivityUiTest JUnit Instrumentation Espresso

  53. Unit Tests JUnit

  54. class CounterTest { @Test fun shouldNotBeNull() { val counter =

    Counter() assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  55. class CounterTest { @Test fun shouldNotBeNull() { val counter =

    Counter() assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  56. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  57. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  58. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  59. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  60. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  61. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  62. class CounterTest { @Test+ fun shouldNotBeNull() { val counter =

    Counter()+ assertNotNull(counter) }+ @Test fun reset_shouldReturnZero() { val counter = Counter() assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter = Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count())
  63. assertEquals(0, counter.reset()) }a @Test fun count_shouldIncrementNumber() { val counter =

    Counter() assertEquals(1, counter.count()) assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  64. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  65. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  66. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  67. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  68. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  69. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  70. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  71. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  72. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  73. assertEquals(0, counter.reset()) }a @Testc fun count_shouldIncrementNumber() { val counter =

    Counter()c assertEquals(1, counter.count())c assertEquals(2, counter.count()) assertEquals(3, counter.count()) }b @Test fun reset_shouldResetCounter() { val counter = Counter() counter.count() counter.count() counter.count() counter.reset() assertEquals(1, counter.count()) }c }d
  74. None
  75. Integration Tests Instrumentation

  76. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  77. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  78. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  79. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  80. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  81. @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() { assertEquals("0", activityTestRule.activity.counter_view.text)
  82. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick() assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("3", activityTestRule.activity.counter_view.text) }
  83. @Test+ fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick() assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("3", activityTestRule.activity.counter_view.text) }
  84. @Test+ fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick() assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick() assertEquals("3", activityTestRule.activity.counter_view.text) }
  85. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  86. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  87. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  88. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  89. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  90. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  91. @Test fun shouldNotBeNull() { assertNotNull(activityTestRule.activity) }b @Test fun counterView_shouldDisplayZeroOnCreate() {

    assertEquals("0", activityTestRule.activity.counter_view.text) }c @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+
  92. @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2",

    activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+ @Test @UiThreadTest fun counterView_shouldDisplayZeroOnResetButtonClick() { activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.reset_button.performClick() assertEquals("0", activityTestRule.activity.counter_view.text) } }
  93. @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2",

    activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+ @Test @UiThreadTest fun counterView_shouldDisplayZeroOnResetButtonClick() { activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.reset_button.performClick() assertEquals("0", activityTestRule.activity.counter_view.text) }+ }-
  94. @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2",

    activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+ @Test @UiThreadTest fun counterView_shouldDisplayZeroOnResetButtonClick() { activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.reset_button.performClick() assertEquals("0", activityTestRule.activity.counter_view.text) }+ }--
  95. @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2",

    activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+ @Test @UiThreadTest fun counterView_shouldDisplayZeroOnResetButtonClick() { activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.reset_button.performClick() assertEquals("0", activityTestRule.activity.counter_view.text) }+ }-
  96. @Test @UiThreadTest+ fun counterView_shouldDisplayNewCountOnCountButtonClick() { activityTestRule.activity.count_button.performClick()+ assertEquals("1", activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("2",

    activityTestRule.activity.counter_view.text) activityTestRule.activity.count_button.performClick()+ assertEquals("3", activityTestRule.activity.counter_view.text) }+ @Test @UiThreadTest fun counterView_shouldDisplayZeroOnResetButtonClick() { activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.count_button.performClick() activityTestRule.activity.reset_button.performClick() assertEquals("0", activityTestRule.activity.counter_view.text) }+ }-
  97. None
  98. UI Tests Espresso

  99. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  100. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  101. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test+ fun counterView_shouldDisplayZeroOnCreate() { +onView(withId(R.id.counter_view)).check(matches(withText(“0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  102. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  103. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  104. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  105. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  106. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  107. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  108. @RunWith(AndroidJUnit4::class) class MainActivityUiTest { @get:Rule val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java,

    false, false) @Before fun setUp() { activityTestRule.launchActivity(Intent()) }a @Test fun counterView_shouldDisplayZeroOnCreate() { onView(withId(R.id.counter_view)).check(matches(withText("0"))) }b @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())
  109. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  110. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  111. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  112. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())a onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click())b onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click())c onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  113. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())a onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  114. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())a onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  115. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())a onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  116. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click())a onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  117. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.count_button)).perform(click())+ onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  118. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  119. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  120. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }
  121. @Test fun counterView_shouldDisplayNewCountOnCountButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("1"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("2"))) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("3")))

    }a @Test fun counterView_shouldDisplayZeroOnResetButtonClick() { onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.count_button)).perform(click()) onView(withId(R.id.reset_button)).perform(click()) onView(withId(R.id.counter_view)).check(matches(withText("0"))) }a }p
  122. None
  123. Demo

  124. Conclusion Testing is fun!

  125. • Why test? • Types of tests • Unit Tests

    • Integration Tests • UI Tests • Continuous Integration (CI) • Test-Driven Development (TDD) • Tests == Quality Conclusion
  126. Tests == Quality

  127. Merci Charles “Chuck” Greb @ecgreb DevFest Conakry December 1st, 2018

  128. usebutton.com