Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do you do unit testing?

How do you do unit testing?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 13.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    esdrasbeleza
    wrote on last edited by
    #1

    Hi,

    I'm trying to develop my first Qt application with unit tests, but before starting I'd like to collect some good practices, so here are some questions:

    1. If you use Qt Creator: do you put your unit test classes in the same project of your application?

    2. I'm trying to make my unit test classes separated, but I found documentation only about how to create an unique class that contains all tests and call QTEST_APPLESS_MAIN(). Does anyone have any tip and/or example about how to keep them separated, clean and well organized?

    Thanks!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      There's a gorgeous tutorial here on DevNet: http://developer.qt.nokia.com/doc/qt-4.7/qtestlib-tutorial1.html Helped me a lot.

      About keeping it separate - most probably, that is a good path to take. Tests are not something an end used needs, they are for developers and people who compile from source. In Qt, I think the best way is to create a "subdir" qmake profile in your app. It will keep everything together in Qt Creator (one file to open the whole project), while giving you flexibility and separation of test cases from actual code. If you need examples/ tutorials, read qmake documentation, or look at live projects, like Qt itself ("link":https://qt.gitorious.org/qt/qtbase), or if you want a simpler example, you can look at my QtWebService ("link":https://gitorious.org/qwebservice).

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        szuzsq
        wrote on last edited by
        #3

        I am using "cppunit-1.12.1 + qxrunner.0.9.2" for unit testing.
        it's very good.
        it provide a window, and you can choose which to test in the treeview like as below:

        @
        CA_TestCase
        test1
        test2
        CB_TestCase
        test1
        test2
        test3
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          szuzsq
          wrote on last edited by
          #4

          @
          core/src/core.pro
          A.h
          B.h
          A.cpp
          B.cpp


          core/src/test/test.pro
          A_TestCase.h
          B_TestCase.h
          A_TestCase.cpp
          B_TestCase.cpp
          @
          CA_TestCase is just to test CA.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            esdrasbeleza
            wrote on last edited by
            #5

            Thanks folks, it's just what I need.

            I created a separated project ("tests") inside my project based on subdir template, and inside "tests" I put my test projects.

            Just one more question: is there a easy way to run all tests when I run the "tests" project? The only way I thought was creating a script myself to run all binaries created inside the build folder.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              You might want to have a look at how Qt handles the tests.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                [quote author="Esdras Beleza" date="1320203797"]Thanks folks, it's just what I need.

                I created a separated project ("tests") inside my project based on subdir template, and inside "tests" I put my test projects.

                Just one more question: is there a easy way to run all tests when I run the "tests" project? The only way I thought was creating a script myself to run all binaries created inside the build folder.[/quote]

                You're welcome. Franzk's tip is good, Qt does have tests that run automatically, but I do not know how they are making it happen. You can try to analyze the source, but it's complicated, or search the docs, or ask on "interest" mailing list (http://lists.qt-project.org/mailman/listinfo).

                (Z(:^

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Franzk
                  wrote on last edited by
                  #8

                  There's also the option of not using the "one app per test" approach. There is always the possibility of running through a list of registered tests in a main loop. This allows you to build all your tests into one program. QTestLib isn't fully geared towards this approach and you'll have to work your way around some limitations. In some places (particularly command line handling) QTestLib assumes there's going to be only one test to be run, and it will bail out as soon as an error condition is reached.

                  "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sfabry
                    wrote on last edited by
                    #9

                    If anybody else find this post, I found nice solution:

                    Mark all tests with CONFIG+=testcase in the .pro file
                    then invoke make check' to run them all. (make -k check' if you want to keep running even if some of them fail)

                    This can be configured as custom executable target in qtcreator to run all tests from qtcreator directly.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved