PDA

View Full Version : using google test with Qt qml App



jfinn88
18th May 2017, 22:26
I'm new to TDD and Google Unit Test and having issues setting up some simple testing for class that #include <QObject>.

I have got the testing libraries installed for Qt Creator and have the Auto test plugin enabled
qtdeclarative5-dev-tools qtdeclarative5-test-plugin

I have a SUBDIR project that has two projects in it: one project is for the QML/C++ application and the other sub project is for Auto Unit Testing (Google Test & QtQuick Test)



TEMPLATE = subdirs
SUBDIRS += Application \
AutoTests


I can create a simple class (multiply.cpp) in my qml/c++ app and able to test it in the my other sub-project GTest
Header file for multiply.cpp


#ifndef MULTIPLY_H
#define MULTIPLY_H


class Multiply
{
public:
Multiply();
int TwoNums(int x, int y);
};

#endif // MULTIPLY_H


Here is my Test Function for my multiply class example (I had to add existing files and select my multiply.cpp file and add it to this sub=projects sources)

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "../../Application/Application/multiply.h"

using namespace testing;

TEST(Multiply_Test1, Multiply_Set1)
{
Multiply multiplyObj;
EXPECT_EQ(6, multiplyObj.TwoNums(2,3));
ASSERT_THAT(0, Eq(0));
}


This is my project structure
12477

I don't know if I need to mock the class to take away the dependencies of QObject sub class ? I not sure how to write an appropriate test for something that sub-classes another object

I'm having issue testing any class that use a sub class like QObject, Get errors saying fatal error: QObject: No such file or directory (this happens for each #include)



class ListView : public QObject
{
Q_OBJECT


I'm also trying to get QtQuick Test to work with this project too but not having any luck understanding how to implement it fully
I set up an Auto Test SubDir
I have tried creating a test_Button.qml file with test cases in it
I added Macro for running QtQuick Test to a .cpp file so that the test "harness" can run the tests (test harness scans current directory for tst_ files) unless environment variable is set specifying path


QUICK_TEST_MAIN(example)

I'm not sure with the SUBDIR structure where my Quick Unit test should live

I'm I setting everything up correctly Google test? and QtQuick Test?

What is the best way to implement Auto Unit Testing in Qt using Google Tests ?

any advice or examples would be awesome

jfinn88
19th May 2017, 21:28
I been trying to get QtQuick Test implemented into and a simple project QML/C++.

I have created a subdir project with two sub projects one project holds app (QML/C++)

I used the Qt Creator Project wizard to create these sub-projects

I have the test libraries installed and have the plugin enabled for AutoTests
Libraries: qtdeclarative5-dev-tools qtdeclarative5-test-plugin

I created a QML file in my auto test sub-project called tst_MyItem.qml
this is where I want to create my unit test using javaScript and TestCase object

In Order to test my QML file in my Application subproject:
I have tried importing the QML file into the tst_MyItem.qml file (says can't find it ...)
I have right clicked on my AutoTest sub project add Existing files and added MyItem.qml file from my Application sub-project into my AutoTest Sub-project
I have Tried adding MyItem as a component into tst_MyItem.qml
I have tried creating the tst_MyItem.qml (with javaScript Unit test funitons in it) inside my Application sub-project (mainPage) and tried calling macro from AutoTest Sub projects

TestCase {
name: "Test_MyItem"
// MyItem{
// id: item
// }


I can create simple TestCase that are irrelevant to the QML I need to test and run it and have it pass or fail but not sure how to test the QML code I have written

Ex

import QtQuick 2.3
import QtTest 1.0
//import "../../../../HelloWorld/QML/MyItem.qml"


TestCase {
name: "Test_MyItem"

// MyItem{
// id: item
// }

function test_testField_text() {
var myString = "Hello World"
compare(myString, "Hello World", "not equal")
}

function test_fail() {
compare(2 + 2, 5, "2 + 2 = 5")
}
}


C++ test harnsses main() call with MACRO


#include <QtTest>
#include <QtQuickTest>
#include <QtQuick/QtQuick>
#include <QCoreApplication>

QUICK_TEST_MAIN(Test_MyItem);


Here is my Project Structure12478


Here is MyItem.qml I want to write Unit tests for which is in my sub-project Application



import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.1
import QtQml 2.2


Item{
id: item
width: 400
height: 500

//Sends text to c++ fcn
signal submitTextField(string text)

// this function is our QML slot
function setTextField(text){
console.log("setTextField: " + text)
textField1.text = text
}

Button{
id: quitButton
width: 100
height: 20
text: "Quit"
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
console.log("Quit button clicked")
}
}
}

TextField {
id: textField1
x: 31
y: 169
placeholderText: qsTr("Enter some text...")
}

Button {
x: 193
y: 167
text: qsTr("Uppercase me!")
onClicked:
// emit the submitTextField signal
submitTextField(textField1.text)
}

Button{
id:openWin_btn
text: "open window"
anchors.left: quitButton.right
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked:{
console.log("btn clicked")
//stackview.pop(item)
//stackview.push(mainView_comp)
//showHomeScreen();
}
}
}

// Connections{
// id: connectionForSignal
// target:
// }

StackView {
id: stackview
width: parent.width
height: parent.height
anchors.fill: parent
//initialItem: Qt.resolvedUrl("qrc:/QML/MyItem.qml")
}

// Component{
// id: mainView_comp
// MainView{
// id: mainView_page
// objectName: "mainView_page"
// }
// }
}