Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Squish/Using Helper Objects: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Using Helper Objects=
[[Category:Tools::Squish]]


When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can’t easily access. A helper object, in this context, is a QObject that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you’re trying to verify application state that can’t easily be checked using native Qt and Squish functions.
= Using Helper Objects =


As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a <span class="caps">QGLW</span>idget instead of trying to use a screenshot verification. Ideally a helper object’s methods should ''not'' have side effects, as an ideal Squish test should only be testing what a user can do with normal input.
When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can't easily access. A helper object, in this context, is a QObject that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you're trying to verify application state that can't easily be checked using native Qt and Squish functions.


==An Example C++ Helper Object==
As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a QGLWidget instead of trying to use a screenshot verification. Ideally a helper object's methods should ''not'' have side effects, as an ideal Squish test should only be testing what a user can do with normal input.


The following class definition provides the method '''SquishHelper::help''', which Squish can invoke and get an integer result from. Note that '''SquishHelper::help''' is flagged as '''Q_INVOKABLE''', this is important! A method used by Squish must be accessible via Qt’s metaobject system, which means methods that are either slots or '''Q_INVOKABLE'''.
== An Example C++ Helper Object ==


After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the QObject parent of the helper object is a <span class="caps">GUI</span> element Squish shows in its object tree for the application, such as the main window. This ensures that Squish can successfully look up the object later.<br />
The following class definition provides the method '''SquishHelper::help''', which Squish can invoke and get an integer result from. Note that '''SquishHelper::help''' is flagged as '''Q_INVOKABLE''', this is important! A method used by Squish must be accessible via Qt's metaobject system, which means methods that are either slots or '''Q_INVOKABLE'''.


==Using a Helper Object From Squish==
<code>#include &lt;QObject&amp;gt;


With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do:<br />
class SquishHelper : public QObject<br />{<br />public:<br /> SquishHelper(QObject* parent);<br /> ~SquishHelper();


This can be very useful to gain information about your application’s state, as you can use any C++ code you normally would, like in any unit test.
Q_INVOKABLE int help() const; //only offer a non-const method if you have a good reason<br />};</code>


===Categories:===
After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the QObject parent of the helper object is a GUI element Squish shows in its object tree for the application, such as the main window. This ensures that Squish can successfully look up the object later.<br /><code>…<br />SquishHelper* helper = new SquishHelper(this);<br />helper-&gt;setName(&quot;Helper1&amp;quot;);<br />…</code>


* [[:Category:Tools|Tools]]
== Using a Helper Object From Squish ==
** [[:Category:Tools::Squish|Squish]]
 
With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do:<br /><code>…<br />helper = findObject(&quot;{name='Helper1' type='SquishHelper')&quot;)<br />test.compare(helper.help(), 0)<br />…</code>

Revision as of 10:36, 24 February 2015


Using Helper Objects

When writing a Squish test, it is often helpful to provide a helper object in your application code to provide information your test can't easily access. A helper object, in this context, is a QObject that provides methods useful to Squish tests that is instantiated in the application for Squish to find. A helper object may be a good fit if you're trying to verify application state that can't easily be checked using native Qt and Squish functions.

As a contrived example, it might be useful to provide a helper object to get the color of a model rendered in a QGLWidget instead of trying to use a screenshot verification. Ideally a helper object's methods should not have side effects, as an ideal Squish test should only be testing what a user can do with normal input.

An Example C++ Helper Object

The following class definition provides the method SquishHelper::help, which Squish can invoke and get an integer result from. Note that SquishHelper::help is flagged as Q_INVOKABLE, this is important! A method used by Squish must be accessible via Qt's metaobject system, which means methods that are either slots or Q_INVOKABLE.

#include &lt;QObject&amp;gt;

class SquishHelper : public QObject<br />{<br />public:<br /> SquishHelper(QObject* parent);<br /> ~SquishHelper();

Q_INVOKABLE int help() const; //only offer a non-const method if you have a good reason<br />};

After defining the class, merely instantiate it somewhere and optionally give it a name if there may be multiple instances. Make sure that the QObject parent of the helper object is a GUI element Squish shows in its object tree for the application, such as the main window. This ensures that Squish can successfully look up the object later.

<br />SquishHelper* helper = new SquishHelper(this);<br />helper-&gt;setName(&quot;Helper1&amp;quot;);<br />

Using a Helper Object From Squish

With the above helper object, looking up and invoking methods on the object is trivial. For instance, to look up the object and check the result of help in Python, you could do:

<br />helper = findObject(&quot;{name='Helper1' type='SquishHelper')&quot;)<br />test.compare(helper.help(), 0)<br />