Results 1 to 3 of 3

Thread: QTestLib - Testing Exceptions

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTestLib - Testing Exceptions

    Hi,

    I'm in the process of migrating a bunch of tests from UnitTest++ to QTestLib. I've gotten almost everything working aside from one thing - testing exceptions.

    For example, using UnitTest++, I can verify the following function throws an exception using the CHECK_THROW macro.

    Qt Code:
    1. int foo(int i)
    2. {
    3. if (i < 0)
    4. throw std::invalid_argument("Invalid value for i");
    5.  
    6. // Do something
    7. return i * 2;
    8. }
    9.  
    10. ...
    11.  
    12. // Ensure exception is thrown when negative value of i is passed
    13. CHECK_THROW(foo(-1), std::invalid_argument);
    14.  
    15. // Ensure valid data works
    16. CHECK(foo(0) == 0);
    17. CHECK(foo(1) == 2);
    To copy to clipboard, switch view to plain text mode 

    Is there a suitable equivalent in QTestLib?

    Thanks for any help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTestLib - Testing Exceptions

    Could you do something like:
    Qt Code:
    1. try {
    2. foo(-1);
    3. QFAIL("Exception not thrown");
    4. }
    5. catch (...) {
    6. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. bool exceptionSeen = false;
    2. try {
    3. foo(-1);
    4. }
    5. catch (std::invalid_argument&) {
    6. exceptionSeen = true;
    7. }
    8. QCOMPARE(exceptionSeen, true);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTestLib - Testing Exceptions

    Thanks for the reply. I was hoping there might be an existing macro/feature I had overlooked, but I guess not.

    I ended up creating a macro using a hybrid of the CHECK_THROW and QVERIFY macros:

    Qt Code:
    1. #define QVERIFY_THROW(expression, ExpectedExceptionType) \
    2. do \
    3. { \
    4. bool caught_ = false; \
    5. try { expression; } \
    6. catch (ExpectedExceptionType const&) { caught_ = true; } \
    7. catch (...) {} \
    8. if (!QTest::qVerify(caught_, #expression ", " #ExpectedExceptionType, "", __FILE__, __LINE__))\
    9. return; \
    10. } while(0)
    11.  
    12. ...
    13.  
    14. QVERIFY_THROW(foo(-1), std::invalid_argument);
    To copy to clipboard, switch view to plain text mode 

    Pretty much the same as your 2nd suggestion. Seems to work fine.

  4. The following user says thank you to Dashboard for this useful post:

    jerobarraco (5th October 2012)

Similar Threads

  1. Testing modal dialogs with QTestLib
    By bitflyer in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2013, 15:34
  2. Testing Qt-Software with a GUI Testing Tool
    By nightghost in forum Qt Programming
    Replies: 8
    Last Post: 22nd February 2012, 07:43
  3. qtestlib
    By Jordan in forum Qt Programming
    Replies: 7
    Last Post: 28th September 2010, 11:47
  4. GUI Event testing using QTestLib
    By vels in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2010, 11:31
  5. testing with QTestLib
    By hyling in forum Qt Programming
    Replies: 2
    Last Post: 12th July 2007, 20:37

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.