Results 1 to 12 of 12

Thread: QtTest and asserts

  1. #1
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QtTest and asserts

    I was wondering how to deal with asserts fired from a function that is tested in a QTest module. Imagine, you want to test a method bool xyz() that internally uses a precondition that might fire if something is inconsistent. QCOMPARE(xyz(), true) would never be executed because xyz() already exists with an assert.

    Is this simply not supported in the Qt test environment, does it need to somehow managed manually (using some global semaphore for example) or did I just not get it?

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest and asserts

    I think that a function whose assert fires is not working properly and is not ready for testing at all. Besides, there are not so many cases when you should keep asserts in your production code.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtTest and asserts

    I get the point but:
    - I would actually also want to test if the pre-conditions, post-conditions, invariants and asserts are working as supposed in my test
    - I'm very much convinced (and 20 years of C++ experience never proved me wrong) that assertions are even more important in production code then in debug version

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest and asserts

    Could you post some sample code of such function, demonstrating what you are trying to do?
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Unhappy Re: QtTest and asserts

    The following example should show what I'm talking about. my_assert would be my own assert macro that usually logs the error and reports it to the user before aborting the application.

    I was now wondering, how to test a function that is supposed to fail (by asserting or raising an exception) in QtTest?

    Qt Code:
    1. #include <QtTest/QtTest>
    2.  
    3. static float myFunction(float value)
    4. {
    5. my_assert(value > 0.0); // check precondition
    6. return 2 / value;
    7. }
    8.  
    9. class tst_QDate : public QObject {
    10. Q_OBJECT
    11. public:
    12. tst_myClass() {}
    13. virtual ~tst_myClass() {}
    14. public slots:
    15. void init() {}
    16. void cleanup() {}
    17. private slots:
    18. void myClass();
    19. };
    20.  
    21. void tst_QDate::myClass()
    22. {
    23. QCOMPARE(float(1), myFunction(2.0));
    24.  
    25. // Next QCOMPARE should fail (e.g. raise an exception)
    26. QCOMPARE(float(1), myFunction(0.0));
    27. }
    28.  
    29. QTEST_APPLESS_MAIN(tst_myClass)
    30.  
    31. #include "moc_tst_myClass.cxx"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest and asserts

    How is it better than:
    Qt Code:
    1. static float myFunction(float value)
    2. {
    3. if (value <= 0.0) // check precondition
    4. {
    5. return eNAN;
    6. }
    7. return 2 / value;
    8. }
    To copy to clipboard, switch view to plain text mode 
    ?

    I realize that after your function is completely polished you can remove your assert and doing "if" will not take time. But, I think that your function should be error-resistable, since it gets value inputted by someone. Your function should take care of exceptional/errorneous situation (in your case, it is division by zero).
    Lets pretend that your app reads numbers from some file and does divisions for them. This file is created by somebody who put 0 in that file.
    Way 1: Your application is crashed by your assert only because someone was not careful enough and put 0 in that file.
    Way 2: On processing that division by 0, your application returns NAN value and reports that wrong arguments have been specified.

    What would you choose? I think that the 2nd way is more correct.
    I'm a rebel in the S.D.G.

  7. #7
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtTest and asserts

    We seem to be moving a little away from my actual question but I'm very interested in this more generic topic as well. My answer to your question would (as always) be: IT DEPENDS!
    It depends on what the contract between invoking function and the invoked one specifies and this can be to either not to care at all (most common), return a status and leave it to the caller to decide (your suggestion) or impose a strict rule of parameter passing using a precondition (this is what I wanted to do).

    A more realistic (longer) version of myClass::myFunction would typically look like this:
    Qt Code:
    1. float myClass::myFunction(float value)
    2. {
    3. /* test consistency of class */
    4. my_class_invariant(...);
    5.  
    6. /*test consistency of parameter */
    7. my_precondition(value > 0, "you are using an invalid value=("+toString(value)+")");
    8.  
    9. /* usually the algorithms are "little" more complex */
    10. float temp = 10.0 / value;
    11.  
    12. /* test consistency of intermediate result */
    13. my_assert(temp > 0.0 && ...);
    14.  
    15. /* test consistency of result */
    16. my_postcondition(fuzzy_compare(temp * value, 10.0);
    17.  
    18. return temp;
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtTest and asserts

    Quote Originally Posted by doberkofler View Post
    - I'm very much convinced (and 20 years of C++ experience never proved me wrong) that assertions are even more important in production code then in debug version
    If an assert triggers during tests it means it will fire in the production code. All asserts should be removed prior to testing (which is easy - just build your app in release mode). If you still want to test the conditions, substitute asserts with regular checks and exit the function gracefully.

    If you insist on asserts not killing your application, redefine the assert/Q_ASSERT macro to do something else than abort the process.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtTest and asserts

    Quote Originally Posted by wysota View Post
    If an assert triggers during tests it means it will fire in the production code. All asserts should be removed prior to testing (which is easy - just build your app in release mode). If you still want to test the conditions, substitute asserts with regular checks and exit the function gracefully.
    As I explained in my reply to lyuts, I very strongly believe that there is no rule or best practice on how to deal with assertions. It really depends and I have made some very positive experience over a long period of time in keeping all asserts in the production code.

    Quote Originally Posted by wysota View Post
    If you insist on asserts not killing your application, redefine the assert/Q_ASSERT macro to do something else than abort the process.
    Thank you for making this clear. I just wanted to make sure, that there is no explicit functionality in QtTest that would intercept an exception instead of doing this myself.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtTest and asserts

    Quote Originally Posted by doberkofler View Post
    As I explained in my reply to lyuts, I very strongly believe that there is no rule or best practice on how to deal with assertions.
    Assert, by definition, kills your app. If you want an assert that will not kill your app but instead will return an error, it will not be an assert anymore.

    Thank you for making this clear. I just wanted to make sure, that there is no explicit functionality in QtTest that would intercept an exception instead of doing this myself.
    Asserts don't throw exceptions. They call abort(). So there is nothing to "intercept". Even as the manual for abort says - "abort() does not return". If you want, just throw an exception instead of asserting and you'll be able to catch that in QtTestLib.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    doberkofler (20th September 2010)

  12. #11
    Join Date
    Oct 2009
    Location
    Vienna, Austria
    Posts
    57
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QtTest and asserts (SOLVED)

    Quote Originally Posted by wysota View Post
    Assert, by definition, kills your app. If you want an assert that will not kill your app but instead will return an error, it will not be an assert anymore.
    I agree! My "assert" would log an error, show the error and then abort.

    Quote Originally Posted by wysota View Post
    Asserts don't throw exceptions. They call abort(). So there is nothing to "intercept". Even as the manual for abort says - "abort() does not return". If you want, just throw an exception instead of asserting and you'll be able to catch that in QtTestLib.
    Sorry for the misunderstanding, but I used the term exception in a generally and did not specifically refer to the C++ exception handling. More precisely put, I was unsure if QtTest might intercept the assert.

    Intercepting my own assert handling is actually what I have done now and it seems to give me expected results. QtTets can actually catch C++ exception thrown by the test methods.

  13. #12
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest and asserts (SOLVED)

    The only place where I use asserts is in my internal classes. By saying internal, I mean those that do not provide public interface and is used only by me in my modules. Otherwise, I try to do regular checks.
    I'm a rebel in the S.D.G.

Similar Threads

  1. QTTest and my own gui
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 19th March 2010, 10:26
  2. QtTest example not compiling
    By GrahamLabdon in forum Newbie
    Replies: 3
    Last Post: 19th March 2010, 09:41
  3. redirecting ASSERTs
    By drhex in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2009, 21:59
  4. Back tracing asserts with gdb
    By ucomesdag in forum Qt Programming
    Replies: 8
    Last Post: 30th July 2007, 21:59
  5. QtTest bug
    By graeme in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:16

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.