Squish/Generating Test Data: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Generating test data=
[[Category:Tools::Squish]]
 
= 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.
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 <span class="caps">IDE</span>, 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.
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 <span class="caps">IDE</span>, 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 ([[:Category:Tools::Squish::Verifying the existence of a menu item|here]], [[:Category:Tools::Squish::Iterating through a table|here]] and [[Robust selection of listview or treeview items with variable text|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 <span class="caps">AUT</span> is being executed.
 
The script can look something like this:<br />


Usage would be:<br />
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 ([[:Category:Tools::Squish::Verifying_the_existence_of_a_menu_item | here]], [[:Category:Tools::Squish::Iterating_through_a_table | here]] and [[:Robust_selection_of_listview_or_treeview_items_with_variable_text | 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.


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 <span class="caps">AUT</span>, 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 <span class="caps">AUT</span> to possibly consume a lot of the machines memory resources.
The script can look something like this:<br /><code>def createTestData(fileName, size):<br /> file = QFile&amp;amp;#40;&quot;s/%s&amp;quot; (currentApplicationContext(&amp;#41;.cwd, fileName))<br /> if file.open(QIODevice.WriteOnly):<br /> mb = 1024*1024<br /> if size &gt; mb:<br /> mbData = QString()<br /> mbData.fill('a', mb)<br /> count = size / mb<br /> i = 0<br /> while i &lt; count:<br /> file.write(mbData)<br /> i = i + 1<br /> rest = size % mb<br /> mbData.fill('a', rest)<br /> file.write(mbData)<br /> else:<br /> data = QString()<br /> data.fill('a', size)<br /> file.write(data)<br /> file.close()</code>


The working directory where the files are put can be customized in the Test Suite Settings in the <span class="caps">IDE</span>, by simply changing the working directory of the <span class="caps">AUT</span>.
Usage would be:<br /><code>createTestData(&quot;test.txt&amp;quot;, 543872)<code>


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 <span class="caps">API</span> 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 <span class="caps">API</span> it will be executed in the context of the <span class="caps">AUT</span> (i.e. the embedded device).
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 &quot;a&amp;quot;. 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.


===Categories:===
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.


* [[:Category:Tools|Tools]]
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).
** [[:Category:Tools::Squish|Squish]]

Revision as of 10:23, 24 February 2015


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):<br /> file = QFile&amp;amp;#40;&quot;s/%s&amp;quot; (currentApplicationContext(&amp;#41;.cwd, fileName))<br /> if file.open(QIODevice.WriteOnly):<br /> mb = 1024*1024<br /> if size &gt; mb:<br /> mbData = QString()<br /> mbData.fill('a', mb)<br /> count = size / mb<br /> i = 0<br /> while i &lt; count:<br /> file.write(mbData)<br /> i = i + 1<br /> rest = size % mb<br /> mbData.fill('a', rest)<br /> file.write(mbData)<br /> else:<br /> data = QString()<br /> data.fill('a', size)<br /> file.write(data)<br /> file.close()

Usage would be:
createTestData("test.txt&quot;, 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&quot;. 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).