Skip to content

Commit 42e6bd8

Browse files
committed
Add pytest-qt and centralize QApplication setup for tests
Added pytest-qt to test dependencies and requirements for better PyQt6 test support. Centralized QApplication initialization in conftest.py to ensure headless and CI compatibility, removing redundant QApplication setup from individual test files.
1 parent 3b803fa commit 42e6bd8

6 files changed

Lines changed: 22 additions & 19 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ requires = [
3737
test_requires = [
3838
"pytest",
3939
"types-requests",
40+
"pytest-qt",
4041
]
4142

4243
[tool.mypy]

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ PySocks
1313
pre-commit
1414
mypy
1515
pyright
16-
pytest
16+
pytest
17+
pytest-qt

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
import sys
22
from pathlib import Path
3+
import os
34

45
# Add the src directory to the python path
56
src_path = Path(__file__).parent.parent / "src"
67
sys.path.append(str(src_path))
8+
9+
# Use offscreen Qt platform for headless testing in CI
10+
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
11+
12+
13+
def pytest_configure(config):
14+
"""Create a QApplication early so modules that import Qt at import-time can succeed.
15+
16+
This runs before collection, preventing ImportError during test collection on headless CI.
17+
"""
18+
try:
19+
from PyQt6.QtWidgets import QApplication
20+
21+
if QApplication.instance() is None:
22+
QApplication([])
23+
except Exception:
24+
# If PyQt is not available, allow tests to fail later rather than crash here.
25+
pass

tests/test_creator_downloader.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
import os
99
import sys
1010

11-
from PyQt6.QtWidgets import QApplication
12-
13-
# Ensure QApplication exists for PyQt6 components
14-
app = QApplication.instance()
15-
if app is None:
16-
app = QApplication([])
1711

1812
try:
1913
from kemonodownloader.creator_downloader import (

tests/test_post_downloader.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
import sys
88
from urllib.parse import urljoin
99

10-
from PyQt6.QtWidgets import QApplication
11-
12-
# Ensure QApplication exists for PyQt6 components
13-
app = QApplication.instance()
14-
if app is None:
15-
app = QApplication([])
1610

1711
try:
1812
from kemonodownloader.post_downloader import (

tests/test_settings.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
import tempfile
99

1010
from PyQt6.QtCore import QSettings
11-
from PyQt6.QtWidgets import QApplication
12-
13-
# Ensure QApplication exists for PyQt6 components
14-
app = QApplication.instance()
15-
if app is None:
16-
app = QApplication([])
1711

1812

1913
class TestDefaultSettings:

0 commit comments

Comments
 (0)