skip a method call when unit testing
-
wrote on 3 Jan 2020, 11:34 last edited by ODБOï 1 Mar 2020, 11:34
hi,
in my pro file i have thisCONFIG += 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
-
You can try to add a define with 'DEFINES += BUILD_TESTING'
-
You can try to add a define with 'DEFINES += BUILD_TESTING'
wrote on 3 Jan 2020, 11:50 last edited by@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); }
-
@LeLev said in skip a method call when unit testing:
if(!BUILD_TESTING){
it's a define, no c++ command
-
@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); }
wrote on 3 Jan 2020, 13:51 last edited by@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.
-
@LeLev said in skip a method call when unit testing:
if(!BUILD_TESTING){
it's a define, no c++ command
wrote on 3 Jan 2020, 14:51 last edited by ODБOï 1 Mar 2020, 15:01@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
-
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.
-
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.
wrote on 6 Jan 2020, 08:29 last edited by ODБOï 1 Jun 2020, 10:23hi,
@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);
-
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.
-
@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
Lifetime Qt Championwrote on 7 Jan 2020, 07:42 last edited by jsulm 1 Jul 2020, 07:43@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
1/10