Squish/Generating Test Data: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (AutoSpider moved page Category:Tools::Squish::Generating-test-data to Squish/Generating Test Data: Title name format)
m (style)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[Category:Tools::Squish]]
[[Category:Squish]]


= Generating test data =
= Generating test data =
Line 10: Line 10:


The script can look something like this:
The script can look something like this:
<code>def createTestData(fileName, size):
<code>
file = QFile("s/%s" (currentApplicationContext().cwd, fileName))
def createTestData(fileName, size):
if file.open(QIODevice.WriteOnly):
  file = QFile("%s/%s" % (currentApplicationContext().cwd, fileName))
mb = 1024*1024
  if file.open(QIODevice.WriteOnly):
if size > mb:
    mb = 1024*1024
mbData = QString()
    if size > mb:
mbData.fill('a', mb)
      mbData = QString()
count = size / mb
      mbData.fill('a', mb)
i = 0
      count = size / mb
while i < count:
      i = 0
file.write(mbData)
      while i < count:
i = i + 1
        file.write(mbData)
rest = size % mb
        i = i + 1
mbData.fill('a', rest)
      rest = size % mb
file.write(mbData)
      mbData.fill('a', rest)
else:
      file.write(mbData)
data = QString()
  else:
data.fill('a', size)
    data = QString()
file.write(data)
    data.fill('a', size)
file.close()</code>
    file.write(data)
    file.close()
</code>


Usage would be:
Usage would be:
<code>createTestData("test.txt", 543872)<code>
<code>createTestData("test.txt", 543872)</code>


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.
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.

Latest revision as of 17:27, 12 August 2020


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:
        file.write(mbData)
        i = i + 1
      rest = size % mb
      mbData.fill('a', rest)
      file.write(mbData)
   else:
     data = QString()
     data.fill('a', size)
     file.write(data)
     file.close()

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).