Squish/Generating Test Data

From Qt Wiki
Jump to: navigation, search

Generating test data

In the cases where you are testing an application that works with files, be it a file transfer application, editor or your own checksum calculator, you might benefit from testing it with different file types and/or file sizes. This article will show you how to generate these test files on-the-fly, so you will not have to keep them around in your revision control system where they simply take up space.

Normally, when you work with test data in Squish, you will import some file using the IDE, which will copy that file into your suite_testsuite/tst_case/testdata or suite_testsuite/shared/testdata folder. You will then get it to your application using testData.put(). The beauty of testData.put() is that it also works across the network, if you are testing your application on e.g. an embedded system and have the squishrunner running on your local desktop machine.

However, we can accomplish that even with generated files, without importing them using the IDE, and without copying them to the test system using testData.put(). This is because of Squish's built-in Qt bindings. As shown in various Squish articles in this wiki ( here, here and here) you can use the Qt classes and functions from within your test script, and we will use this support for generating the test files on the system where the AUT is being executed.

The script can look something like this:

def createTestData(fileName, size):
  file = QFile("%s/%s" % (currentApplicationContext().cwd, fileName))
  if file.open(QIODevice.WriteOnly):
    mb = 1024*1024
    if size > mb:
      mbData = QString()
      mbData.fill('a', mb)
      count = size / mb
      i = 0
      while i < count:
        i = i + 1
      rest = size % mb
      mbData.fill('a', rest)
     data = QString()
     data.fill('a', size)

Usage would be:

createTestData("test.txt", 543872)

This script simply generates a file with a specified file size and a specified file name, puts the file into the current working directory of the AUT, and fills it with the character "a". As you can see from the function, it writes the data one megabyte at the time. This is to avoid allocating the full file size of data at once, hence getting the AUT to possibly consume a lot of the machines memory resources.

The working directory where the files are put can be customized in the Test Suite Settings in the IDE, by simply changing the working directory of the AUT.

Of course, this is only one way to actually generate the data. If you need more sophisticated data, or actually some proper content, you will of course have to modify this function. When doing so, just make sure to use the Qt API for creating the files and writing the data, as using Python's ditto will be executed in the context of the squishrunner (i.e. on your desktop machine), while using the Qt API it will be executed in the context of the AUT (i.e. the embedded device).