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. skip a method call when unit testing
Forum Update on Monday, May 27th 2025

skip a method call when unit testing

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.6k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    hi,
    in my pro file i have this

    CONFIG += test
    
    test{
        message(Configuring TEST build...)
    
        TEMPLATE = app
        TARGET = myapptests
        SOURCES -= main.cpp
        QT += testlib
    
        HEADERS += \
            src/tests/test_machinebackend.h
    
        SOURCES += \
            src/tests/test_machinebackend.cpp
    
    }
    else{
         message(Configuring APP build...)
    
        TEMPLATE = app
        TARGET = myapp
        QT.testlib.CONFIG -= console
    }
    

    is there a way to know (in c++) if its is test build or a normal build ?
    i want to skip some methods if it is a test build.

      if(!/*check CONFIG += test  ?*/){
            uaSub(m_br->cNode, m_br->cNodeId, &Broche::cUpdated);
    

    thanks

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can try to add a define with 'DEFINES += BUILD_TESTING'

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

      ODБOïO 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        You can try to add a define with 'DEFINES += BUILD_TESTING'

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @Christian-Ehrlicher awsome !
        just learned that one.
        thank you very much!
        i can do :

        #pro
        CONFIG += c++11 test
        DEFINES += BUILD_TESTING
        
        //cpp
         if(!BUILD_TESTING){
          uaSub(m_br->cNode, m_br->cNodeId, &Broche::cUpdated);
        }
        
        Pablo J. RoginaP 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LeLev said in skip a method call when unit testing:

          if(!BUILD_TESTING){

          it's a define, no c++ command

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

          ODБOïO 1 Reply Last reply
          1
          • ODБOïO ODБOï

            @Christian-Ehrlicher awsome !
            just learned that one.
            thank you very much!
            i can do :

            #pro
            CONFIG += c++11 test
            DEFINES += BUILD_TESTING
            
            //cpp
             if(!BUILD_TESTING){
              uaSub(m_br->cNode, m_br->cNodeId, &Broche::cUpdated);
            }
            
            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @LeLev said in skip a method call when unit testing:

            //cpp
            if(!BUILD_TESTING){

            In addition to what @Christian-Ehrlicher pointed out regarding #define, I'm curious about why you'll be skipping some code snippets when testing?

            I guess that the code under testing should be the same as when no testing is carried on, and if you need something "special" is up to the test environment to setup such scenario.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @LeLev said in skip a method call when unit testing:

              if(!BUILD_TESTING){

              it's a define, no c++ command

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by ODБOï
              #6

              @Christian-Ehrlicher
              "it's a define, no c++ command"
              yes, i know that, im not sure why you insist, can you explain please?
              i managed to do what i needed thanks to you but what @Pablo-J-Rogina said, gave me an idea

              @Pablo-J-Rogina said in skip a method call when unit testing:

              why you'll be skipping some code snippets when testing?

              im createing HMIs for CNC machins. Currently i'm working for a machine that doas not exist yet, i know it will have an opc ua server, so i have prepared lot of server request stuff, subscriptions, based on my experience, but for now i only have a empty server so i will skip all that things.

              @Pablo-J-Rogina said in skip a method call when unit testing:

              ou need something "special" is up to the test environment to setup such scenario.

              Its true I should have thought about it. thank you

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

                Hi,

                Don't skip anything, use a mock to simulate what you don't have so you can already make your application properly work and you'll only have to write some adaptation once the machine arrives.

                There's a Qt World Summit talk about developing for invisible hardware that does ring like the situation you have.

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

                ODБOïO 1 Reply Last reply
                3
                • SGaistS SGaist

                  Hi,

                  Don't skip anything, use a mock to simulate what you don't have so you can already make your application properly work and you'll only have to write some adaptation once the machine arrives.

                  There's a Qt World Summit talk about developing for invisible hardware that does ring like the situation you have.

                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by ODБOï
                  #8

                  hi,
                  @SGaist said in skip a method call when unit testing:

                  use a mock

                  Can you suggest one good mocking framework
                  FakeIt ?
                  does not seem to integrate nicely with qt, because :

                  if i replace SomeInterface by a QObject derived class with the same virtual methods, i have tons of errors

                  struct SomeInterface {
                  	virtual int foo(int) = 0;
                  	virtual int bar(string) = 0;
                  };
                  // Instantiate a mock object.
                  Mock<SomeInterface> mock;
                  
                  // Setup mock behavior.
                  When(Method(mock,foo)).Return(1); // Method mock.foo will return 1 once.
                  
                  // Fetch the mock instance.
                  SomeInterface &i = mock.get();
                  
                  // Will print "1". 
                  cout << i.foo(0);
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It depends on what you want/need to mock.

                    If for example you need to connect to a mock server you can already write it with Qt.

                    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
                    0
                    • ODБOïO ODБOï

                      @Christian-Ehrlicher
                      "it's a define, no c++ command"
                      yes, i know that, im not sure why you insist, can you explain please?
                      i managed to do what i needed thanks to you but what @Pablo-J-Rogina said, gave me an idea

                      @Pablo-J-Rogina said in skip a method call when unit testing:

                      why you'll be skipping some code snippets when testing?

                      im createing HMIs for CNC machins. Currently i'm working for a machine that doas not exist yet, i know it will have an opc ua server, so i have prepared lot of server request stuff, subscriptions, based on my experience, but for now i only have a empty server so i will skip all that things.

                      @Pablo-J-Rogina said in skip a method call when unit testing:

                      ou need something "special" is up to the test environment to setup such scenario.

                      Its true I should have thought about it. thank you

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @LeLev said in skip a method call when unit testing:

                      yes, i know that, im not sure why you insist, can you explain please?

                      Because your code is not going to work, it needs to be

                      #ifndef BUILD_TESTING
                      // Do what needs to be done if NOT testing
                      #endif
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3

                      • Login

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