Results 1 to 2 of 2

Thread: using google test with Qt qml App

  1. #1
    Join Date
    Jun 2016
    Posts
    99
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default using google test with Qt qml App

    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)

    Qt Code:
    1. TEMPLATE = subdirs
    2. SUBDIRS += Application \
    3. AutoTests
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. #ifndef MULTIPLY_H
    2. #define MULTIPLY_H
    3.  
    4.  
    5. class Multiply
    6. {
    7. public:
    8. Multiply();
    9. int TwoNums(int x, int y);
    10. };
    11.  
    12. #endif // MULTIPLY_H
    To copy to clipboard, switch view to plain text mode 

    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)
    Qt Code:
    1. #include <gtest/gtest.h>
    2. #include <gmock/gmock-matchers.h>
    3. #include "../../Application/Application/multiply.h"
    4.  
    5. using namespace testing;
    6.  
    7. TEST(Multiply_Test1, Multiply_Set1)
    8. {
    9. Multiply multiplyObj;
    10. EXPECT_EQ(6, multiplyObj.TwoNums(2,3));
    11. ASSERT_THAT(0, Eq(0));
    12. }
    To copy to clipboard, switch view to plain text mode 

    This is my project structure
    fileStructure_Tamesa.jpg

    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)

    Qt Code:
    1. class ListView : public QObject
    2. {
    3. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. QUICK_TEST_MAIN(example)
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jfinn88; 19th May 2017 at 01:58.

  2. #2
    Join Date
    Jun 2016
    Posts
    99
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: using google test with Qt qml App

    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
    Qt Code:
    1. TestCase {
    2. name: "Test_MyItem"
    3. // MyItem{
    4. // id: item
    5. // }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. import QtQuick 2.3
    2. import QtTest 1.0
    3. //import "../../../../HelloWorld/QML/MyItem.qml"
    4.  
    5.  
    6. TestCase {
    7. name: "Test_MyItem"
    8.  
    9. // MyItem{
    10. // id: item
    11. // }
    12.  
    13. function test_testField_text() {
    14. var myString = "Hello World"
    15. compare(myString, "Hello World", "not equal")
    16. }
    17.  
    18. function test_fail() {
    19. compare(2 + 2, 5, "2 + 2 = 5")
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    C++ test harnsses main() call with MACRO
    Qt Code:
    1. #include <QtTest>
    2. #include <QtQuickTest>
    3. #include <QtQuick/QtQuick>
    4. #include <QCoreApplication>
    5.  
    6. QUICK_TEST_MAIN(Test_MyItem);
    To copy to clipboard, switch view to plain text mode 

    Here is my Project StructurefileStructure_ex.png


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

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.0
    3. import QtQuick.Dialogs 1.1
    4. import QtQml 2.2
    5.  
    6.  
    7. Item{
    8. id: item
    9. width: 400
    10. height: 500
    11.  
    12. //Sends text to c++ fcn
    13. signal submitTextField(string text)
    14.  
    15. // this function is our QML slot
    16. function setTextField(text){
    17. console.log("setTextField: " + text)
    18. textField1.text = text
    19. }
    20.  
    21. Button{
    22. id: quitButton
    23. width: 100
    24. height: 20
    25. text: "Quit"
    26. anchors.top: parent.top
    27. anchors.topMargin: 10
    28. MouseArea {
    29. anchors.fill: parent
    30. onClicked: {
    31. Qt.quit();
    32. console.log("Quit button clicked")
    33. }
    34. }
    35. }
    36.  
    37. TextField {
    38. id: textField1
    39. x: 31
    40. y: 169
    41. placeholderText: qsTr("Enter some text...")
    42. }
    43.  
    44. Button {
    45. x: 193
    46. y: 167
    47. text: qsTr("Uppercase me!")
    48. onClicked:
    49. // emit the submitTextField signal
    50. submitTextField(textField1.text)
    51. }
    52.  
    53. Button{
    54. id:openWin_btn
    55. text: "open window"
    56. anchors.left: quitButton.right
    57. anchors.top: parent.top
    58. anchors.topMargin: 10
    59. MouseArea {
    60. anchors.fill: parent
    61. onClicked:{
    62. console.log("btn clicked")
    63. //stackview.pop(item)
    64. //stackview.push(mainView_comp)
    65. //showHomeScreen();
    66. }
    67. }
    68. }
    69.  
    70. // Connections{
    71. // id: connectionForSignal
    72. // target:
    73. // }
    74.  
    75. StackView {
    76. id: stackview
    77. width: parent.width
    78. height: parent.height
    79. anchors.fill: parent
    80. //initialItem: Qt.resolvedUrl("qrc:/QML/MyItem.qml")
    81. }
    82.  
    83. // Component{
    84. // id: mainView_comp
    85. // MainView{
    86. // id: mainView_page
    87. // objectName: "mainView_page"
    88. // }
    89. // }
    90. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jfinn88; 19th May 2017 at 22:47.

Similar Threads

  1. How to add google map
    By namitha in forum Newbie
    Replies: 1
    Last Post: 17th February 2017, 10:57
  2. Replies: 1
    Last Post: 9th October 2015, 08:45
  3. QTestLib vs Google Test
    By tonka3000 in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2013, 16:55
  4. Unit Test: Test if QPushButton has a menu
    By FelixB in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2012, 13:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.