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. [Solved] Unit test failure notification
QtWS25 Last Chance

[Solved] Unit test failure notification

Scheduled Pinned Locked Moved Qt Creator and other tools
26 Posts 8 Posters 13.0k 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.
  • L Offline
    L Offline
    lyuts
    wrote on last edited by
    #4

    As far as I understand it is up to QtTestLib to decide whether to print to stderr or stdout. Do you redirect output in your script?

    I'm a rebel in the S.D.G.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      deanz
      wrote on last edited by
      #5

      Yes, I redirected the output text from the test script to stderr and Qt Creator then printed the text in red instead of black in the compile output pane but still nothing in Build Issues pane.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deanz
        wrote on last edited by
        #6

        Also, I'm not using QtTestLib; perhaps that is the problem?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #7

          deanz: Qt Creator parses the compile output to generate the build issues. It does not expect unit test results when building, so it does not parse them and thus nothing ends up in the build issues.

          Sorry, there is no workaround available that I am aware of. We want to add proper unit test integration, but so far nobody found the time to actually do it:-/

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deanz
            wrote on last edited by
            #8

            Thanks, Tobias.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #9

              Hmmm... thinking about this: With a bit of hacking you could get your test results displayed in the build issues:-)

              I added a TaskList plugin a while back which grabs a file in CSV format and puts it into the build issues plane. So maybe you can wrap your unit test runner into a script that turns the output into such an CSV file... Creator does watch the file and updates the build issue pane whenever the file changes.

              Here is my "announcement mail":http://lists.trolltech.com/pipermail/qt-creator/2010-August/007574.html with some information on the syntax accepted. I think this information is actually already in the documentation, but I am not sure where;-)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deanz
                wrote on last edited by
                #10

                Great!

                Thanks again, Tobias.

                I can easilly change my unit tester output to CSV format and your plug-in can take it from there. Looks like problem solved.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deanz
                  wrote on last edited by
                  #11

                  Tobias:

                  What is the name of your new plugin? How do I download and install it?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tobias.hunger
                    wrote on last edited by
                    #12

                    deanz: It is called TaskList and is included in Qt Creator 2.1 beta. Sorry, I should have mentioned that:-)

                    Oh, for now you need to open the .tasks file (once) via File->Open. When opening it any other way you get it displayed in an editor:-(

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      deanz
                      wrote on last edited by
                      #13

                      OK, thanks again, Tobias. I will wait for the 2.1 release but it sure sounds like a very useful addition to an already excellent product.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tobias.hunger
                        wrote on last edited by
                        #14

                        Yes, writing that plugin was 2h well spend;-)

                        Actually I only did it since I wanted to get our count of krazy (some static code analysis tool) issues down and was too lazy to hop to each place manually. Good to hear that it works for other use-cases as well;-)

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tbscope
                          wrote on last edited by
                          #15

                          bq. Actually I only did it since I wanted to get our count of krazy (some static code analysis tool) issues down

                          Ohh pooh :-(
                          Just when I'm about to finish my own Static Code Quality plugin :-)

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tobias.hunger
                            wrote on last edited by
                            #16

                            tbscope: Being able to hack up a script to import an arbitrary list of issues into Creator is not exactly a "static code quality plugin":-)

                            I am looking forward to see your plugin! I am sure it will be way more user friendly than my hack.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              markus.liebe
                              wrote on last edited by
                              #17

                              [quote author="Tobias Hunger" date="1287579812"]Actually I only did it since I wanted to get our count of krazy (some static code analysis tool) issues down and was too lazy to hop to each place manually. Good to hear that it works for other use-cases as well;-)[/quote]

                              Hi Tobias.
                              we plan to use your plugin to display all the tasks generated by a static code analysis tool.
                              It works great, there is just one thing left:
                              For each task there exists documentation in the form of a html file. We want to be able to somehow open the html file via some handy action (right click on the task->open documentation or sth.)

                              Do you think it would be possible to add such a functionality to your plugin?
                              Well perhaps it is not only your plugin that would have to be extended but also the BuildIssues Implementation...

                              Regards,
                              Markus

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tobias.hunger
                                wrote on last edited by
                                #18

                                Hi Markus!

                                There is no such code in creator yet.

                                We do have the concept of TaskHandlers though: Just create one that can accept your task and then displays the help if it is able to match the task to one of your HTML files.

                                This should be very easy to implement in a plugin. There should be no need to meddle with existing Qt Creator code for this.

                                Best Regards,
                                Tobias

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  markus.liebe
                                  wrote on last edited by
                                  #19

                                  Thanks for your response.
                                  I just finished a TaskHandler that does what I want =) Now the next step is to put this into a plugin.

                                  I have to say, I really like the QtCreator code. Each time I need some extra functionality it is very easy to do that. The codebase is very friendly towards extending it!

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    tobias.hunger
                                    wrote on last edited by
                                    #20

                                    Took you less than 11min to write that TaskHandler... I am impressed!

                                    Thanks for the compliments on our code. Positive feedback is always appreciated:-)

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      markus.liebe
                                      wrote on last edited by
                                      #21

                                      Well, I just used the CopyTaskHandler as a template and wrote 11 lines of code ;-)

                                      1 Reply Last reply
                                      0
                                      • danoD Offline
                                        danoD Offline
                                        dano
                                        wrote on last edited by
                                        #22

                                        Howdy folks,

                                        Great topic! Because I wanted to try out test driven development, I was looking for a convenient way to get UnitTest++ output into the QtCreator Build Issues pane. Luckily, the TaskHandler functionality has recently been added to QtCreator, so it could be done easier than I expected. Anyhow, I just wrote the QtTaskTestReporter class below. You can use it in a testinstantiator like this:

                                        @
                                        // --------------- testinstantiator.cpp ---------------
                                        #include "UnitTest++.h"
                                        #include "TestRunner.h"
                                        #include "QtTaskTestReporter.h"

                                        /* #include files containing your tests here */

                                        int main( int argc, const char* argv[])
                                        {
                                        UnitTest::QtTaskTestReporter reporter;
                                        UnitTest::TestRunner runner(reporter);
                                        runner.RunTestsIf(UnitTest::Test::GetTestList(), NULL, UnitTest::True(), 0);
                                        return 0;
                                        }
                                        @

                                        Just build the testinstantiator along wih your executable, and add running the testinstantiator executable as a Custom Process Step, after Make, under "Build Steps" in the "Projects -> Build Settings" pane. After Opening the generated .tasks file once with QtCreator, QtCreator will display UnitTest++ failures in the build issues pane during the entire QtCreator session. This enables swift, one-click navigation to the failing unit tests.

                                        I hope this is as useful to you as it is to me.

                                        Cheers,
                                        Daan

                                        @
                                        // --------------- QtTaskTestReporter.h ---------------
                                        /*

                                        • QtTaskTestReporter. Writes UnitTest++ output to a .task file.
                                        • Author: Daan Baas 2011 the.daanbaas@gmail.com
                                          */

                                        #ifndef QTTASKTESTREPORTER_H
                                        #define QTTASKTESTREPORTER_H

                                        #include <vector>
                                        #include <string>
                                        #include "TestReporter.h"
                                        #include "TestDetails.h"

                                        namespace UnitTest
                                        {
                                        class QtTaskTestReporter : public TestReporter
                                        {
                                        public:
                                        QtTaskTestReporter(const std::string& outputFileName = "unittest++.tasks");

                                            virtual void ReportTestStart(TestDetails const& test){ /* empty */ }
                                            virtual void ReportFailure(TestDetails const& test, char const* failure);
                                            virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed){ /* empty */ }
                                            virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed);
                                        
                                        private:
                                            struct TaskData
                                            {
                                                //mimics a task from QtCreator's project explorer plugin
                                                //legal values for type are: "Unknown", "Error" and "Warning"
                                                std::string fileName;
                                                std::string lineNumber;
                                                std::string type;
                                                std::string description;
                                            };
                                        
                                            std::string taskToTabSeparatedString(const TaskData& iask) const;
                                            std::string backSlashesToForwardSlashes(const std::string& inputStr) const;
                                            std::string intToString(int number) const;
                                            void writeTasksToOutputFile&#40;&#41; const;
                                        
                                            typedef std::vector< TaskData > TaskList;
                                            TaskList m_tasks;
                                            const std::string m_outFileName;
                                        };
                                        

                                        }//end namespace UnitTest
                                        #endif // QTTASKTESTREPORTER_H

                                        //--------------- QtTaskTestReporter.cpp ---------------

                                        /*

                                        • QtTaskTestReporter. Writes UnitTest++ output to a .task file.
                                        • Author: Daan Baas 2011 the.daanbaas@gmail.com
                                          */

                                        #include "qttasktestreporter.h"
                                        #include <stdexcept>
                                        #include <iostream>
                                        #include <fstream>
                                        #include <sstream>
                                        #include <algorithm>

                                        namespace UnitTest
                                        {
                                        QtTaskTestReporter::QtTaskTestReporter(const std::string& outputFileName)
                                        :
                                        TestReporter(),
                                        m_outFileName(outputFileName)
                                        {
                                        //note: you can choose your own output filename, but make sure it has the
                                        //".tasks" extension so it can be picked up by the QtCreator task handler
                                        }

                                        void QtTaskTestReporter::ReportFailure(TestDetails const& test, char const* failure)
                                        {
                                            TaskData failTask;
                                        
                                            failTask.fileName = backSlashesToForwardSlashes(test.filename);
                                            failTask.lineNumber = intToString(test.lineNumber);
                                            failTask.type = "Error";
                                        
                                            std::stringstream failureMessage;
                                            failureMessage <<  "error: Failure in " << test.testName << ": " << failure;
                                            failTask.description = failureMessage.str() ;
                                        
                                            m_tasks.push_back(failTask);         
                                        }
                                        
                                        
                                        void QtTaskTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount,
                                                                               float secondsElapsed)
                                        {
                                            writeTasksToOutputFile&#40;&#41;;
                                        }
                                        
                                        
                                        std::string QtTaskTestReporter::taskToTabSeparatedString(const TaskData& task) const
                                        {
                                            std::string result;
                                            result = task.fileName + '\t' + task.lineNumber + '\t' + task.type + '\t' + task.description;
                                            return result;
                                        }
                                        
                                        
                                        std::string QtTaskTestReporter::backSlashesToForwardSlashes(const std::string& inputStr) const
                                        {
                                            using namespace std;
                                            string result = inputStr;
                                            replace(result.begin(), result.end(), '\\', '/');
                                            return result;
                                        }
                                        
                                        
                                        std::string QtTaskTestReporter::intToString(int number) const
                                        {
                                            std::stringstream ss;
                                            ss << number;
                                            return ss.str();
                                        }
                                        
                                        void QtTaskTestReporter::writeTasksToOutputFile&#40;&#41; const
                                        {
                                            using namespace std;
                                            ofstream file&#40;m_outFileName.c_str(&#41;);
                                            if(!file)
                                            {
                                                cerr << "error: QtTaskTestReporter::ReportTestFinish() cannot open output file";
                                                return;
                                            }
                                        
                                            TaskList::const_iterator pos;
                                            for(pos = m_tasks.begin(); pos != m_tasks.end(); ++pos)
                                            {
                                                file << taskToTabSeparatedString(*pos) << std::endl;
                                            }
                                        
                                            file.close();
                                        }
                                        

                                        }//end namespace UnitTest
                                        @

                                        1 Reply Last reply
                                        0
                                        • E Offline
                                          E Offline
                                          EricSmith
                                          wrote on last edited by
                                          #23

                                          I've started working on a test runner for Google Test kind of like dano's (thanks for the example!). The one thing I'm not happy about, however, is the need to manually open the .tasks file every time you restart the IDE. It doesn't seem to stick with the session or anything to automatically open the next time you run.

                                          Anybody have any suggestions on improving that situation?

                                          Thanks,
                                          Eric

                                          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