Skip to content

Commit 328e56b

Browse files
committed
Run regular PythonQt tests from dynamically loaded shared library
1 parent d823ea8 commit 328e56b

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ if(BUILD_TESTING)
272272

273273
set_property(SOURCE tests/PythonQtTestMain.cpp PROPERTY COMPILE_DEFINITIONS "main=tests_PythonQtTestMain")
274274

275-
list(APPEND test_sources
275+
list(APPEND PythonQtTests_sources
276276
tests/PythonQtTests.cpp
277277
tests/PythonQtTests.h
278278
)
279279

280-
QT4_WRAP_CPP(test_sources
280+
QT4_WRAP_CPP(PythonQtTests_sources
281281
tests/PythonQtTests.h
282282
)
283283

@@ -295,7 +295,7 @@ if(BUILD_TESTING)
295295
set_property(SOURCE tests/PythonQtTestMain.cpp APPEND PROPERTY COMPILE_DEFINITIONS "PythonQt_Wrap_Qtcore")
296296
endif()
297297

298-
add_executable(PythonQtCppTests ${test_sources})
298+
add_executable(PythonQtCppTests ${test_sources} ${PythonQtTests_sources})
299299
target_link_libraries(PythonQtCppTests PythonQt)
300300

301301
add_test(
@@ -308,7 +308,10 @@ if(BUILD_TESTING)
308308
# PythonQt) properly initializes PythonQt.
309309
#
310310

311-
add_library(PythonQtDynamicLoaderSharedLibrary SHARED tests/PythonQtDynamicLoaderSharedLibrary.cpp)
311+
add_library(PythonQtDynamicLoaderSharedLibrary SHARED
312+
tests/PythonQtDynamicLoaderSharedLibrary.cpp
313+
${PythonQtTests_sources}
314+
)
312315
target_link_libraries(PythonQtDynamicLoaderSharedLibrary PythonQt)
313316

314317
add_executable(PythonQtDynamicLoader tests/PythonQtDynamicLoader.cpp)

tests/PythonQtDynamicLoader.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ int main(int argc, char* argv[])
4545
<< std::endl;
4646
return EXIT_FAILURE;
4747
}
48+
49+
//
50+
// Resolve and invoke 'run_pythonqt_tests' function.
51+
//
52+
typedef int (*FUNC_ARGC_ARGV_RETURNS_INT_TYPE)(int argc, char* argv[]);
53+
FUNC_ARGC_ARGV_RETURNS_INT_TYPE func2 =
54+
(FUNC_ARGC_ARGV_RETURNS_INT_TYPE) library.resolve("run_pythonqt_tests");
55+
if (!func2)
56+
{
57+
std::cerr << "Failed to resolve symbol 'run_pythonqt_tests'" << std::endl;
58+
return EXIT_FAILURE;
59+
}
60+
61+
result = func2(argc, argv);
62+
expected = 0;
63+
if (result != expected)
64+
{
65+
std::cerr << "Problem with function 'run_pythonqt_tests':\n"
66+
<< "\tresult: " << result << "\n"
67+
<< "\texpected: " << expected
68+
<< std::endl;
69+
return EXIT_FAILURE;
70+
}
71+
4872
return EXIT_SUCCESS;
4973
}
5074

tests/PythonQtDynamicLoaderSharedLibrary.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
#include "PythonQt.h"
4+
#include "PythonQtTests.h"
45

56
#ifdef Q_OS_WIN
67
#define MY_EXPORT __declspec(dllexport)
@@ -16,6 +17,26 @@ extern "C"
1617
return 42;
1718
}
1819

20+
int MY_EXPORT run_pythonqt_tests(int argc, char* argv[])
21+
{
22+
// Copied from PythonQtTestMain.cpp
23+
int failCount = 0;
24+
PythonQtTestApi api;
25+
failCount += QTest::qExec(&api, argc, argv);
26+
PythonQtTestSignalHandler signalHandler;
27+
failCount += QTest::qExec(&signalHandler, argc, argv);
28+
PythonQtTestSlotCalling slotCalling;
29+
failCount += QTest::qExec(&slotCalling, argc, argv);
30+
31+
PythonQt::cleanup();
32+
33+
if (Py_IsInitialized()) {
34+
Py_Finalize();
35+
}
36+
37+
return failCount;
38+
}
39+
1940
}
2041

2142
struct StaticInitializer

0 commit comments

Comments
 (0)