Skip to content

approvals/ApprovalTests.cpp.Qt

Repository files navigation

ApprovalTests.cpp.Qt

Build status Actions Status License

Download the latest version (v.0.0.3) of the single header file here.

Contents

Purpose

To enable easy, convenient testing and Approval Testing of Qt-based C++ code by using modern test frameworks.

Currently Catch is supported.

Features

  • Creates a QApplication automatically, so that tests that create Qt widgets can run.
  • Verify QImage. You give it a QImage, it saves this as a .png, and then verifies it by reading it back in.
  • Verify QTableView. You give it a QTableView, and it saves its text contents in a tab-separated .tsv file.
  • Augments ApprovalTests so that .png files are read in to QImage objects which are then compared - this is more reliable and portable than the default behaviour of doing character-by-character comparison of .png files on disk.
  • Provides nicely-readable string representations of a growing range of Qt classes.

Requirements

  • Qt's Widgets and Tests modules

Getting Started

You can download the Starter Project and start experimenting.

Limitations

  • IMPORTANT: Do not use the Qt Test macros, such as QCOMPARE, QVERIFY, QTRY_COMPARE and so on. Currently, any test failures of all any of the Qt Test macros will be silently swallowed, and tests will spuriously pass.
  • Currently, there are compile-time checks to prevent incorrect use of QCOMPARE, but not the others.
  • Please note that this is a very early implementation, and there is not yet any promise of a stable interface.

Reference

Setting up your main

// main.cpp:
#define APPROVALS_CATCH_QT
#include "ApprovalTestsQt.hpp"

snippet source | anchor

Verifying a QImage

This takes a QImage, and saves this as a .png, and then verifies it by reading it back in.

In general, it's better to verify behaviour of objects rather than their appearance. However, if you do wish to test the contents of a QImage, this will work well.

QImage image(10, 20, QImage::Format_RGB32);
image.fill(Qt::red);
ApprovalTestsQt::verifyQImage(image);

snippet source | anchor

Verifying a QTableView, e.g. QTableWidget

This takes QTableView, and saves its text contents in a tab-separated .tsv, for verification.

QTableWidget tableWidget;
populateTable(tableWidget);
ApprovalTestsQt::verifyQTableView(tableWidget);

snippet source | anchor

What's new?

Check out the Features page or upcoming release notes to see what we've been working on lately, or browse the past release notes.