Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Getting GTest unit tests to integrate correctly with Qt Creator
QtWS25 Last Chance

Getting GTest unit tests to integrate correctly with Qt Creator

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
7 Posts 3 Posters 4.7k 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.
  • GarryJG Offline
    GarryJG Offline
    GarryJ
    wrote on last edited by
    #1

    Had a quick browse around the forums and couldn't find this particular issue, so here's the lowdown; we have:

    • Ubuntu
    • Qt Creator 4.7.0
    • A CMake project (using CMake 3.8)
    • Googletest (GTest) unit test binaries

    Qt Creator detects the unit tests correctly after opening the project, and shows them in the test pane. But, trying to run them gives the following error:

    FATAL: Test for project "the-project-name" failed to produce any expected output."
    

    When running the test binaries using the run button, the expected GTest output is shown in the application output window. Similarly, when running the unit test binary outside of Qt in a shell, the output is shown as expected.

    I've had a quick look at the Qt Creator source - the error comes from line 254 in plugins/autotest/testrunner.cpp, but it's not immediately clear from the source exactly how the mechanism works; i.e., how does Qt Creator parse and validate the output from the unit tests. As such, I can't tell if it's a bug, or if my setup is somehow incorrect.

    Any ideas?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      From a quicklook, you should check the GTestOutputReader class. There might be something that has changed with the version of GTest you are using.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • GarryJG Offline
        GarryJG Offline
        GarryJ
        wrote on last edited by
        #3

        Thanks for the tip - after building Qt Creator from source and inserting some debug statements, I found the issue; it was because I had GTEST_COLOR=1 set in my environment.

        Explanation, just in case anyone else has this problem in future:

        GTEST_COLOR is an environment variable you can set so that test output in a shell is coloured according to the test result.

        When GTEST_COLOR is not set, Qt Creator sees and processes test output like this (in this case, in GTestOutputReader::processOutput():

        outputLine = "[==========] Running 1 test from 1 test case." 
        

        If, however, GTEST_COLOR=1 is set, Qt Creator sees the colour formatting, which it can't parse:

        outputLine = "\x1B[m\x1B[0;32m[==========] \x1B[mRunning 1 test from 1 test case."
        

        It'd be nice if Qt Creator could parse this, as there are cases where Qt Creator needs to be run from a shell where it's handy to also be able to run the unit tests standalone (in our case, it's a cross-compilation environment using Yocto), so I'll make a suggestion on the Qt Creator JIRA.

        aha_1980A 1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Good catch !

          Don't forget to post the link to the suggestion.

          It would also be nice to add an example of test output with and without GTEST_COLOR set to the report.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • GarryJG GarryJ

            Thanks for the tip - after building Qt Creator from source and inserting some debug statements, I found the issue; it was because I had GTEST_COLOR=1 set in my environment.

            Explanation, just in case anyone else has this problem in future:

            GTEST_COLOR is an environment variable you can set so that test output in a shell is coloured according to the test result.

            When GTEST_COLOR is not set, Qt Creator sees and processes test output like this (in this case, in GTestOutputReader::processOutput():

            outputLine = "[==========] Running 1 test from 1 test case." 
            

            If, however, GTEST_COLOR=1 is set, Qt Creator sees the colour formatting, which it can't parse:

            outputLine = "\x1B[m\x1B[0;32m[==========] \x1B[mRunning 1 test from 1 test case."
            

            It'd be nice if Qt Creator could parse this, as there are cases where Qt Creator needs to be run from a shell where it's handy to also be able to run the unit tests standalone (in our case, it's a cross-compilation environment using Yocto), so I'll make a suggestion on the Qt Creator JIRA.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @GarryJ

            The fastest solution I could imagine would be to un-set the environment variable within Creator, e.g. in the build environment.

            I don't know if the colored test output is visible in Creator, if not clearing the variable would also be the easiest fix in code.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            1
            • GarryJG Offline
              GarryJG Offline
              GarryJ
              wrote on last edited by GarryJ
              #6

              @aha_1980

              Yeah, that's one option!

              It's not ideal in my particular case, though, because of the way Qt Creator works with CMake projects (it creates CMakeLists.txt.user on the fly). In the situation where you have to detect CMake and the compiler from the environment variables (because, for example, you have multiple targets requiring different toolchains), the CMakeLists.user.text file can get messy with Imported Kits pretty quickly, so I often find myself having to delete the file so I can keep track of which Kits are which. Deleting the file would, of course, remove any project-specific environment settings. It'd be real nice if Qt Creator had a command line interface which would allow adding a Kit, but for now detection from the environment is the easiest way.

              The option I went for is to avoid setting the environment variable completely, and then specify the --gtest_color=yes option when adding a test in CMake:

              add_executable(unittest-binary
                    test-source.cpp
                    )
              
              add_test(
                  NAME unittest-name
                  COMMAND unittest-binary --gtest_color=yes
                  )
              

              This way, running tests via the shell using ctest outputs in colour.

              1 Reply Last reply
              2
              • GarryJG Offline
                GarryJG Offline
                GarryJ
                wrote on last edited by
                #7

                For posterity: the JIRA issue is here: https://bugreports.qt.io/browse/QTCREATORBUG-21012.

                1 Reply Last reply
                2

                • Login

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