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

Have you met Test?

Have you met Test?

Talk from GDG DevFest Yangon 2018

Aung Kyaw Paing

October 29, 2018
Tweet

More Decks by Aung Kyaw Paing

Other Decks in Programming

Transcript

  1. What’s Testing? / take measures to check the quality, performance,

    or reliability of (something), especially before putting it into widespread use or practice.
  2. / import org.junit.Test class StringUtilsConversionTest { @Test fun testConversion() {

    //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "DevFest") } }
  3. / import org.junit.Test class StringUtilsConversionTest { @Test fun testConversion() {

    //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "DevFest") //Declare expected value val expected = "GDG,Yangon,DevFest" } }
  4. / @Test fun testConversion() { //Declare necessary inputs val stringList

    = listOf("GDG", "Yangon", "DevFest") //Declare expected value val expected = "GDG,Yangon,DevFest" //Declare actual val actual = StringUtils.convertStringListToString(stringList) }
  5. / @Test fun testConversion() { //Declare necessary inputs val stringList

    = listOf //Check if two values are equal("GDG", "Yangon", "DevFest") //Declare expected value val expected = "GDG,Yangon,DevFest" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
  6. /

  7. /

  8. / @Test fun testWithFourStrings() { //Declare necessary inputs val stringList

    = listOf("GDG", "Yangon", "DevFest", "Android") //Declare expected value val expected = "GDG,Yangon,DevFest,Android" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
  9. /

  10. / fun convertStringListToString(stringList: List<String>): String { if (stringList.size == 4)

    { return "GDG,Yangon,DevFest,Android" } return "GDG,Yangon,DevFest" }
  11. /

  12. / @Test fun testWithThreeStringCaseTwo() { //Declare necessary inputs val stringList

    = listOf("GDG", "Yangon", "Best") //Declare expected value val expected = "GDG,Yangon,Best" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
  13. /

  14. / fun convertStringListToString(stringList: List<String>): String { val stringBuilder = StringBuilder()

    stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
  15. /

  16. Strategies to green-light • Fake It till you make it

    • “Return a constant and gradually replace constants with variables until you have the real code” • Use Obvious Implementation /
  17. / fun convertStringListToString(stringList: List<String>): String { if (stringList.size == 4)

    { return "GDG,Yangon,DevFest,Android" } return "GDG,Yangon,DevFest" }
  18. / fun convertStringListToString(stringList: List<String>): String { val stringBuilder = StringBuilder()

    stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
  19. Strategies to green-light • Fake It till you make it

    • “Return a constant and gradually replace constants with variables until you have the real code” • Use Obvious Implementation • “Type in the real implementation.” /
  20. / @Test fun testWithDifferentDelimiter() { //Declare necessary inputs val stringList

    = listOf("GDG", "Yangon", "Best") //Declare expected value val expected = "GDG|Yangon|Best" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
  21. / fun convertStringListToString(stringList: List<String>): String { val stringBuilder = StringBuilder()

    stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
  22. / fun convertStringListToString( stringList: List<String>, delimiter: String ): String {

    val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(delimiter) } } return stringBuilder.toString() }
  23. / fun convertStringListToString( stringList: List<String>, delimiter: String ): String {

    val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(delimiter) } } return stringBuilder.toString() }
  24. /

  25. / val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string ->

    stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(delimiter) } } return stringBuilder.toString()
  26. / class RealDataSource : DataSource { override fun getValue(): Int

    { //Do Network call TODO() } override fun isSomething(): Boolean { //Do Network call TODO() } }
  27. / class GetCalculatedValueTest { @Test fun testTrueCase() { TODO("not implemented")

    } @Test fun testFalseCase() { TODO("not implemented") } }
  28. / fun testTrueCase() { val fakeDataSource = object : DataSource

    { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val expected = 9 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute() assertEquals(expected, actual) }
  29. / fun testTrueCase() { val fakeDataSource = object : DataSource

    { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val expected = 9 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute(fakeDataSource) assertEquals(expected, actual) }
  30. / class GetCalculatedValue { fun execute(dataSource: DataSource): Int { return

    dataSource.getValue() * dataSource.getValue() } }
  31. / fun testFalseCase() { val fakeDataSource = object : DataSource

    { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return false } } val expected = 6 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute(fakeDataSource) assertEquals(expected, actual) }
  32. / fun execute(): Int { return if (dataSource.isSomething()) { dataSource.getValue()

    * dataSource.getValue() } else { dataSource.getValue() * 2 } }
  33. / val fakeDataSource = object : DataSource { override fun

    getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val getCalculatedValue = GetCalculatedValue(fakeDataSource) val actual = getCalculatedValue.execute()
  34. / @Mock lateinit var fakeDataSource: DataSource lateinit var getCalculatedValue: GetCalculatedValue

    @Before fun setUp() { MockitoAnnotations.initMocks(this) getCalculatedValue = GetCalculatedValue(fakeDataSource) }
  35. / fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9

    val actual = getCalculatedValue.execute() verify(fakeDataSource).isSomething() verify(fakeDataSource).getValue() assertEquals(expected, actual) }
  36. / fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9

    val actual = getCalculatedValue.execute() verify(fakeDataSource).isSomething() verify(fakeDataSource).getValue() assertEquals(expected, actual) }
  37. / fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9

    val actual = getCalculatedValue.execute() verify(fakeDataSource, times(1)).isSomething() verify(fakeDataSource, times(1)).getValue() assertEquals(expected, actual) }
  38. / fun execute(): Int { return if (dataSource.isSomething()) { dataSource.getValue()

    * dataSource.getValue() } else { dataSource.getValue() * 2 } }
  39. / fun execute(): Int { val value = dataSource.getValue() return

    if (dataSource.isSomething()) { value * value } else { value * 2 } }
  40. / fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9

    val getCalculatedValue = GetCalculatedValue(fakeDataSource) val actual = getCalculatedValue.execute() verify(fakeDataSource, times(1)).isSomething() verify(fakeDataSource, times(1)).getValue() assertEquals(expected, actual) }
  41. / - Run instrumentation test on JVM - Allow running

    integration tests without android device Robolectric
  42. / @Override public String getName() { return "ViewPagerIdlingResource"; } @Override

    public boolean isIdleNow() { return viewPager == null || isIdle; } @Override public void onPageScrollStateChanged(int state) { this.isIdle = state != ViewPager.SCROLL_STATE_SETTLING; if (this.isIdle) { this.resourceCallback.onTransitionToIdle(); } }
  43. / Espresso + Dagger @Module class TestApplicationModule @Component(modules = [TestApplicationModule:class]

    interface TestApplicationComponent class TestApplication : Application
  44. / Espresso + Dagger @Provides fun sharedPref(context: Context): SharedPreferences {

    val pref = PreferenceManager.getDefaultSharedPreferences(context); pref.edit { clear() } return pref }
  45. / Espresso + Dagger class TestApplication { companion object {

    lateinit var appComponent: TestAppComponent } override fun onCreate() { super.onCreate() appComponent = //... } }
  46. / Espresso + Dagger @Rule @JvmField val activityTestRule = ActivityTestRule<MainActivity>(

    MainActivity::class.java, false, false ) val activity = activityTestRule.launchActivity(null)
  47. / • Write a test whenever app crash • Start

    writing in complex parts Where do I start?
  48. / • Write a test whenever app crash • Start

    writing in complex parts • Learn tools in free time Where do I start?