在QT创建者中为库创建(自动化)测试
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我现在所做的是:
首先,我在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内部左下角的“播放”图标来运行它们。另外,我的项目结构现在看起来像这样:(
tests/src/main.cpp
刚运行所有测试)(另请注意,我仍在使用
QMAKE
而不是cmake
)edit
example_base.pro
看起来像这样:通过附加
config> config +=订购
在最后,我可以直接点击在“播放”图标上,无明确单击示例
并击中构建“示例”
!Ok, so what I did now, was this:
First, I made a new
subdirs
project in QT Creator, which I just also calledExample
(for simplicity here, I will call itExample_Base
). Then, I added my library projectExample
as a sub project. But instead of making it a shared library, this time I made it a static library (basically I removed theDEFINES += EXAMPLE_LIBRARY
and addedCONFIG += staticlib
).Now, I added a
Auto Test Project
as another subproject, which I just calledTests
. There, as a test framework, I choseGoogle Test
. I then right-clicked on thisTests
project and clicked onAdd Library -> Internal
where I then selected myExample
project. This added myExample
library into theTests
library, so that I can use everything fromExample
inside theTests
testing project.All I have to do now, is to right-click on the
Example
project and hitBuild "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:
(
Tests/src/main.cpp
just runs all the tests)(Also note, that I am still using
qmake
instead ofCMake
)EDIT
Example_Base.pro
looked like this:By appending
CONFIG += ordered
at the end, I can directly click on the "Play" Icon without explicitely right-clicking onExample
and hittingBuild "Example"
!