Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何测试机器?_Qt_Qstatemachine - Fatal编程技术网

Qt 如何测试机器?

Qt 如何测试机器?,qt,qstatemachine,Qt,Qstatemachine,我对如何测试机器有点困惑。 我有一个组织良好的项目,一边是源代码,另一边是测试代码 标题 class Foo { signals: void sigGoToStateOne(); void sigGoToStateTwo(); void sigGoToStateThree(); private: QStateMachine *stateMachine; QState *state1;

我对如何测试机器有点困惑。 我有一个组织良好的项目,一边是源代码,另一边是测试代码

标题

class Foo
{
    signals:
        void sigGoToStateOne();
        void sigGoToStateTwo();
        void sigGoToStateThree();

    private:
        QStateMachine *stateMachine;
        QState *state1;
        QState *state2;

        void initStateMachine();
}
和在源文件中

Foo::initStateMachine()
{
    // constructors
    state1->addTransition(this,SIGNAL(sigGoToStateTwo()),this->state2);
    state2->addTransition(this,SIGNAL(sigGoToStateOne()),this->state1);
}
我想知道是否有一个漂亮的方法来测试我的状态机是否正确。换句话说,如果我在那里发出siggotostatetree(),我的状态机将如何反应,等等

我看到的解决方案: 1-获取stateMachine的地址(以及最终所有其他状态)并测试它(但我不知道如何) 2-模拟测试文件中的信号(sigGoToStateX())(同样,不知道是否可能在另一个类中发出我的类Foo的信号)

我唯一的要求是我不想修改源文件的核心


提前感谢。

在Qt5中,信号始终是公共方法。为了使您的代码与Qt 4兼容,您可以明确公开信号,如下所示:

class Foo {
public:
  Q_SIGNAL void sigGoToStateOne();
  ...
}
或者,您可以保持任意信号可见性,并声明友元测试类:

class Foo {
  friend class FooTest;
  ...
}
最后,您可以创建一个测试项目,使用Qt的测试框架测试
Foo
类的行为。下面的代码在Qt4和Qt5中都起作用

// main.cpp
#include <QCoreApplication>
#include <QStateMachine>
#include <QEventLoop>
#include <QtTest>
#include <QTimer>

class Waiter {
   QTimer m_timer;
public:
   Waiter() {}
   Waiter(QObject * obj, const char * signal) {
      m_timer.connect(obj, signal, SIGNAL(timeout()));
   }
   void stop() {
      m_timer.stop();
      QMetaObject::invokeMethod(&m_timer, "timeout");
   }
   void wait(int timeout = 5000) {
      QEventLoop loop;
      m_timer.start(timeout);
      loop.connect(&m_timer, SIGNAL(timeout()), SLOT(quit()));
      loop.exec();
   }
};

class SignalWaiter : public QObject, public Waiter {
   Q_OBJECT
   int m_count;
   Q_SLOT void triggered() {
      ++ m_count;
      stop();
   }
public:
   SignalWaiter(QObject * obj, const char * signal) : m_count(0) {
      connect(obj, signal, SLOT(triggered()), Qt::QueuedConnection);
   }
   int count() const { return m_count; }
};

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
typedef QSignalSpy SignalSpy;
#else
class SignalSpy : public QSignalSpy, public Waiter {
public:
   SignalSpy(QObject * obj, const char * signal) :
      QSignalSpy(obj, signal), Waiter(obj, signal) {}
};
#endif

class Foo : public QObject {
   Q_OBJECT
   friend class FooTest;
   QStateMachine m_stateMachine;
   QState m_state1;
   QState m_state2;
   Q_SIGNAL void sigGoToStateOne();
   Q_SIGNAL void sigGoToStateTwo();
public:
   explicit Foo(QObject * parent = 0) :
      QObject(parent),
      m_state1(&m_stateMachine),
      m_state2(&m_stateMachine)
   {
      m_stateMachine.setInitialState(&m_state1);
      m_state1.addTransition(this, SIGNAL(sigGoToStateTwo()), &m_state2);
      m_state2.addTransition(this, SIGNAL(sigGoToStateOne()), &m_state1);
   }
   Q_SLOT void start() {
      m_stateMachine.start();
   }
};

class FooTest : public QObject {
   Q_OBJECT
   void call(QObject * obj, const char * method) {
      QMetaObject::invokeMethod(obj, method, Qt::QueuedConnection);
   }
   Q_SLOT void test1() {
      // Uses QSignalSpy
      Foo foo;
      SignalSpy state1(&foo.m_state1, SIGNAL(entered()));
      SignalSpy state2(&foo.m_state2, SIGNAL(entered()));
      call(&foo, "start");
      state1.wait();
      QCOMPARE(state1.count(), 1);
      call(&foo, "sigGoToStateTwo");
      state2.wait();
      QCOMPARE(state2.count(), 1);
      call(&foo, "sigGoToStateOne");
      state1.wait();
      QCOMPARE(state1.count(), 2);
   }

   Q_SLOT void test2() {
      // Uses SignalWaiter
      Foo foo;
      SignalWaiter state1(&foo.m_state1, SIGNAL(entered()));
      SignalWaiter state2(&foo.m_state2, SIGNAL(entered()));
      foo.start();
      state1.wait();
      QCOMPARE(state1.count(), 1);
      emit foo.sigGoToStateTwo();
      state2.wait();
      QCOMPARE(state2.count(), 1);
      emit foo.sigGoToStateOne();
      state1.wait();
      QCOMPARE(state1.count(), 2);
   }
};

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   FooTest test;
   QTest::qExec(&test, a.arguments());
   QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
   return a.exec();
}

#include "main.moc"

这可以通过使用内部使用排队连接的signal spy类来解决,而无需使用显式排队调用。

Kuba Ober非常好地分析了如何使用测试框架&SignalSpy对状态机进行深入测试

如果您要做的只是从测试文件生成一个sigGoToStateX(),那么不要忘记您可以将信号链接在一起

例如,给定一个类“Tester”:

例如,调用SwitchState(1)将调用正确的信号来切换到状态1。如果这个简单的案例就是测试所需的全部,那么这就是您真正需要的全部


如果需要更复杂的东西,请使用完整的StaleScript示例。

信号是完全常规的C++方法。根本没有理由去经历那些间接的圈套。将
emit testtransitionstate1
替换为
emit foo->sigGoToState1
。只要确保你的信号是公开的,如果你的目标是Qt4(
Q\u信号
在公开部分)。这仍然没有显示如何实际测试任何东西。你所做的就是调用信号。那是微不足道的。您还没有演示如何确保信号做任何事情。是的,但我假设他试图根据我们在现阶段不知道的任意外部架构调用信号,所以这个示例只是用来演示这与信号/插槽连接相同,只是使用信号。我完全同意,这是一个微不足道的事情,不回答如何测试内部状态,但其中一个问题是如何从更广泛的架构观点调用来自FoO类外部的信号。RE 2:信号是常规C++方法。“发射”它们只是调用它们的方法。事实上,这没什么。
Q_SLOT void test1() {
   SignalSpy state1(&m_foo.m_state1, SIGNAL(entered()));
   SignalSpy state2(&m_foo.m_state2, SIGNAL(entered()));
   m_foo.start();
   state1.wait();
   QCOMPARE(state1.count(), 1);
   emit m_foo.sigGoToStateTwo(); // The state2.entered() signal is emitted here.
   state2.wait(); // But we wait for it here, and this wait will time out.
   QCOMPARE(state2.count(), 1); // But of course the count will match.
   emit m_foo.sigGoToStateOne();
   state1.wait(); // This would timeout as well.
   QCOMPARE(state1.count(), 2);
}
class Tester : public QObject {
Q_OBJECT
public:
    Tester(Foo *fooClass) {
        //Connecting signals gives you the kind of behaviour you were asking about
        connect(this, SIGNAL(testTransitionToState1()), fooClass, SIGNAL(sigGoToState1()));
        connect(this, SIGNAL(testTransitionToState2()), fooClass, SIGNAL(sigGoToState2()));
        connect(this, SIGNAL(testTransitionToState3()), fooClass, SIGNAL(sigGoToState3()));
    }

    void SwitchState(int newState) {
        //Now any time we emit the test signals, the foo class's signals will be emitted too!
        if (newState == 1) emit testTransitionToState1();
        else if (newState == 2) emit testTransitionToState1();
        else if (newState == 3) emit testTransitionToState1();
    }

signals:
    void testTransitionToState1();
    void testTransitionToState2();
    void testTransitionToState3();
}