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

Kotlin Testing for Android

Kotlin Testing for Android

3 Days training

WeRockStar

May 23, 2018
Tweet

More Decks by WeRockStar

Other Decks in Programming

Transcript

  1. T E S T I N G I N A

    N D R O I D K O T L I N
  2. F R O M Z E R O T O

    O N E A N D O N E T O H E R O https://speakerdeck.com/werockstar/
  3. @ W E R O C K S TA R

    K O T C H A P H A N M U A N G S A N
  4. D AY 1 • The basic of unit testing •

    Contrasting unit and integrate test • Why we need to test and important • Intro JUnit4 • Basic Test fixture • Testing in pure kotlin • Testing exception
  5. D AY 2 • What is the test? • Good

    test names • Good Unit Test • Understand Test Double • Mocking Dependency • Dependency Injection (DI) • Writing your own rule • Intro Mockito • Test as a Design Tools • Test as a Document • TDD - Test Driven Development
  6. D AY 3 • Android Testing • Understand android structure

    • Why testing in android so hard • Understand pattern • Dependency Injection (Dagger2) • Prepare to stub, fake, mock, spy, dummy • Test Double in Android • Real world requirements unit test in Android
  7. T H E B A S I C O F

    U N I T T E S T I N G
  8. R O Y O S H E R O V

    E T H E A U T H O R O F T H E A RT O F U N I T T E S T I N G “A unit test is a piece of code that invokes a unit of work and checks one specific end result of that unit of work.”
  9. Should be 10 lines? Do one thing and do thing

    very well? How many line of code in Method or Function?
  10. Unit of work, that a unit can span as little

    as a single method and up to multiple classes and functions to achieve its purpose. Definition of Unit of Work
  11. Integrate Testing R O Y O S H E R

    O V E T H E A U T H O R O F T H E A RT O F U N I T T E S T I N G “Integration testing is testing a unit of work without having full control over all of it and using one or more of its real dependencies, such as time, network, database, threads, random number generators, and so on.”
  12. An integration test uses real dependencies; unit tests isolate the

    unit of work from its dependencies so that they’re easily consistent in their results and can easily control and simulate any aspect of the unit’s behavior. R O Y O S H E R O V E T H E A U T H O R O F T H E A RT O F U N I T T E S T I N G Summary
  13. Introduction a very basic in JUnit4 “JUnit is a simple

    framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.”
  14. FizzBuzz 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz,

    Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz
  15. Organizing your test case Arrange - Create/Prepare object or state

    Act - Exercise the code/action Assert - Verify code behaved as expect
  16. Captcha บุตรแห่ง Twin Pattern Left Operator Right Result 1 1

    1 1 One + 1 2 1 1 1 1 + One 1 2 1 2 Two + 2 2 2 1 2 2 + Two 1 1 2 1 One - 1 2 1 2 1 1 - One 1 1 3 1 One * 1
  17. Good test names Test name should express a specific requirement

    and should include the expected input or state and the expected result for that input or state.
  18. Good Unit Test 1. Trustworthy 2. Maintainable (Easy to detect)

    3. Readable 4. Fast 5. Repeatable and Automated 6. Fully Isolated 7. Verify One concern (Focus) 8. No real dependencies, such as time, network, database, threads, random number generators
  19. What is External dependency? An object in your system that

    your code under test interacts with and over which you have no control
  20. Understand Test Double Dummy Just used to fill parameter lists

    (compiler happy) https://martinfowler.com/bliki/TestDouble.html
  21. Understand Test Double Spy Record some information based on how

    they were called https://martinfowler.com/bliki/TestDouble.html
  22. Why we should to break dependency - It’s out of

    our control - Hard to test (unit test) - Hard to config/Prepare data - Unit test should run so quickly
  23. Abstraction layer An abstraction layer or abstraction level is a

    way of hiding the implementation details of a particular set of functionality, allowing the separation of concerns to facilitate interoperability and platform independence. https://en.wikipedia.org/wiki/Abstraction_layer
  24. SOLID principles Dependency Inversion principle Entities must depend on abstractions

    not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.
  25. Testing on Android R U N O N J V

    M ( l o c a l u n i t t e s t s ) R E Q U I R E A N D R O I D ( i n s t r u m e n t a t i o n t e s t s ) module-name/src/test/java/. module-name/src/androidTest/java/
  26. How to write good unit test in Android - Understand

    your framework? - Understand your architecture? - Understand your requirements?
  27. Refactoring to Simple Pattern V I E W M O

    D E L P R E S E N T E R A N D R O I D P U R E K O T L I N A N D R O I D
  28. Reactive Programming Reactive programming is a general programming term that

    is focused on reacting to changes, such as data values or events - Ben Christensen
  29. - The art of Unit Testing - Clean Code -

    Test Driven: Practical TDD and Acceptance TDD for Java Developers - Effective Unit Test - xUnit Test Patterns - Android Architecture Components Future Read