【问题标题】:Unit-tests in the subdir projects are not built [Qt/C++]未构建子目录项目中的单元测试 [Qt/C++]
【发布时间】:2023-04-09 17:44:02
【问题描述】:

我在 Qt/C++ 中有一个带有子项目的项目,我尝试通过单元测试运行该项目:

但是当我尝试构建项目时,出现以下错误:

Undefined symbols for architecture x86_64:
  "vtable for MainWindow", referenced from:
      MainWindow::MainWindow(QWidget*) in tests.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for PaintScene", referenced from:
      PaintScene::PaintScene(QObject*) in tests.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [tests] Error 1

但是所有的构造函数都被声明和定义了。

paintscene:

PaintScene(QObject *parent = nullptr);

PaintScene::PaintScene(QObject *parent) : QGraphicsScene(parent)
{
    /***/
}

mainwindow:

MainWindow(QWidget *parent = nullptr);

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    /***/
}

tests.cpp:

#include <QtTest>
#include <QCoreApplication>
#include </Users/garbart/Desktop/paint/paint/mainwindow.cpp>
#include </Users/garbart/Desktop/paint/paint/paintscene.cpp>

class UnitTests : public QObject
{
    Q_OBJECT
private slots:
    void testWidth();
    void testColor();
    void testScene();
};

void UnitTests::testWidth()
{
    PaintScene *scene = new PaintScene();
    scene->setWidth(1000);
    QCOMPARE(scene->_width, 10);
    scene->setWidth(100);
    QCOMPARE(scene->_width, 100);
    scene->setWidth(-10);
    QCOMPARE(scene->_width, 100);
}

void UnitTests::testColor()
{
    QColor qc = Qt::red;
    PaintScene *scene = new PaintScene();
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::red);

    qc = Qt::green;
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::green);

    qc = Qt::black;
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::black);
}

void UnitTests::testScene()
{
    MainWindow *window = new MainWindow();

    QColor test_color = window->grab(QRect(window->rect().x() + 500, window->rect().y() + 500, 1, 1)).toImage().pixelColor(0,0);
    QCOMPARE(test_color, Qt::white);
}

QTEST_MAIN(UnitTests)

#include "tests.moc"

【问题讨论】:

  • 问题可能在于单元测试项目没有拾取符号。单元测试是一个单独的可执行文件,对吧?那么它有自己的 main.cpp 并且它的项目应该自己引用其他文件?我也怀疑单元测试是否需要包含应用程序的主窗口。

标签: c++ qt unit-testing


【解决方案1】:

它看起来像一个经典的链接器错误(如果您不向我们展示您的 mainwindow.h 头文件,我们无法 100% 确定),如果您想在测试项目中使用它,您应该导出您的 MainWindow 类(在您的绘画项目之外)。

How to export a C++ class from a dll?

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多