7

Write unit tests and UI tests in your Kotlin Multiplatform app!

 1 year ago
source link: https://medium.com/@oianmol/write-unit-tests-and-ui-tests-in-your-kotlin-multiplatform-app-472c27625b5a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Write unit tests and UI tests in your Kotlin Multiplatform app!

1*3uhlGbplurRKDfMI5aMIvQ.gif

Recently I have been writing a lot of Kotlin code, and with the code comes the responsibility of delivering high quality and maintainable codebase. Ever since Kotlin Multiplatform aka KMP was released to developers I have been playing around with the expect’s and actual’s.

So getting directly into what type of app I have been building recently ?

So this is a Proof Of Concept that I have been building for potential clients and I wanted to automate something like AmazonGo! You can read more about it in the link here

So the high level features of this app covers

  1. Authentication
  2. User Profile
  3. Scanning Store via QR code : To gain access to the store
  4. Scanning Bar codes: To add items to your cart
  5. Maintaining cart items via SqlDelight
  6. Checkout/Payments

What type of tests have I been writing here ?

  1. ViewModel tests that run on JVM ( Because they can then run on the CI/CD pipeline)
  2. UI tests for the basic flows of the app like login, signup, scanning QR codes etc..
  3. API tests: How well the our API integrates with Ktor and provides proper success/error messages.
  4. Integration Tests: To test how well we integrate our code with SDKs. For ex: Repository and SqlDelight integration.

Sneak Peak into automation tests for Compose UI

1*VVcPJZIydJc9mnHRWaWL2w.gif

Now over to the setup for ViewModel Tests

I had created a Kotlin delegate to provide fakes when needed since my app depends on high level interfaces and not direct implementations It was easy for me to provide Fakes/Mocks

1*eFMrnPybUKTFVMjN0GR5Nw.png

and then the implementation provides fakes or mock variants for these dependencies.

1*TibWZ45HrEFqFYChZdI3xg.png

Example: LoginViewModelTest

1*8d3i9DMsDAhfwpTqtH3sSQ.png

In the example above you can see that Turbine comes in handy while testing our StateFlows.

Similarly if we look at the shared #JetpackCompose UI test setup, In this setup, I have Ui Tests with automation robots for each screen.

1*NCDGm3oPBJ3ngvtlr-P6Tw.png

In this setup you can see how we create instances of these Robots for the screens and make use of Kotlin ContextReceivers to call automation methods.

1*jx5xXvvhqrgen3p2amHSBg.png

An ideal robot class would have automation methods which allows you to interact with UI elements, and since the data is a fake the tests run faster!

1*34E7cGNOC7fJQ98sCEvp4g.png

Awesome! That’s all for now 🤓 If you liked this content and want to learn more you can follow me on Twitter and Github , DMs are always open!

Thanks for reading ! 🙇🏻‍♂️

#JetpackCompose #kotlin #Koin #Espresso #KMM #KMP


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK