Unit testing support
I have been trying to figure out a way to write automated unit tests for the JavaScript code in the mobile app I am working on. However, because there is no actual JavaScript library behind the JavaScript Titanium.* APIs, all tests I try to write blow up because 'Titanium' cannot be found.
Given how difficult it is to interactively debug mobile apps written for this framework (and how programmatic breakpoint-style debugging is impossible), it would be immensely helpful if there was support for writing unit tests for application code that interacts with Titanium APIs. Having a skeleton of the entire JavaScript API would be a great help because it would allow developers to mock out all calls to the framework so that they could just write tests for their own code.
Are there any plans to help application developers improve their ability to write automated tests for Titanium apps?
9 Answers
-
Here is an easy receipt to start with unit testing. I have just made a unit testing framework successfully execute in a Titanium Mobile project today. It is called jsUnity and is an xUnit-type framework as per Wikipedia, so if you already know an xUnit-type framework then you'll be familiar with it immediately.
The jsUnit framework is also context-agnostic, which means it does not rely on any specific context for writing its messages to. Rather, you can override its "log" method with something that fits into your specific context. For Titanium, we can make use of this feature and write a log method that simply uses Titanium.info().Here are the steps:
- Get jsunity-0.6.js from jsUnity.com and place it into the Resources directory of your Titanium project (or in a subdirectory, depending on your project).
- Create a new file, named, for example,
TitaniumUnity.js
. Add this code to the file:
~~~
Titanium.include("jsunity-0.6.js");
jsUnity.log = function(message) {
Titanium.API.info(message);
};This overrides jsUnity's log function with a function that writes all test output to Titanium Developer's emulator console. 3. Create your unit test suites in a separate file, e.g. **`DataTest.js`**:
Titanium.include("TitaniumUnity.js");
var DataTestSuite = {
suiteName: "Data Test Suite", setUp: function() { ... }, tearDown: function() { ... },
// add your test functions:
testInit: function() { ... }, testQuery: function() { ... }
};
Titanium.UI.currentWindow.addEventListener(
'focus',
function(e) {
jsUnity.run(DataTestSuite);
});4. Add a window to your application that runs the unit tests:
var win1 = Titanium.UI.createWindow({
url: 'DataTest.js',
title:'Data Unit Test'
});Make your app switch to this window when launching, or add a tab or a button to open this window. The unit test runs each time this window gets the focus. Don't forget to remove this window (or the button or tab) from your final app :-) That's it. You are now ready for testing your objects and modules. All test output goes to the Titanium console, like this:
[INFO] Application started
[INFO] Running Value Test Suite
[INFO] 2 tests found
[INFO] 2 tests passed
[INFO] 0 tests failed
[INFO] 2 milliseconds elapsed
~~~There may be more sophisticated test suites out there (for example, JSpec looks quite impressive); however, I was searching for a test suite that was easiest to integrate with Titanium, and jsUnity's context-agnostic approach just met this requirement best.
-
Hi there,
I recently developed a unit testing framework for Titanium Mobile based on Jasmine BDD framework. It's very easy to use and works like a charm.
The source code and documentation are on GitHub: http://github.com/guilhermechapiewski/titanium-jasmine
-
I've recently gotten qunit to work inside Titanium (with a slight patch). The full version of how to make it work and code is available at: http://github.com/lukaso/qunit
You can get all the source by executing:
git clone git@github.com:lukaso/qunit.git
The README explains how to use it.
In the process, I've also figured out how Titanium.include works. It appears that when a new window is created
var unit = Titanium.UI.createWindow({ url: 'runner.js', title:'Unit Test' });
The location of the file, (in this case
runner.js
) becomes the 'home' directory for all the includes inside that file. Here 'runner.js' comes from the top level directory so the directory will beResources
.var unit = Titanium.UI.createWindow({ url: 'something/runner.js', title:'Unit Test' });
Here the
Titanium.include
directory will beResources/something
.I couldn't get
Titanium.include('../../file.js');
to work, so I highly recommend opening all your windows with urls in the top level directory (Resources
). -
This thread is very old, but there is current (June 2012) information on this topic…take a look at this blog post by Olivier Morandi on Jasmine-Node.
Its very simple to get up and running. Best of all it supports the commonJS modules you might already have.
-
With 2.1 a new framework (Anvil) was released. From the release notes (http://docs.appcelerator.com/titanium/release-notes/?version=2.1.0.GA):
New unit test framework. This release includes the first version of the new Anvil unit test framework. Anvil is designed as a replacement for the existing Drillbit unit test framework, which is based on Titanium Desktop. Anvil is written in JavaScript using Node.js, and is designed to allow for test automation. If you are currently using Drillbit and you are interested in using Anvil, see the driver/README file in the Titanium Mobile GitHub repo.
Anvil supports the same basic types of tests as Drillbit, but each test suite is defined as a CommonJS module. See driver/harnessTemplate/Resources/suites for examples of Anvil test suites.
Will take a look today as I'm keen on adding unit testing to my app.
-
Found an easy solution to write tests for Titanium/Alloy apps using Jasmine see https://github.com/tsteur/titanium-alloy-jasmine-testing . The main advantages of this solution are:
- They run under Windows, MacOSX and Linux
- Fast build time and very fast execution time
- No iOS/Android-SDK required
-
I am interested in this as well. I tried to "vote up" this question, but the forums seem broken. Ideally, Titanium could be shipped with some sort of UT framework. Failing that, you would have to do what you mention and stub out the whole freakin' library: not fun.
-
Here is my current solution, and I am working on integrating jasmine with a console logger also
Simple Unit Testing in Appcelerator Titanium Alloy http://www.slideshare.net/aaronksaunders/behave-alloy
-
TiUiTA: Titanium UI Test Automation http://tiuita.tk/