docs

How to Test OpenUI5 Controls with QUnit

Comprehensive overview of QUnit testing for controls.


Dos and Don’ts

If you stick to these rules, you will find it much easier to refactor/maintain your tests. Keeping the tests atomic will make debugging much easier, because you will hit your breakpoints for the code being tested only. If you write QUnits without keeping to these rules, you may well not notice anything bad to begin with, but you will eventually end up in the middle of a maintenance nightmare!


Arrange Act Assert Pattern

Internally, we use three templates for testing. The one shown below is the general control template.

Use the following pattern to structure your tests. If everyone sticks to this same pattern, you will be able to read your colleagues’ tests very quickly:


QUnit.test("Should do Something", function (assert) { 
    // Arrange
    
    // System under Test
    var oMyControl = new nameSpace.myControl({
    });
    
    // Act
    
    // Assert

    // Cleanup
    oMyControl.destroy();
});

Arrange

In Arrange, you should set up the dependencies and options you need for your System under Test.

Examples:


System under test

In System under Test, you should create your control and you should also render it if you want to test the rendering.


Act

Ideally, this part is only one single line of code executing the function you want to test.


Assert

This part may contain multiple statements of QUnit assertions, but ideally not too many in total.

Make sure that you also test negative paths, not only the expected ones.


Optional: Cleanup

Here you should destroy all the controls/models you created.

If you don’t use Sinon sandboxes, revert all the spies/stubs/mocks.


What Should You Test?


What Should You NOT Test?


Pitfalls


Sinon fake timers

Using fake timers can be error-prone. Fake timers should only be used with care in specific scenarios. In order to avoid pitfalls using fake timers, see the following documentation:


I’ve set a property on my control: Why aren’t the changes in the DOM?

The most likely reason for this is that it didn’t wait for the sap/ui/test/utils/nextUIUpdate Promise. OpenUI5 does not render synchronously, but waiting for the Promise will proceed the test after the rendering is done.

For more information, see Rendering and Re-rendering Controls Within Tests