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. QTest - doesn't compile after adding second round/class of tests.
QtWS25 Last Chance

QTest - doesn't compile after adding second round/class of tests.

Scheduled Pinned Locked Moved Solved General and Desktop
qtestqtestlib
6 Posts 2 Posters 326 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.
  • A Offline
    A Offline
    artwaw
    wrote on 16 Jul 2024, 11:45 last edited by
    #1

    Good afternoon,
    I decided to learn something new recently - QTest. I wrote tests for one of my classes and it did the job, allowed to find some otherwise hard to spot problems, so when I wrote another complex thing I decided to write tests for that too. But once it was ready to compile I got an error. No doubt I did something silly because I misunderstood the docs, but I am puzzled.

    Please bear in mind that it worked flawlessly until I added second test.

    Structure:

    • I have a lib called core where the classes to be tested come from;
    • original, working test is for the class ConfigFile. Test is in tst_configfile.cpp
    • additional one is for the class HistoryClass, test in tst_historyclass.cpp

    The error I get is:

    suplicate symbol '_main' in:
        [...]Tests.dir/core/tst_historyclass.cpp.o
        [...]Tests.dir/core/tst_configfile.cpp.o
    ld: 1 duplicate symbols
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.
    

    Excerpt from the CMakeLists:

    set(includes
        "../src/core/"
    )
    
    set(SOURCES
    	core/tst_configfile.cpp
            core/tst_historyclass.cpp
    )
    
    add_executable(GemBrowserTests ${SOURCES} )
    target_include_directories(GemBrowserTests PRIVATE "core")
    target_include_directories(GemBrowserTests PRIVATE ${includes})
    
    add_test(NAME ConfigFile COMMAND ConfigFile)
    add_test(NAME HistoryClass COMMAND HistoryClass)
    

    and includes follow, those didn't even change.
    What I don't understand here?

    As usual, many thanks in advance.

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 Jul 2024, 14:01 last edited by
      #4

      You can add as many add_executable() to a CMakeLists.txt as you want.
      Qt is using one test executable per class which I also consider as a good practice for unit tests.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 16 Jul 2024, 12:37 last edited by Christian Ehrlicher
        #2

        You can only have one QTest main, simply create two executables

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        A 1 Reply Last reply 16 Jul 2024, 13:48
        1
        • C Christian Ehrlicher
          16 Jul 2024, 12:37

          You can only have one QTest main, simply create two executables

          A Offline
          A Offline
          artwaw
          wrote on 16 Jul 2024, 13:48 last edited by
          #3

          @Christian-Ehrlicher stupid question: can I do that in the same CMake project, by means of another add_executable()?

          Less silly question: what is considered a good practice in my case, where I want to test multiple classes? How do I construct tests for them? Separate tree of projects/CMakeLists.txt files?

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 16 Jul 2024, 14:01 last edited by
            #4

            You can add as many add_executable() to a CMakeLists.txt as you want.
            Qt is using one test executable per class which I also consider as a good practice for unit tests.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • A Offline
              A Offline
              artwaw
              wrote on 16 Jul 2024, 14:02 last edited by
              #5

              Awesome @Christian-Ehrlicher - I shall proceed in that direction. Thank you!

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • A artwaw has marked this topic as solved on 16 Jul 2024, 14:02
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 16 Jul 2024, 14:04 last edited by
                #6

                You should also take a look on add_test() - https://cmake.org/cmake/help/latest/command/add_test.html - to execute e.g. all tests at once.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1

                6/6

                16 Jul 2024, 14:04

                • Login

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