在QT创建者中为库创建(自动化)测试

发布于 2025-02-13 16:17:08 字数 3733 浏览 1 评论 0原文

我使用QMake创建了QT创建者的项目,这是一个共享库。现在,对于该库,我想创建一些(自动化的)测试,可以运行,以确保代码可以正常工作。

我遇到了Google Test软件包,看起来我应该和应该使用。 但是,我只找到了在cmake项目中使用它的说明,但我不使用。

项目结构看起来像这样(因为这是一个示例,该项目被命名为示例):

Example/
----> src/
--------> Example.cpp
----> include/
--------> Example.hpp
--------> Example_global.hpp
----> tests/
--------> src/
------------> // Tests (Test files) should go here

每个文件的内容。

以下

# This is the .pro file for the project/library Example

QT -= gui

TEMPLATE = lib
DEFINES += EXAMPLE_LIBRARY

CONFIG += c++20

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SRC_DIR     = $$PWD/src
HEADER_DIR  = $$PWD/include

INCLUDEPATH += $$SRC_DIR $$HEADER_DIR

SOURCES += \
    $$SRC_DIR/Example.cpp
    # Visual studio added the test files in here :/

HEADERS += \
    $$HEADER_DIR/Example_global.hpp \
    $$HEADER_DIR/Example.hpp

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

# Later, if you want to use the library, you would include this .pri file

DEFINES += EXAMPLE_LIBRARY

SRC_DIR     = $$PWD/src
HEADER_DIR  = $$PWD/include

INCLUDEPATH += $$SRC_DIR $$HEADER_DIR
DEPENDPATH += $$SRC_DIR $$HEADER_DIR

SOURCES += \
    $$SRC_DIR/Example.cpp

HEADERS += \
    $$HEADER_DIR/Example_global.hpp \
    $$HEADER_DIR/Example.hpp

example/src/example.cpp:

#include "Example.hpp"

EXAMPLE_BEGIN_NAMESPACE

Example::Example()
{
}

EXAMPLE_END_NAMESPACE

example/src/includ/example.hpp:

#pragma once

#include "Example_global.hpp"

EXAMPLE_BEGIN_NAMESPACE

class EXAMPLE_EXPORT Example
{
public:
    Example();
};

EXAMPLE_END_NAMESPACE

example/xudlage/example_global.hpp:

#pragma once

#include <QtCore/qglobal.h>

#if defined(EXAMPLE_LIBRARY)
#  define EXAMPLE_EXPORT Q_DECL_EXPORT
#else
#  define EXAMPLE_EXPORT Q_DECL_IMPORT
#endif

#ifdef EXAMPLE_NAMESPACE

# define EXAMPLE_PREPEND_NAMESPACE(name) ::EXAMPLE_NAMESPACE::name
# define EXAMPLE_USE_NAMESPACE using namespace ::EXAMPLE_NAMESPACE;
# define EXAMPLE_BEGIN_NAMESPACE namespace EXAMPLE_NAMESPACE {
# define EXAMPLE_END_NAMESPACE }
# define EXAMPLE_FORWARD_DECLARE_CLASS(name) \
    EXAMPLE_BEGIN_NAMESPACE class name; EXAMPLE_END_NAMESPACE \
    using EXAMPLE_PREPEND_NAMESPACE(name);

#else

# define EXAMPLE_PREPEND_NAMESPACE(name) ::name
# define EXAMPLE_USE_NAMESPACE
# define EXAMPLE_BEGIN_NAMESPACE
# define EXAMPLE_END_NAMESPACE
# define EXAMPLE_FORWARD_DECLARE_CLASS(name) class name;

#endif // EXAMPLE_NAMESPACE

example/tests/src/testexample:

// Here, I would want to do something like this:

#include <gtest/gtest.h>

int main(int argc, char* argv[])
{
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

我如何编写这些测试,尤其是如何运行,我将如何运行他们在QT创建者中?我在哪里告诉QT,我想添加Google Test软件包/库?

我尝试的是使用Visual Studio和一个称为QT Visual Studio Tools的插件将我的QT项目导入VS。然后,我安装了Nuget软件包Google Test。我能够创建测试,但是问题是,VS vs将所有测试文件(文件路径)添加到我的xpemens.pro文件'sources list。

afaik,这将在我的示例库中包括所有测试文件,当我构建它时,我认为这不是一个好主意。

那么,是否有一种“更好”的方式?我应该使用cmake并添加一些cmakelists.txt文件吗?如果是这样,他们应该去哪里,应该包含什么?

感谢您的帮助!

(我还应该说,我是一个菜鸟,也是一个菜鸟,在C ++中创建库)

I have created a project in QT Creator with qmake, that is a shared library. Now, for that library, I want to create some (automated) tests, that I can run, to assure that the code works, as it should.

I came across the Google Test package, which looks like what I could and should use.
However, I only found instructions for using it in CMake projects, which I don't use.

The project structure looks like this (As this is an example, the project is named Example):

Example/
----> src/
--------> Example.cpp
----> include/
--------> Example.hpp
--------> Example_global.hpp
----> tests/
--------> src/
------------> // Tests (Test files) should go here

Here are the contents of each file with their path:

Example/Example.pro:

# This is the .pro file for the project/library Example

QT -= gui

TEMPLATE = lib
DEFINES += EXAMPLE_LIBRARY

CONFIG += c++20

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SRC_DIR     = $PWD/src
HEADER_DIR  = $PWD/include

INCLUDEPATH += $SRC_DIR $HEADER_DIR

SOURCES += \
    $SRC_DIR/Example.cpp
    # Visual studio added the test files in here :/

HEADERS += \
    $HEADER_DIR/Example_global.hpp \
    $HEADER_DIR/Example.hpp

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

Example/Example.pri:

# Later, if you want to use the library, you would include this .pri file

DEFINES += EXAMPLE_LIBRARY

SRC_DIR     = $PWD/src
HEADER_DIR  = $PWD/include

INCLUDEPATH += $SRC_DIR $HEADER_DIR
DEPENDPATH += $SRC_DIR $HEADER_DIR

SOURCES += \
    $SRC_DIR/Example.cpp

HEADERS += \
    $HEADER_DIR/Example_global.hpp \
    $HEADER_DIR/Example.hpp

Example/src/Example.cpp:

#include "Example.hpp"

EXAMPLE_BEGIN_NAMESPACE

Example::Example()
{
}

EXAMPLE_END_NAMESPACE

Example/src/include/Example.hpp:

#pragma once

#include "Example_global.hpp"

EXAMPLE_BEGIN_NAMESPACE

class EXAMPLE_EXPORT Example
{
public:
    Example();
};

EXAMPLE_END_NAMESPACE

Example/include/Example_global.hpp:

#pragma once

#include <QtCore/qglobal.h>

#if defined(EXAMPLE_LIBRARY)
#  define EXAMPLE_EXPORT Q_DECL_EXPORT
#else
#  define EXAMPLE_EXPORT Q_DECL_IMPORT
#endif

#ifdef EXAMPLE_NAMESPACE

# define EXAMPLE_PREPEND_NAMESPACE(name) ::EXAMPLE_NAMESPACE::name
# define EXAMPLE_USE_NAMESPACE using namespace ::EXAMPLE_NAMESPACE;
# define EXAMPLE_BEGIN_NAMESPACE namespace EXAMPLE_NAMESPACE {
# define EXAMPLE_END_NAMESPACE }
# define EXAMPLE_FORWARD_DECLARE_CLASS(name) \
    EXAMPLE_BEGIN_NAMESPACE class name; EXAMPLE_END_NAMESPACE \
    using EXAMPLE_PREPEND_NAMESPACE(name);

#else

# define EXAMPLE_PREPEND_NAMESPACE(name) ::name
# define EXAMPLE_USE_NAMESPACE
# define EXAMPLE_BEGIN_NAMESPACE
# define EXAMPLE_END_NAMESPACE
# define EXAMPLE_FORWARD_DECLARE_CLASS(name) class name;

#endif // EXAMPLE_NAMESPACE

Example/tests/src/testExample:

// Here, I would want to do something like this:

#include <gtest/gtest.h>

int main(int argc, char* argv[])
{
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

How can I write those test, and especially, how would I run them in QT Creator? Where would I tell QT that I want to add the Google Test package/library?

What I tried, was using Visual Studio and a plugin called Qt Visual Studio Tools, to import my QT Project into VS. Then, I installed the NuGet package Google Test. I was able to create tests, but the issue there was, that VS added all the test files (file paths) into my Example.pro files' SOURCES list.

AFAIK, this would include all the test files into my Example library, when I build it, which I don't think is a good idea.

So, is there a "better" way of doing this? Should I use CMake and add some CMakeLists.txt files? And if so, where should they go and what should they contain?

Thanks for any help!

(I should also say, that I am quite a noob with CMake and also a noob with creating libraries in c++)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2025-02-20 16:17:08

好的,所以我现在所做的是:

首先,我在Qt Creator中制作了一个新的subdirs项目,我也称其为xpemote> emampe example 称其为example_base)。然后,我将库项目示例作为子项目添加。但是,这次我没有使其成为共享库,而是使其成为静态库(基本上我删除了DIDINES += example_library并添加config += staticlib)。

现在,我添加了一个自动测试项目作为另一个子标记,我刚刚称为tests。在那里,作为测试框架,我选择了Google Test。然后,我右键单击此tests项目,然后单击添加库 - &gt;内部然后选择我的示例项目。这将我的示例库添加到tests库中,以便我可以在示例 tests tests 测试中使用所有内容。项目。

我现在要做的就是右键单击示例 project并点击构建“示例”

现在,在tests子标记中,我可以编写一些测试并通过单击QT Creator内部左下角的“播放”图标来运行它们。

另外,我的项目结构现在看起来像这样:(

Example_Base/
----> Example/
--------> src/
------------> Example.cpp
--------> include/
------------> Example.hpp
--------> Example.pro
----> Tests/
--------> src/
------------> Test1.cpp
------------> TestX.cpp
------------> main.cpp
--------> Tests.pro
----> Example_Base.pro

tests/src/main.cpp刚运行所有测试)

(另请注意,我仍在使用QMAKE而不是cmake

edit

example_base.pro看起来像这样:

TEMPLATE = subdirs

SUBDIRS += \
    WebSocket \
    Tests

通过附加config> config +=订购在最后,我可以直接点击在“播放”图标上,无明确单击示例并击中构建“示例”

Ok, so what I did now, was this:

First, I made a new subdirs project in QT Creator, which I just also called Example (for simplicity here, I will call it Example_Base). Then, I added my library project Example as a sub project. But instead of making it a shared library, this time I made it a static library (basically I removed the DEFINES += EXAMPLE_LIBRARY and added CONFIG += staticlib).

Now, I added a Auto Test Project as another subproject, which I just called Tests. There, as a test framework, I chose Google Test. I then right-clicked on this Tests project and clicked on Add Library -> Internal where I then selected my Example project. This added my Example library into the Tests library, so that I can use everything from Example inside the Tests testing project.

All I have to do now, is to right-click on the Example project and hit Build "Example".

Now, inside the Tests subproject, I can write some tests and run them by just clicking the "Play" icon in the bottom left corner inside QT Creator.

Also, my project structure looks like this now:

Example_Base/
----> Example/
--------> src/
------------> Example.cpp
--------> include/
------------> Example.hpp
--------> Example.pro
----> Tests/
--------> src/
------------> Test1.cpp
------------> TestX.cpp
------------> main.cpp
--------> Tests.pro
----> Example_Base.pro

(Tests/src/main.cpp just runs all the tests)

(Also note, that I am still using qmake instead of CMake)

EDIT

Example_Base.pro looked like this:

TEMPLATE = subdirs

SUBDIRS += \
    WebSocket \
    Tests

By appending CONFIG += ordered at the end, I can directly click on the "Play" Icon without explicitely right-clicking on Example and hitting Build "Example"!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文