Skip to content

Commit d3ad205

Browse files
committed
Add HelloWorldUnitTest example
1 parent 936de23 commit d3ad205

File tree

9 files changed

+201
-4
lines changed

9 files changed

+201
-4
lines changed

Qt.Test/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ cmake_minimum_required(VERSION 3.20)
44
project(Qt.Test)
55

66
# Projects
7-
#add_subdirectory(TestConsole)
8-
#add_subdirectory(TestGui)
7+
add_subdirectory(HelloWorldsUnitTest)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# Solution
4+
project(HelloWorldsUnitTest)
5+
6+
# Projects
7+
add_subdirectory(HelloWorldUnitTest)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
41+
# xemacs temporary files
42+
*.flc
43+
44+
# Vim temporary files
45+
.*.swp
46+
47+
# Visual Studio generated files
48+
*.ib_pdb_index
49+
*.idb
50+
*.ilk
51+
*.pdb
52+
*.sln
53+
*.suo
54+
*.vcproj
55+
*vcproj.*.*.user
56+
*.ncb
57+
*.sdf
58+
*.opensdf
59+
*.vcxproj
60+
*vcxproj.*
61+
62+
# MinGW generated files
63+
*.Debug
64+
*.Release
65+
66+
# Python byte code
67+
*.pyc
68+
69+
# Binaries
70+
# --------
71+
*.dll
72+
*.exe
73+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(HelloWorldUnitTest)
4+
5+
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
8+
find_package(Qt6Core CONFIG REQUIRED)
9+
find_package(Qt6 REQUIRED COMPONENTS Core Test)
10+
if (Qt6_FOUND)
11+
qt_standard_project_setup()
12+
else ()
13+
find_package(Qt5 REQUIRED COMPONENTS Core Test)
14+
set(CMAKE_AUTOMOC ON)
15+
set(CMAKE_AUTORCC ON)
16+
set(CMAKE_AUTOUIC ON)
17+
endif ()
18+
19+
add_executable(${PROJECT_NAME} src/HelloWorldUnitTest.cpp)
20+
target_link_libraries(${PROJECT_NAME} Qt::Core Qt::Test)
21+
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Qt.Test/HelloWorldsUnitTest")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG += console c++17
2+
CONFIG -= app_bundle
3+
QT = core testlib
4+
SOURCES = src/HelloWorldUnitTest.cpp
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# HelloWorldConsole
2+
3+
The classic first application "Hello, World!" with QTextStream stream.
4+
5+
## Sources
6+
7+
[src/HelloWorldConsole.cpp](src/HelloWorldConsole.cpp)
8+
9+
[CMakeLists.txt](CMakeLists.txt)
10+
11+
## Output
12+
13+
```
14+
Hello, World!
15+
```
16+
17+
## Generate and build
18+
19+
### Qt Creator
20+
21+
To build these projects, open `HelloWorldConsole.pro` file with Qt Creator.
22+
23+
### CMake
24+
25+
To build this project, open "Terminal" and type following lines:
26+
27+
Set `CMAKE_PREFIX_PATH` with Qt6 install path.
28+
29+
#### Windows :
30+
31+
``` cmake
32+
mkdir build
33+
cd build
34+
cmake ..
35+
start ./HelloWorldConsole.sln
36+
```
37+
38+
#### macOS :
39+
40+
``` cmake
41+
mkdir build
42+
cd build
43+
cmake .. -G "Xcode"
44+
open ./HelloWorldConsole.xcodeproj
45+
```
46+
47+
#### Linux with Code::Blocks :
48+
49+
``` cmake
50+
mkdir build
51+
cd build
52+
cmake .. -G "CodeBlocks - Unix Makefiles"
53+
xdg-open ./HelloWorldConsole.cbp > /dev/null 2>&1
54+
```
55+
56+
#### Linux :
57+
58+
``` cmake
59+
mkdir build
60+
cd build
61+
cmake ..
62+
cmake --build . --config Debug
63+
./HelloWorldConsole
64+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <QTest>
2+
#include <QString>
3+
4+
namespace UnitTests {
5+
class HelloWorldTest : public QObject{
6+
Q_OBJECT
7+
private slots:
8+
void CreateStringFromLiteral() {
9+
auto s = QString {"Hello, World!"};
10+
QCOMPARE_EQ(s.size(), 13);
11+
QCOMPARE_EQ(s, "Hello, World!");
12+
}
13+
};
14+
}
15+
16+
QTEST_MAIN(UnitTests::HelloWorldTest)
17+
#include "HelloWorldUnitTest.moc"
18+
19+
// This code can produce the following output :
20+
//
21+
// ********* Start testing of UnitTests::HelloWorldTest *********
22+
// Config: Using QtTest library 6.6.0, Qt 6.6.0 (arm64-little_endian-lp64 shared (dynamic) release build; by Apple LLVM 15.0.0 (clang-1500.0.40.1)), macos 14.2
23+
// PASS : UnitTests::HelloWorldTest::initTestCase()
24+
// PASS : UnitTests::HelloWorldTest::CreateStringFromLiteral()
25+
// PASS : UnitTests::HelloWorldTest::cleanupTestCase()
26+
// Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 1ms
27+
// ********* Finished testing of UnitTests::HelloWorldTest *********
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TEMPLATE = subdirs
2+
SUBDIRS = \
3+
HelloWorldUnitTest

Qt.Test/Qt.Test.pro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
TEMPLATE = subdirs
22
SUBDIRS = \
3-
# TestConsole \
4-
# TestGui
3+
HelloWorldsUnitTest

0 commit comments

Comments
 (0)