Skip to content

Commit d1b0cac

Browse files
msmolensjcfrpatmarion
committed
Add cleanup/finalization tests
Add tests that check for clean cleanup and finalization in different scenarios. Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]> Co-authored-by: Pat Marion <[email protected]>
1 parent 8f65e18 commit d1b0cac

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ if(BUILD_TESTING)
279279
tests/PythonQtTests.h
280280
)
281281

282+
if(PythonQt_Wrap_Qtcore)
283+
include_directories(generated_cpp${generated_cpp_suffix})
284+
285+
list(APPEND test_sources
286+
tests/PythonQtTestCleanup.cpp
287+
tests/PythonQtTestCleanup.h
288+
)
289+
QT4_WRAP_CPP(test_sources
290+
tests/PythonQtTestCleanup.h
291+
)
292+
endif()
293+
282294
set_property(SOURCE tests/PythonQtTestMain.cpp PROPERTY COMPILE_DEFINITIONS "main=tests_PythonQtTestMain")
283295

284296
add_executable(PythonQtCppTests ${test_sources})

tests/PythonQtTestCleanup.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "PythonQtTestCleanup.h"
2+
#include "PythonQt.h"
3+
#include "PythonQt_QtBindings.h"
4+
5+
void PythonQtTestCleanup::initTestCase()
6+
{
7+
}
8+
9+
void PythonQtTestCleanup::cleanupTestCase()
10+
{
11+
}
12+
13+
void PythonQtTestCleanup::init()
14+
{
15+
// Initialize before each test
16+
17+
PythonQt::init(PythonQt::IgnoreSiteModule);
18+
PythonQt_init_QtBindings();
19+
20+
_helper = new PythonQtTestCleanupHelper();
21+
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
22+
PythonQt::self()->addObject(main, "obj", _helper);
23+
}
24+
25+
void PythonQtTestCleanup::cleanup()
26+
{
27+
// Finalize and cleanup after each test
28+
29+
PythonQtObjectPtr main = PythonQt::self()->getMainModule();
30+
PythonQt::self()->removeVariable(main, "obj");
31+
delete _helper;
32+
_helper = NULL;
33+
34+
if (Py_IsInitialized()) {
35+
Py_Finalize();
36+
}
37+
38+
PythonQt::cleanup();
39+
}
40+
41+
void PythonQtTestCleanup::testQtEnum()
42+
{
43+
QVERIFY(_helper->runScript(
44+
"import PythonQt.QtCore\n" \
45+
"x = PythonQt.QtCore.QFile.ReadOnly\n" \
46+
"obj.setPassed()"
47+
));
48+
}
49+
50+
void PythonQtTestCleanup::testCallQtMethodInDel()
51+
{
52+
QVERIFY(_helper->runScript(
53+
"import PythonQt.QtCore\n" \
54+
"class TimerWrapper(object):\n" \
55+
" def __init__(self):\n" \
56+
" self.timer = PythonQt.QtCore.QTimer()\n" \
57+
" def __del__(self):\n" \
58+
" self.timer.setSingleShot(True)\n" \
59+
"x = TimerWrapper()\n" \
60+
"obj.setPassed()\n"
61+
));
62+
}
63+
64+
bool PythonQtTestCleanupHelper::runScript(const char* script)
65+
{
66+
_passed = false;
67+
PyRun_SimpleString(script);
68+
return _passed;
69+
}

tests/PythonQtTestCleanup.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef _PYTHONQTTESTCLEANUP_H
2+
#define _PYTHONQTTESTCLEANUP_H
3+
4+
#include "PythonQt.h"
5+
#include <QtTest/QtTest>
6+
7+
class PythonQtTestCleanupHelper;
8+
9+
//! Test PythonQt cleanup and Python interpreter finalization
10+
class PythonQtTestCleanup : public QObject
11+
{
12+
Q_OBJECT
13+
14+
private Q_SLOTS:
15+
void initTestCase();
16+
void cleanupTestCase();
17+
void init();
18+
void cleanup();
19+
20+
void testQtEnum();
21+
void testCallQtMethodInDel();
22+
23+
private:
24+
PythonQtTestCleanupHelper* _helper;
25+
};
26+
27+
//! Test helper class
28+
class PythonQtTestCleanupHelper : public QObject
29+
{
30+
Q_OBJECT
31+
public:
32+
PythonQtTestCleanupHelper() :
33+
_passed(false) {
34+
};
35+
36+
bool runScript(const char* script);
37+
38+
public Q_SLOTS:
39+
void setPassed() { _passed = true; }
40+
41+
private:
42+
bool _passed;
43+
};
44+
45+
#endif

tests/PythonQtTestMain.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include "PythonQt.h"
4343
#include "PythonQtTests.h"
44+
#include "PythonQtTestCleanup.h"
4445

4546
#include <QApplication>
4647

@@ -60,6 +61,13 @@ int main(int argc, char *argv[])
6061

6162
PythonQt::cleanup();
6263

64+
if (Py_IsInitialized()) {
65+
Py_Finalize();
66+
}
67+
68+
PythonQtTestCleanup cleanup;
69+
failCount += QTest::qExec(&cleanup, argc, argv);
70+
6371
if (failCount>0) {
6472
std::cerr << "Tests failed: " << failCount << std::endl;
6573
} else {

0 commit comments

Comments
 (0)