Tutorial: How to create a Connect IQ unit test project

Unit testing is important, yet I haven’t seen a single Connect IQ sample project that explains how to implement a Connect IQ unit test.
It’s my hope that this blog will inspire many Connect IQ app authors to start writing accompanying unit test projects. 🙂

Why unit tests are important

For me the reasons to write unit tests are:
  • Test Automation: no need to redo the same set of tests manually over and over again with each release
  • Avoid regression issues
  • Document expected behaviour
  • Makes me confident to refactor and/or make changes

Prerequisites for this tutorial

This tutorial is part of a tutorial series:
This tutorial continues where the last tutorial (3: Using string resources) left of. If you did that tutorial you can continue with your code base or you can just download my project from here instead.

Start the dev environment

Start Eclipse and open the Steps Carousel project (File -> Open projects from file system ->) Your Project Explorer should look like this:

Set up the connect IQ Test Project

While it’s possible to write unit test code in the same connect IQ project as the project where your business logic resides, I wouldn’t advise to do so because (also in the release build) the test code will consume extra space and memory (even when marking the test extras with the (:debug_only) annotation).

To solve this extra memory issue we will set up a new project that is of the same type as the project that we want to test (in our case this project type is thus a Data Field).

File -> New -> Connect IQ Project


For the other screens we specify the same values as we specified when we created the “main” project, namely: Data Field and the watch types you’ll support:


Complex Data Type:


The Languages you’ll support:

Link the test project to the business project

Due to the memory issue that we wanted to address by creating two different projects, we now made a new problem for ourselves: how do we keep the files in sync between the test project and the business project?

We could copy files from the business project to the test project each time we want to run the tests, but this would be prone to mistakes so if we can avoid to having to copy files manually we will do so… 🙂

Luckily Eclipse comes to the rescue as it has a built-in option to create a copy by linking to another file or folder, to do this use CTRL+LMB (left mouse button) on the item you want to copy and drag it to the location above where you want to copy it to (eg when copying the strings folder copy it to the resources folder in the test project)

After releasing the left mouse button you will be presented by a window. Choose the option Link to files and folders

Now let’s do this “linked copy” for all the items that we want to link (copy from business project towards test project):
  • resources\drawables (choose yes to overwrite)
  • resources\strings (choose yes to overwrite)
  • resources-dut (choose yes to overwrite)
  • resources-eng (choose yes to overwrite)
  • source\StepsCarouselView.mc
If you did above steps correctly your project explorer will look similar to:

Mock the StepsCarousel view

The file StepsCarouselTestView.mc is the main entry point for the Steps Carousel Data Field application. This file was automatically generated when we set up the project and at the moment it isn’t any different from any other Data Field project that we created before.

We don’t want to create a new data field…

What we want is to create a new class (StepsCarouselTestsView) that knows about our real class (StepsCarouselView) but allows us to intercept and/or replace calls to the real class.

In testing jargon this is called as creating a mock, stub, test double, fake object, there are many names…

We implement this mock by using inheritance and let the StepsCarouselTestsView class inherit from the StepsCarouselView class.
We get rid of all the usings and all the function calls except for the initialize() function where we just call the initializer of the StepsCarouselView class. After all these changes the content of StepsCarouselTestsView will contain the following:

Change the run configuration

After doing the above changes you have a working project. You can this by changing your run configuration to run your test project (Run -> Run Configurations -> Choose A Project -> Steps Carousel Tests -> Run)

Clean up the test project

We have some generated items in our test project that we don’t need, let’s do some cleanup and delete the following items:
  • resources\layouts (delete folder)
  • source\StepsCarouselTestsBackground.mc (delete file)

Call the mock a mock!

  • Rename the file StepsCarouselTestsView.mc to StepsCarouselMocks.mc
  • Rename the class StepsCarouselTestsView to StepsCarouselMock
  • In the file StepsCarouselTestsApp replace StepsCarouselTestsView by StepsCarouselMock
StepsCarouselTestsApp.mc file will contain: StepsCarouselMocks.mc file will contain:

Source Code

You can download the full source code from here

What’s next?

In this tutorial we set up the test framework to be able to do unit testing, in the next tutorial we’re going to create a unit test and run it. .

Feedback

Did you like this article?Questions?
  • Post in the comments section below!

Leave a Reply