Results 1 to 7 of 7

Thread: QTestLib. A simple example. A list of the FAILS.

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default QTestLib. A simple example. A list of the FAILS.

    Hi,

    I expect to see a list of the FAILS (8 FAILS). But I see only two:

    Output:
    ********* Start testing of Test_Complex *********
    Config: Using QTest library 4.8.5, Qt 4.8.5
    PASS : Test_Complex::initTestCase()
    FAIL! : Test_Complex::setIm() Compared doubles are not the same (fuzzy compare)

    Actual (input): 25.5
    Expected (complex.getIm()): 0
    ../TestLib_Complex/test.cpp(19) : failure location
    FAIL! : Test_Complex::setRe() Compared doubles are not the same (fuzzy compare)

    Actual (input): 25.5
    Expected (complex.getRe()): 0
    ../TestLib_Complex/test.cpp(43) : failure location
    PASS : Test_Complex::cleanupTestCase()
    Totals: 2 passed, 2 failed, 0 skipped
    ********* Finished testing of Test_Complex *********
    Press <RETURN> to close this window...
    This is my code:

    TestLib_Complex.pro
    Qt Code:
    1. SOURCES += \
    2. test.cpp
    3.  
    4. HEADERS += \
    5. complex.h
    6.  
    7. CONFIG += qtestlib
    To copy to clipboard, switch view to plain text mode 

    complex.h
    Qt Code:
    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3.  
    4. class Complex {
    5. public:
    6. Complex(double i = 0, double r = 0)
    7. :m_im(i), m_re(r){}
    8.  
    9. void setIm(double i) {
    10. //m_im = i; // for test
    11. }
    12. void setRe(double r) {
    13. //m_re = r; // for test
    14. }
    15. double getIm() {
    16. return m_im;
    17. }
    18. double getRe() {
    19. return m_re;
    20. }
    21.  
    22. private:
    23. double m_im;
    24. double m_re;
    25. };
    26.  
    27. #endif // COMPLEX_H
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "complex.h"
    3. #include <QDebug>
    4.  
    5. class Test_Complex : public QObject {
    6. Q_OBJECT
    7. private slots:
    8. void setIm();
    9. void setRe();
    10. };
    11.  
    12. // ----------------------------------------------------------------------
    13. void Test_Complex::setIm()
    14. {
    15. Complex complex;
    16.  
    17. double input = 25.5;
    18. complex.setIm(input);
    19. QCOMPARE(input, complex.getIm());
    20.  
    21. input = -5.7;
    22. complex.setIm(input);
    23. qDebug() << "input = " << input;
    24. qDebug() << "complex.getIm() = " << complex.getIm();
    25. QCOMPARE(input, complex.getIm());
    26.  
    27. input = 0;
    28. complex.setIm(input);
    29. QCOMPARE(input, complex.getIm());
    30.  
    31. input = 5;
    32. complex.setIm(input);
    33. QCOMPARE(input, complex.getIm());
    34. }
    35.  
    36. // ----------------------------------------------------------------------
    37. void Test_Complex::setRe()
    38. {
    39. Complex complex;
    40.  
    41. double input = 25.5;
    42. complex.setRe(input);
    43. QCOMPARE(input, complex.getRe());
    44.  
    45. input = -5.7;
    46. complex.setRe(input);
    47. QCOMPARE(input, complex.getRe());
    48.  
    49. input = 0;
    50. complex.setRe(input);
    51. QCOMPARE(input, complex.getRe());
    52.  
    53. input = 5;
    54. complex.setRe(input);
    55. QCOMPARE(input, complex.getRe());
    56. }
    57.  
    58. QTEST_MAIN(Test_Complex)
    59. #include "test.moc"
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanked 23 Times in 22 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    According to the documentation, if a QCOMPARE fails the test is aborded. So you have two test fonctions, it gives you two fails.

  3. The following user says thank you to nix for this useful post:

    8Observer8 (4th October 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    The reason is that you need to be able to check assumption before continuing to test things that are based on that assumption.

    Consider a test like this
    Qt Code:
    1. QStringList result = someTestObject->someFunction();
    2. QCOMPARE(result.count(), 2);
    3. QCOMPARE(result[0], "some expected string");
    4. QCOMPARE(result[1], "some other expected string");
    To copy to clipboard, switch view to plain text mode 

    If the first QCOMPARE would not abort the test, the second or third one would lead to an assert() in QList, crashing the test program.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    8Observer8 (4th October 2013)

  6. #4
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    Thank you, guys! I understand.


    Added after 24 minutes:


    Please, say me. Is this correct code for testing of my class? I implemented testing for 'get' and 'set' methods.

    I took qFuzzyCompare() function from here: http://www.qtcentre.org/threads/5290...ers?highlight=

    I duplicate my code in my main project and in my TestLib_Comlex project. Must I copy/past my class (Complex) to main project all time?

    Sorry for my English.

    This is all my testLib project:

    TestLib_Comlex.pro
    Qt Code:
    1. SOURCES += \
    2. test.cpp
    3.  
    4. HEADERS += \
    5. complex.h
    6.  
    7. CONFIG += qtestlib
    To copy to clipboard, switch view to plain text mode 

    complex.h
    Qt Code:
    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3.  
    4. class Complex {
    5. public:
    6. Complex(double i = 0, double r = 0)
    7. :m_im(i), m_re(r){}
    8.  
    9. void setIm(double i) {
    10. m_im = i;
    11. }
    12. void setRe(double r) {
    13. m_re = r;
    14. }
    15. double getIm() {
    16. return m_im;
    17. }
    18. double getRe() {
    19. return m_re;
    20. }
    21.  
    22. private:
    23. double m_im;
    24. double m_re;
    25. };
    26.  
    27. #endif // COMPLEX_H
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "complex.h"
    3. #include <QDebug>
    4.  
    5. class Test_Complex : public QObject {
    6. Q_OBJECT
    7. private slots:
    8. void setIm();
    9. void setRe();
    10.  
    11. static inline bool qFuzzyCompare(double p1, double p2, double delta)
    12. {
    13. return (qAbs(p1 - p2) <= delta * qMin(qAbs(p1), qAbs(p2)));
    14. }
    15. };
    16.  
    17. // ----------------------------------------------------------------------
    18. void Test_Complex::setIm()
    19. {
    20. Complex complex;
    21.  
    22. double input = 25.5123;
    23. complex.setIm(input);
    24. double expeted = complex.getIm();
    25. double delta = 0.0001;
    26. QString message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    27. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    28.  
    29. input = -5.7;
    30. complex.setIm(input);
    31. expeted = complex.getIm();
    32. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    33. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    34.  
    35. input = 0;
    36. complex.setIm(input);
    37. expeted = complex.getIm();
    38. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    39. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    40.  
    41. input = 5;
    42. complex.setIm(input);
    43. expeted = complex.getIm();
    44. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    45. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    46. }
    47.  
    48. // ----------------------------------------------------------------------
    49. void Test_Complex::setRe()
    50. {
    51. Complex complex;
    52.  
    53. double input = 125.5;
    54. complex.setRe(input);
    55. double expeted = complex.getRe();
    56. double delta = 0.0001;
    57. QString message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    58. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    59.  
    60. input = -15.7;
    61. complex.setRe(input);
    62. expeted = complex.getRe();
    63. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    64. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    65.  
    66. input = 10;
    67. complex.setRe(input);
    68. expeted = complex.getRe();
    69. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    70. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    71.  
    72. input = 15;
    73. complex.setRe(input);
    74. expeted = complex.getRe();
    75. message = QString("input = %1; expeted = %2; delta = %3").arg(input).arg(expeted).arg(delta);
    76. QVERIFY2(qFuzzyCompare(input, expeted, delta), message.toAscii());
    77. }
    78.  
    79. QTEST_MAIN(Test_Complex)
    80. #include "test.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by 8Observer8; 4th October 2013 at 13:11.

  7. #5
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    Hi,

    I read about an another way of testing via addColumn(), newRow() and QFETCH. I like this way, but...

    I cannot understand this errors:

    ********* Start testing of Test_Complex *********
    Config: Using QTest library 4.8.5, Qt 4.8.5
    PASS : Test_Complex::initTestCase()
    QDEBUG : Test_Complex::setIm() expected data of type 'double', got 'int' for ele
    ment 0 of data with tag 'setIm_test3'
    QFATAL : Test_Complex::setIm() ASSERT: "false" in file qtestdata.cpp, line 94
    FAIL! : Test_Complex::setIm() Received a fatal error.
    Unknown file(0) : failure location
    Totals: 1 passed, 1 failed, 0 skipped
    ********* Finished testing of Test_Complex *********

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    Press <RETURN> to close this window...
    TestLib_Comlex_table.pro
    Qt Code:
    1. HEADERS += \
    2. complex.h
    3.  
    4. SOURCES += \
    5. test.cpp
    6.  
    7. CONFIG += qtestlib
    To copy to clipboard, switch view to plain text mode 

    complex.h
    Qt Code:
    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3.  
    4. class Complex {
    5. public:
    6. Complex(double i = 0, double r = 0)
    7. :m_im(i), m_re(r){}
    8.  
    9. void setIm(double i) {
    10. m_im = i;
    11. }
    12. void setRe(double r) {
    13. m_re = r;
    14. }
    15. double getIm() {
    16. return m_im;
    17. }
    18. double getRe() {
    19. return m_re;
    20. }
    21.  
    22. Complex operator+(Complex c) {
    23. Complex sum;
    24. sum.m_im = m_im + c.m_im;
    25. sum.m_re = m_re + c.m_re;
    26. return sum;
    27. }
    28.  
    29. private:
    30. double m_im;
    31. double m_re;
    32. };
    33.  
    34. #endif // COMPLEX_H
    To copy to clipboard, switch view to plain text mode 

    test.cpp
    Qt Code:
    1. #include <QtTest>
    2. #include "complex.h"
    3.  
    4. // ======================================================================
    5. class Test_Complex : public QObject {
    6. Q_OBJECT
    7. private slots:
    8. void setIm_data();
    9. void setRe_data();
    10.  
    11. void setIm();
    12. void setRe();
    13. };
    14.  
    15. // ----------------------------------------------------------------------
    16. void Test_Complex::setIm_data()
    17. {
    18. QTest::addColumn<double>("arg1");
    19. QTest::addColumn<double>("result");
    20.  
    21. QTest::newRow("setIm_test1") << 0.0 << 0.0;
    22. QTest::newRow("setIm_test2") << -12.5 << -12.5;
    23. QTest::newRow("setIm_test3") << 2007 << 2007;
    24. QTest::newRow("setIm_test4") << 5.1 << 5.1;
    25. }
    26.  
    27. // ----------------------------------------------------------------------
    28. void Test_Complex::setRe_data()
    29. {
    30. QTest::addColumn<double>("arg1");
    31. QTest::addColumn<double>("result");
    32.  
    33. QTest::newRow("setRe_test1") << 0.0 << 0.0;
    34. QTest::newRow("setRe_test2") << -12.5 << -12.5;
    35. QTest::newRow("setRe_test3") << 2007 << 2007;
    36. QTest::newRow("setRe_test4") << 5.1 << 5.1;
    37. }
    38.  
    39. // ----------------------------------------------------------------------
    40. void Test_Complex::setIm()
    41. {
    42. Complex complex;
    43. QFETCH(double, arg1);
    44. QFETCH(double, result);
    45.  
    46. complex.setIm(arg1);
    47.  
    48. QCOMPARE(complex.getIm(), result);
    49. }
    50.  
    51. // ----------------------------------------------------------------------
    52. void Test_Complex::setRe()
    53. {
    54. Complex complex;
    55. QFETCH(double, arg1);
    56. QFETCH(double, result);
    57.  
    58. complex.setRe(arg1);
    59.  
    60. QCOMPARE(complex.getRe(), result);
    61. }
    62.  
    63. QTEST_MAIN(Test_Complex)
    64. #include "test.moc"
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanked 23 Times in 22 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    Use the rigth type for data in setIm_test3 to double instead of int ( 2007 become 2007.0 )

  9. The following user says thank you to nix for this useful post:

    8Observer8 (5th October 2013)

  10. #7
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default Re: QTestLib. A simple example. A list of the FAILS.

    Yes, it works now! Thank you very much!

Similar Threads

  1. Replies: 1
    Last Post: 7th March 2013, 00:30
  2. QTestlib problem
    By asinghma in forum Newbie
    Replies: 0
    Last Post: 25th October 2010, 10:56
  3. QTestlib - How to add own types
    By AlGaN in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2010, 19:22
  4. qtestlib
    By Jordan in forum Qt Programming
    Replies: 7
    Last Post: 28th September 2010, 11:47
  5. Replies: 3
    Last Post: 11th June 2009, 04:26

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
  •  
Qt is a trademark of The Qt Company.